counting table

May 28, 2013 at 10:41pm
so im trying to make a function that will count up and out put in a calendar style table but my counter is backwards. this is what i got when i tried.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  int main()
{
   // display the header
   cout << "Mon Tue Wen Thu Fri Sat Sun\n";
   
   //
   for (int start = 0; start < 30; )
      cout << setw(3) << start++
           << setw(4) << start++
           << setw(4) << start++
           << setw(4) << start++
           << setw(4) << start++
           << setw(4) << start++
           << setw(4) << start++
           << endl;

   return 0;
}


but my out put is always
Mon Tue Wen Thu Fri Sat Sun
6 5 4 3 2 1 0
ect....

(the columns are aligned in the out put.)
this is just a skeleton so it requires not input (and doesnt start at one) why is it counting backwards?
Last edited on May 28, 2013 at 10:43pm
May 28, 2013 at 11:19pm
closed account (jyU4izwU)
Well Thats the easy way... But You Probably Dont Want It... i Can Help if You Specifiy What the Problem is.
1
2
cout << "\n Mon  Tue Wen  Thu   Fri   Sat   Sun                                                                                               "
cout << "\n   1   2    3     4    5   6    7                                                                "
Last edited on May 28, 2013 at 11:20pm
May 28, 2013 at 11:26pm
I did not know how to solve the issue with the reverse output with that code but I managed to use this. Also your for loop is missing an interval. Lastly you need to set start = 1 since 1 would be the first day

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int values = 0;
// display the header
cout << " Mon Tue Wen Thu Fri Sat Sun\n";


for (int start = 1; start <= 30;start++ )//set increment for days
{
cout << setw(4) << start; //setting the number of digits per value
values++; //using values as a number of days per line
if (values==7)
{
cout<<endl;//once values reach 7 for days of week
values=0; // endl and rest values to 0 to run if again
}
}
cout<<endl;
system("pause");
return 0;
}
Last edited on May 28, 2013 at 11:38pm
May 28, 2013 at 11:30pm
The problem is that the order of evaluating of function arguments is not defined. Usually they are evaluated from the right to the left.

This code

1
2
3
4
5
6
7
8
      cout << setw(3) << start++
           << setw(4) << start++
           << setw(4) << start++
           << setw(4) << start++
           << setw(4) << start++
           << setw(4) << start++
           << setw(4) << start++
           << endl;



can be rewritten (with omiting unimportant details) the following way

cout.operator <<( start++ ).operator <<( start++ ).operator <<( start++ ).operator <<( start++ ).operator <<( start++ ).operator <<( start++ ).operator <<( start++ );

To escape this uncertainty it is better split this compound expression into several statements;

1
2
3
4
5
6
7
8
      cout << setw(3) << start++;
      cout << setw(4) << start++;
      cout << setw(4) << start++;
      cout << setw(4) << start++;
      cout << setw(4) << start++;
      cout << setw(4) << start++;
      cout << setw(4) << start++;
      cout << endl;

Last edited on May 28, 2013 at 11:31pm
May 28, 2013 at 11:43pm
ah should of done multiple couts myself but should probably be done in a while loop and would need to be <=30 because <30 will only print out 29 days

int start = 1

while(start<=30)
{
cout << setw(3) << start++;
cout << setw(4) << start++;
cout << setw(4) << start++;
cout << setw(4) << start++;
cout << setw(4) << start++;
cout << setw(4) << start++;
cout << setw(4) << start++;
cout << endl;
}
Last edited on May 28, 2013 at 11:46pm
May 29, 2013 at 5:04pm
Thanks for the ideas everyone.

(and i know i was aware about it starting at 0 and not including the right numbers yet, I was just focusing on the output. I can fix the other things when i write the full program. )
Last edited on May 29, 2013 at 5:05pm
Topic archived. No new replies allowed.