First time using if and else, please help?

Feb 25, 2014 at 11:16pm
I've made a program which when the 'if' and 'else' statements are used, here's the code...The output is almost working, when I input 'no' it displays 'bad' as it should, but when I input 'yes' it displays both Good and Bad? does anyone have any ideas? Which is a rhetorical question cause you guys are all probably aware of what the problem is unlike me;)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//random9

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

int main()
 {
 	string response;
 	cout << "Do you like me?\n";
 	cin >> response;
 	if (response == "yes") {
 	 cout << "Good.\n"; 
	 }	
	 else (response == "no");{
	 	cout << "Bad.\n";
	 }
 }
Feb 25, 2014 at 11:28pm
try using else if (response == "no") //and no semicolon on line 15. and also, you're leaving a hole in your program where if the user types something other than the expected text in, nothing happens. if you are going to check for multiple scenarios, it's a good idea to check for all of them.
Last edited on Feb 25, 2014 at 11:33pm
Feb 25, 2014 at 11:31pm
I deleted it and it says expected ';' before '{' token?
Feb 25, 2014 at 11:34pm
Can you post your updated code?
Feb 25, 2014 at 11:34pm
sorry, updated my answer. haven't really been doing much c++ lately (i'm learning python.)
Feb 25, 2014 at 11:35pm
You there, I like you...GENIUS! It worked, my thanks to you:) so using 'else if' means that if they type anything else in, what would be the answer?
Last edited on Feb 25, 2014 at 11:35pm
Feb 25, 2014 at 11:35pm
you're welcome.
Feb 25, 2014 at 11:45pm
Quick last question, I added this end to it but now whenever I take the semi colon from line 25, the output also puts both quotes in. When i input the "I don't know" without the semi colon, it does not output the goodbye on it's own, but when I put the semi colon back in, the "goodbye" displays as expected, but then when I input the quote "Carion" with the semi colon it displays both 'cout' quotes?...any ideas?

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
//random9

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

int main()
 {
 	string response;
 	cout << "Do you like me?\n";
 	cin >> response;
 	if (response == "yes") {
 	 cout << "Good.\n"; 
	 }	
	else if (response == "no") {
	 	cout << "Bad.\n";
	 }
	 {
		 string response;
		 cout << "What is my name?\n";
		 cin >> response;
		 if (response == "Carion") {
		 	cout << "Welcome to the program.\n";
		 }
		 else if (response == "I don't know") {
		 	cout << "Goodbye";
		 }
		 }
		 
	 }
Feb 26, 2014 at 10:44pm
the reason it's not working is because cin >> response only takes input up until the first space. what you should use is getline(cin, response) which takes in all the characters inputted, no matter how many spaces there are. so, on line 21, replace cin >> with getline(cin, response).
Topic archived. No new replies allowed.