445,820 Members | 1,187 Online 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
4 Replies

 100+ P: 293 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 const int MAX_NUM_DIGITS = 4; const int WORDS_PER_LINE = 4; string output =  "";   For each line {   output = "";   i = 0;   While (i < WORDS_PER_LINE)   {     output += number;     While(output.length < (i * (1+MAX_NUM_DIGITS)))      { // The extra space is so that the numbers don't run together        output += " ";     }     i++;   } } Jul 1 '06 #2

 Expert Mod 5K+ P: 8,916 That seems very complex D_C I went for Expand|Select|Wrap|Line Numbers #include    using namespace std;   int main(int argc, char* argv[]) {     int a=1;     int b=2;     int c=3;       int x=10;     int y=11;     int z=12;       cout.width(4);     cout << a;     cout.width(4);     cout << b;     cout.width(4);     cout << c;     cout << endl;       cout.width(4);     cout << x;     cout.width(4);     cout << y;     cout.width(4);     cout << z;     cout << endl;       return 0; }   Output Expand|Select|Wrap|Line Numbers    1   2   3   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 #include    using namespace std;   int main(int argc, char* argv[]) {     int a=1;     int b=2;     int c=3;       int x=10;     int y=11;     int z=12;       printf("%4d%4d%4d\n", a, b, c);     printf("%4d%4d%4d\n", x, y, z);     return 0; } Output Output Expand|Select|Wrap|Line Numbers    1   2   3   10  11  12 Jul 1 '06 #3

 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 