tags:

views:

76

answers:

5

I have the following program that generates a multiplication table. A formating problem arises when the outputs reach the double digits. How do I straighten out the columns?

#include <iostream>


using namespace std ;
int main() 
{ 

while (1 != 2)
{
    int column, row, c, r, co, ro; 

    cout << endl ;


    cout << "Enter the number of columns: " ;
    cin >> column ;
    cout << endl ;
    cout << "Enter the number of rows:    " ;
    cin >> row ;
    cout << endl ;



    int temp[column] ;
    c = 1 ; 
    r = 1 ; 
for(ro = 1; ro < row ; ro ++ ){
    for(co = 1; co < column ; co ++ ){
            c = c ++ ;
            r = r ++ ;
            temp [c]= co * ro;

            cout << temp[c] << " ";
}
    cout << endl ;




}


    system("pause");  

}

}
+1  A: 

You could use "\t" instead of " ".

vash47
I'm not going to downvote this since it's useful in at least fairly limited circumstances. But, since it depends entirely on the terminal tabstop settings, it's not really suitable for production-quality code. If you don't control the tabstops, or if you output a number that crosses such a tabstop, your output will be munged.
paxdiablo
+2  A: 

use the setw output manipulator:

cout << setw(3) << temp[c];

By default, this uses spaces to fill, which it looks like you want.

You will need to include iomanip as the documentation says.

JoshD
+3  A: 

C++ had setw and setfill for just this purpose. setw sets the width and setfill sets the fill character.

In your case, you can just use something like:

#include <iostream>
#include <iomanip>

int main (void) {
    std::cout << std::setw(5) << 7 << std::endl; // will output "    7".
    return 0;
}

You have a number of other problems with that code, at least some of which are listed below:

  • You don't allocate enough space for your array, it should be column*row (or use a two-dimensional array).
  • Array indexes start at 0, not 1.
  • c = c++ is not a good idea, c++ will be enough to increment c.
  • You may be trying to increment c twice in each iteration, once if the for statement itself and once in the for body.
  • system("pause"); is an ugly hack where the language provides a perfectly good getchar or cin equivalent.
  • while (1 != 2) just looks plain wrong :-) since 1 will never equal 2. Just use while (1) or for(;;) - any coder worth their salt will know what you mean.
paxdiablo
+2  A: 

You can set width of your column elements by using stream manipulators like this:

cout << setw(3) << temp[c]

But this is something you need to fix besides: c = c++; does not increment the variable!

Alex Emelianov
+1  A: 

This is one of those situations where the old-fashioned printf is a lot easier than cout. Replace cout << temp[c] << " " with printf("%2d ", temp[c]).

And I hope you've discovered the bug in your c calculation.

Mark Ransom
In the immortal words of Yoda when Luke tried to use C features in C++ after the crash on Dagobah: "No, try not. Do, or do not, there is no try." :-) Using `printf` may well be simpler than `cout` _for C coders_, but, if you want to be a C++ coder, use the full power of C++. Yes, I know `printf` will work, so will `#define` macros for code and constants - that doesn't necessarily make them a good idea. Again, not willing to downvote since I used this early on in my transition to C++ and it was a hell of a lot easier than trying to learn iostream/iomanip in its entirety in one day.
paxdiablo
@paxdiablo, I know that printf is *way* out of fashion, and I have the -1 vote to prove it. I also know that it's a lot less type safe than the alternate. I still often find it to be the pragmatic approach, probably the only part of C that I still cling to.
Mark Ransom