Need help with an easy program

I can't find my error in this:
Here's the problem:
-------------------------------------------------------
You should then leave one blank line and prompt for and accept from the user the "Day of the Week." You are to use single characters to represent each day of the week ('S' 'M' 'T' 'W' 'R' 'F' 'A'). You should display these choices to the user as part of your prompt. Your program should assume the user will only enter uppercase letters for the days of the week; all other characters entered (including the lowercase letters for the days) are to be considered invalid.

Test the value that the user enters and display an appropriate response, using the following algorithm:

if Saturday or Sunday then
write a "Weekend" message, such as: "Hooray!, it's the weekend!"
else
if it's a Weekday then
prompt for and accept the hour of day (0 through 23)
[you can assume the user will enter a valid integer]
if hour is between 8 and 17 inclusive then
write a "should be at work" message
else
if hour is >= 0 and < 8 then
write a "still asleep" message
else
if hour is > 17 and < 24 then
write an "off work" message
else
write an "hour invalid" message
else
write a "day invalid" message

Write the program so that it goes through this sequence only once. If the user would like to enter a new day he must rerun the program.

Please note that the algorithm shown above is not in proper C syntax. It is merely an English-like stating (commonly referred to as pseudocode) of the algorithm to be used for this program. For this pseudocode to work properly in C, you will have to declare the appropriate variables, perform the necessary comparison tests (as indicated) using the proper C if-else-statement syntax, and (where needed due to performing more than one command inside an if statement) properly use blocks.
---------------------------------------------------------------

Here's my code:
/* C++ Program
*/

#include <iostream>

int main() {
int hour = 0;
char day = 'q';

while (day != 'S' && day != 'M' && day != 'T' && day != 'W' && day != 'R' && day != 'F' && day != 'A')
{
cout << "What day is it? << endl << "Please enter your response with a capital letter: S, M, T, W, R, F, or A" << endl;
cin >> day;
if (day != 'S' && day != 'M' && day != 'T' && day != 'W' && day != 'R' && day != 'F' && day != 'A')
cout << "Invalid entry" << endl;
}
if (day == 'S' || day == 'A')
cout << "Enjoy the weekend! Yay!" << endl;
else {
while (hour <= 0 or hour >= 23)
{
cout << "What hour is it?" << endl << "Enter a number between 0 and 23" << endl;
cin >> hour;
if (hour <= 0 or hour >= 23)
cout << "Invalid hour" << endl;
}
if (hour >= 8 && hour <= 17)
cout << "You should be working! Get back to work!" << endl;
if (hour >= 0 && hour <= 8)
cout << "Go back to sleep!" << endl;
if (hour >= 17 && hour <= 23)
cout << "You're off work! Relax!" << endl;
}
return 0;
}
Your program very nearly works- excellent job! The logical flow is correct- you just have a few syntax errors.

Try this:
replace all instances of cin and cout with std::cin and std::cout.
You are missing a quote on line nine, just after '"What day is it'.
replace all endl with "\n" (quote marks included)
replace all " or " with " || ", except for the " or " in the string "S, M, T, W, R, F, or A"

Furthermore, although it isn't strictly mandatory, it's good practice to use curly brackets with your "if" statements. It makes things more maintainable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

int main() {
	int hour = 0;
	char day = 'q';

	while (day != 'S' && day != 'M' && day != 'T' && day != 'W' && day != 'R' && day != 'F' && day != 'A'){
		std::cout << "What day is it?" << "\n" << "Please enter your response with a capital letter: S, M, T, W, R, F, or A" << "\n";
		std::cin >> day;
		if (day != 'S' && day != 'M' && day != 'T' && day != 'W' && day != 'R' && day != 'F' && day != 'A'){
			std::cout << "Invalid entry" << "\n";
		}
	}
	if (day == 'S' || day == 'A'){
		std::cout << "Enjoy the weekend! Yay!" << "\n";
	}
	else {
		while (hour <= 0 || hour >= 23){
			std::cout << "What hour is it?" << "\n" << "Enter a number between 0 and 23" << "\n";
			std::cin >> hour;
			if (hour <= 0 || hour >= 23){
				std::cout << "Invalid hour" << "\n";
			}
		}
		if (hour >= 8 && hour <= 17){
			std::cout << "You should be working! Get back to work!" << "\n";
		}
		if (hour >= 0 && hour <= 8){
			std::cout << "Go back to sleep!" << "\n";
		}
		if (hour >= 17 && hour <= 23){
			std::cout << "You're off work! Relax!" << "\n";
		}
	}
	return 0;
}


In the future, don't forget to use code tags, and list the error messages for us so we don't need to compile it ourselves.
Last edited on
closed account (z05DSL3A)
AS I read it the pseudocode should be...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Promt what day

if day = S or A
{
    write Weekend message
}
else if day = M .... F
{
    promt hour
    
    if hour between 8 and 17
    {
       write work message
    }
    else if hour between 0 and 8
    {
       write sleep message
    }
    else if hour between 17 and 24
    {
        write out of work message
    }
    else
    {
        write invalid time message
    }
}
else
{
    write invalid day message
}  

return
shouldn't there also be:

using namespace std;

beneath the #include<iostream>

just wondering.
ACHRN, you have an option:
1. add "std::" before all of your cin's and cout's
2. put "using namespace std;" beneath the #include line.

You can use one method or the other. #2 is less typing, but I heard that #1 is less error-prone.
halo, i try abit. is tat u less of declaration of the char of day??
and one of hte if hour <=8 should cancel the =. or else it would promp two answer.
your should be working and you should get back to sleep.
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>


int main()
{
	char day;
	
	std::cout << "What day is it?" << "\n" << "Please enter your response with a capital letter: S, M, T, W, R, F, or A" << "\n";
	std::cin >> day;

	if (day == 'S' || day == 'A')
	{
		std::cout << "Enjoy the weekend! Yay!" << "\n";
	}
	else if (day == 'M' || day == 'T' || day == 'W' || day == 'R' || day == 'F')
	{
		int hour;
		
		std::cout << "What hour is it?" << "\n" << "Enter a number between 0 and 23" << "\n";
		std::cin >> hour;
		
		if (hour >= 8 && hour <= 17)
		{
			std::cout << "You should be working! Get back to work!" << "\n";
		}
		else if (hour >= 0 && hour < 8)
		{
			std::cout << "Go back to sleep!" << "\n";
		}
		else if (hour > 17 && hour < 24)
		{
			std::cout << "You're off work! Relax!" << "\n";
		}
		else
		{
			std::cout << "Hour: Invalid entry" << "\n";
		}
	}
	else
	{
		std::cout << "Day: Invalid entry" << "\n";
	}
	return 0;
}
Topic archived. No new replies allowed.