Monday, May 24, 2010

How to format output with setw() in C++?

In my code i need the output to be seperated by 5 spaces (wich i have alread done as you can see) but now i need it only to print 10 words/numbers per line insted of stretching the whole window long. How would I do this using %26lt;%26lt;endl? if so how becasue i have entered it but can get it right. Thanks





heres the code





#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


using namespace std;


bool bip(int num)


{


if (num % 3 == 0)


return true;


else


return false;


}


bool bop(int num)


{


if (num % 7 == 0)


return bop;


else


return false;


}


int main()


{


int num;


for(num= 1; num %26lt; 101; num++)


{


if(bop(num) == true)


cout %26lt;%26lt;setw(5) %26lt;%26lt;"bop";


else if(bip(num) == true)


cout %26lt;%26lt;setw(5) %26lt;%26lt;"bip";


else


cout %26lt;%26lt;setw(5) %26lt;%26lt;num; }


}

How to format output with setw() in C++?
Inside your for-loop put this check first.





if ((num % 10) == 0)


{


cout %26lt;%26lt; endl;


}





Also, it may be a typo but in bop() you are trying to return bop rather than true.


No comments:

Post a Comment