Write a for loop that outputs the days of the week

Write a for loop that outputs the days of the week, each on a separate line, starting from Saturday and working backward to Sunday.


I think what I have is correct, but the only problem is there is no output! I can't for the life of me figure out why..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iomanip>

using namespace std;

int count;

int main()
{
	for (int count = 6; count <=0; count--)
		switch (count)
		{
			case 0 : cout << "Sunday";break;
			case 1 : cout << "Monday";break;
			case 2 : cout << "Tuesday";break;
			case 3 : cout << "Wednesday";break;
			case 4 : cout << "Thursday";break;
			case 5 : cout << "Friday";break;
			case 6 : cout << "Saturday";break;
			default : cout<< "Error";break;
		}
	return 0;
}
There is a bug in the for-loop condition. It should be count >= 0. You don't need to include the <iomanip> header file. You forgot to add the newline character at the end of each string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int count;

int main()
{
	for (int count = 6; count >= 0; count--)
		switch (count)
		{
			case 0 : cout << "Sunday\n";break;
			case 1 : cout << "Monday\n";break;
			case 2 : cout << "Tuesday\n";break;
			case 3 : cout << "Wednesday\n";break;
			case 4 : cout << "Thursday\n";break;
			case 5 : cout << "Friday\n";break;
			case 6 : cout << "Saturday\n";break;
			default : cout<< "Error\n";break;
		}
	return 0;
}
Last edited on
Thank you for the timely response. The code you posted works and runs great, now I have come to the second part of the problem.

Change the For loop in Ch7-WarmUp3 so that it outputs the days of the week in forward order, starting on Wednesday and going through Tuesday.


This is what I have come up with,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iomanip>

using namespace std;

int count;

int main()
{
	for (int count = 3; count >= 0; count++)
		switch (count)
		{
			case 0 : cout << "Sunday\n";break;
			case 1 : cout << "Monday\n";break;
			case 2 : cout << "Tuesday\n";break;
			case 3 : cout << "Wednesday\n";break;
			case 4 : cout << "Thursday\n";break;
			case 5 : cout << "Friday\n";break;
			case 6 : cout << "Saturday\n";break;
			default : cout<< "Error\n";break;
		}
	return 0;
}

however it produces "ERROR" as output in infinite loop.

I conclude that this is because it gets stuck going up and up past the integer 6 (never having a case of "0-2"). So, is changing the output statements the only way to correct this? As changing the case integers doesn't seem to warrant any different results..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iomanip>

using namespace std;

int count2=6;

int main()
{
	for (int count = 3; count <= count2; count++)
		switch (count)
		{
			case 0 : cout << "Sunday\n";break;
			case 1 : cout << "Monday\n";break;
			case 2 : cout << "Tuesday\n";count2=2;break;
			case 3 : cout << "Wednesday\n";break;
			case 4 : cout << "Thursday\n";break;
			case 5 : cout << "Friday\n";break;
			case 6 : cout << "Saturday\n";count=-1;break;
			default : cout<< "Error\n";break;
		}
	return 0;
}
@Zero Cool

Here's how I would do your problems.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

using namespace std;

int count;

int main()
{
	string WeekDays[]={"Saturday", "Friday","Thursday","Wednesday","Tuesday","Monday","Sunday"};
	int DayOrder[]={3,2,1,0,6,5,4};
	for (int count = 0; count <7; count++)
	{
		cout <<  WeekDays[count] << endl;
	}
	cout << endl << endl;
	for (int count = 0; count <7; count++)
	{
		cout << WeekDays[DayOrder[count]] << endl;
	}
	cout << endl << endl<<endl;	
	return 0;
}
Pls am kobina, am new in computer programming and i will be needing you guy's help alot. Good Morning
Topic archived. No new replies allowed.