By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

help displaying output

P: 7
Hey guys.... i've never posted here before, but everytime i need some c++ help, google takes me here, so i figured i'd give it a shot....i'm posting about homework for a class.

this is a pretty newbie question.....

anyways....we had to input data from a file...unkown number of rows...manipulate the numbers....then output all the data...i wrote my program..it works perfectly, but i have to display the the results ....which are 4 numbers/words per row....and have to make em line up nicely in a column.
I tried using the setw command, but my data doesn't line up if any 1 of the numbers in 1 row has a different number of digits in another...basically...

1 2 3
5 6 7
12 34 56

that happens
is there a different command i can use???

thanks
Jul 1 '06 #1
Share this Question
Share on Google+
4 Replies


100+
P: 293
D_C
Is there a specified range for each number input? In other words, do you know that no number will be more than, say, 4 digits? Assuming no negative numbers (a minus sign would require an extra space), I'll give some psuedocode

Expand|Select|Wrap|Line Numbers
  1. const int MAX_NUM_DIGITS = 4;
  2. const int WORDS_PER_LINE = 4;
  3. string output =  "";
  4.  
  5. For each line
  6. {
  7.   output = "";
  8.   i = 0;
  9.   While (i < WORDS_PER_LINE)
  10.   {
  11.     output += number;
  12.     While(output.length < (i * (1+MAX_NUM_DIGITS))) 
  13.     { // The extra space is so that the numbers don't run together
  14.        output += " ";
  15.     }
  16.     i++;
  17.   }
  18. }
Jul 1 '06 #2

Banfa
Expert Mod 5K+
P: 8,916
That seems very complex D_C I went for

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(int argc, char* argv[])
  6. {
  7.     int a=1;
  8.     int b=2;
  9.     int c=3;
  10.  
  11.     int x=10;
  12.     int y=11;
  13.     int z=12;
  14.  
  15.     cout.width(4);
  16.     cout << a;
  17.     cout.width(4);
  18.     cout << b;
  19.     cout.width(4);
  20.     cout << c;
  21.     cout << endl;
  22.  
  23.     cout.width(4);
  24.     cout << x;
  25.     cout.width(4);
  26.     cout << y;
  27.     cout.width(4);
  28.     cout << z;
  29.     cout << endl;
  30.  
  31.     return 0;
  32. }
  33.  
Output
Expand|Select|Wrap|Line Numbers
  1.    1   2   3
  2.   10  11  12
Which seems to work, however even this seems long winded compared to the c version which makes me wonder if there is a better way of doing it in C++

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(int argc, char* argv[])
  6. {
  7.     int a=1;
  8.     int b=2;
  9.     int c=3;
  10.  
  11.     int x=10;
  12.     int y=11;
  13.     int z=12;
  14.  
  15.     printf("%4d%4d%4d\n", a, b, c);
  16.     printf("%4d%4d%4d\n", x, y, z);
  17.     return 0;
  18. }
Output


Output
Expand|Select|Wrap|Line Numbers
  1.    1   2   3
  2.   10  11  12
Jul 1 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
Oh and wandafoda if you need more help then posting the output code you are currently using will help us help you :)
Jul 1 '06 #4

P: 7
D_C and Banfa...thanx....extremely helpful....none of those sorta things were in our lecture notes....and we covered about 15 chapters in 2 weeks.....really hard to find the material in the book.
Jul 3 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.