if/else not working

Oct 2, 2015 at 7:16am
I'm new to c++ and im trying to make a program that will allow a user to figure out what Gregory is doing based on the day of the week and other variables.


#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main() {



cout << "Find out what Gregory will do today. \n";



string weekday;


cout << "What day of the week is it? \n";

getline (cin,weekday);



if(weekday == "wednesday") {

cout << "It is Wednesday";

}



return 0;

}

I havent fully figured out how to get strings from a user yet so this was my first attempt, but when running the program it allows a user to type a weekday, but doesn't go on to print "It is Wednesday" regardless of user input.

//this code is my second try

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main() {

cout << "Find out what Gregory will do today. \n";

string weekday;


cout << "What day of the week is it? \n";
cin >> weekday;

if(weekday == "wednesday") {
cout << "It is Wednesday";
}

else {
cout << "It's not Wednesday";
}

return 0;
}

In my second try I got frustrated and went back to just using cin to collect input, but with this code, "It's not Wednesday" is printed regardless of user input.
Oct 2, 2015 at 7:49am
"It's not Wednesday" is printed regardless of user input.
Cannot reproduce. When I enter "wednesday" (all lowercase), it prints It is Wednesday
Oct 2, 2015 at 8:03am
your second code is working fine, i didnt used #include<sstream>...it works fine


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
#include <iostream>

#include <string>
using namespace std;

int main() {

cout << "Find out what Gregory will do today. \n";

string weekday;


cout << "What day of the week is it? \n";
cin >> weekday;

if(weekday == "wednesday") {
cout << "its wednesday";
}

else {
cout << "It's not Wednesday";
}

return 0;
}




i/p:wednesday, o/p:its wednesday.  anyother string goes to else part
Last edited on Oct 2, 2015 at 8:05am
Topic archived. No new replies allowed.