gotta be a better way than this?
hi,
this code prints out numbers like so.
1 2 (there should be a 25 space gap)
3 4
5 6 etc.
but i am not happy with my method of doing it . Could anyone show me a better alternative.
code.
#include <iostream>
#include <iomanip> // for use of set(x)
using namespace std;
int main ()
{
int count=1;
int num=0;
for(count = 1; count <= 20;count++)
{
num +=1;
cout << setw(3) << num ;
num +=1;
cout << setw(25) << num <<endl;
}
cout << endl;
return 0;
}
thanks
gotta be a better way than this?
Zen is correct it is being evaluate from right to left. So if you really want to get that code to one line it can be done like this:
Code:
#include <iostream>
#include <iomanip> // for use of setw(x)
using std:: cout;
using std::endl;
using std::setw;
int main ()
{
int count=1, num2 = 0 , num1 = num2;
for(count = 1; count <= 20;count++)
{
cout << setw(3) << --num2 << setw(25) << ( num2 = num1 += 2 ) << endl;
}
cout << endl;
return 0;
}