First time using if and else, please help?

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";
	 }
 }
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
I deleted it and it says expected ';' before '{' token?
Can you post your updated code?
sorry, updated my answer. haven't really been doing much c++ lately (i'm learning python.)
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
you're welcome.
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";
		 }
		 }
		 
	 }
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.