Hi! I'm taking a summer intro to computer science class and have a question on my code. I am using logical operators to write the below program. I successfully built the instructions but once I get to the below lines, I am stuck as to how to end the program. The questions just get asked again. I've spent an hour looking for videos, FAQs and forums but haven't found my specific problem / code sample that explains how to end the program once I successfully execute the final step. Any hints / assistance (with explanation) is much appreciated!
if (homework2 == "yes" || clean2 == "yes"){
cout << "You can play after school!\n";
Instructions from professor: Problem 5 - A mother told her child on Monday, Tuesday, Wednesday and Thursday “If you room is clean AND you did you homework, you may go out and play afterschool”. On Friday, Saturday and Sunday. she told her son, “If you room is clean OR you did you homework, you may go out and play after school”.
Write a program that asks a child the day of the week, are their room clean, is their homework done, and determine if they can go out and play.Use the fewest if statements.
// ChildHomeworkCleanroomWeekDayWeekend.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
cout << "Child, what day of the week (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday) is it?\n";
string day;
cin >> day;
if (day == "Monday" || "Tuesday" || "Wednesday" || "Thursday")
cout << "Did you do your homework? (yes or no)\n";
string homework;
cin >> homework;
cout << "Did you clean your room? (yes or no)\n";
string clean;
cin >> clean;
if (homework == "yes" && clean == "yes"){
cout << "You can play after school!\n";
}
else{
cout << "You can't play afterschool";
}
if (day == "Friday" || "Saturday" || "Sunday")
cout << "Did you clean your room? (yes or no) \n";
cout << "Did you do your homework? (yes or no)\n";
string homework2;
cin >> homework2;
cout << "Did you clean your room? (yes or no)\n";
string clean2;
cin >> clean2;
if (homework2 == "yes" || clean2 == "yes"){
cout << "You can play after school!\n";
}
else{
cout << "You can't play afterschool";
}
return 0;
}
Some of your if() statements are malformed. When using the logical operators you need both sides of the comparison: if (day == "Friday" || day == "Saturday")
Also remember that without the "optional" braces only one line will be included within the body of the statement.
And don't forget that C++ is case sensitive, "Yes" is not the same as "yes" or "YES" ect.
Thanks jlb, I also found that I was incorrectly comparing strings as if they were integers. I changed
if (day == "Friday" || "Saturday" || "Sunday")
to
else if ((day.compare("Friday") == 0) || (day.compare("Saturday") == 0) || (day.compare("Sunday") == 0)){ //compares strings
I also found that I was incorrectly comparing strings as if they were integers.
No, you were comparing the strings using the overloaded comparison operator. It is legal to compare C++ strings with the comparison operators as well as using the compare() member funciton. And don't forget that the compare() function has three possible return values zero, less than zero, or greater than zero.