help with if statements and strings

Hi! I'm relatively new to programming and I have been practicing some basic concepts. this is a short conversation I made. on line 18, I'm having trouble with the or statement. for example, I could enter "fruit" and it will still run even though "fruit" isn't one of the options. second, I thought: what if someone enters "cherry pie" instead of just "cherry"? can I have a line that removes the word "pie" from string "a" if it is entered into the answer? thanks!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// just for fun and lulz btw

#include <iostream>
#include <string>
#include <math.h>

using namespace std;
string a;
int main(){
    cout<<"hello world!!!" "\ndo you like pie?";
    redopie:
    getline(cin, );
    if(a=="yes"){cout<<"what is your favorite kind? "; goto piedone;}
    else if(a=="no"){cout<<"you don't like pie?!?!?!?!?!\ni'm terminating this process until you do!!! >:( "; goto redopie;}
    else cout<<"yes or no answer please "; goto redopie;
    piedone:
    getline(cin, a); 
    if(a=="cherry" or "apple" or "pumpkin"){cout<<"I LOVE "<<a<<" pie!!!!!";}//for some reason, any answer in my "if" will run the "cout"
    else cout<<"i've never heard of "<<a<<"pie";
    return 0;
}
try

1
2
3
4
5
cin >> a;
if (a=="cherry" || a=="apple" || a=="pumpkin") 
     cout<<"I LOVE "<<a<<" pie!!!!!";
else
     cout <<"I have never heard of "<<a<<" pie";


Use the double pipe || operator as another way to say "or", and you must redefine the condition fully (a == " ... "), after each double pipe.

As for the user entering "cherry pie", using the 'cin' standard input stream should only accept a word until it reaches a space, then stops, opposed to using getline, which would accept multiple words in the string.

However, I'm not sure on this.
Last edited on
both of your tips worked, thank you!!!
Let me tell you you should also avoid using goto's, use whiles instead.
could you give me an example of how to use while instead of goto?
Last edited on
EssGeEich is right, goto leads to practically unreadable code. Here is a while loop that will control the flow of the program neatly

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

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

string a;
string makePie = "false";
int main() {
while(makePie=="false") {
		cout<<"hello world!!!" "\ndo you like pie?";
		cin >> a;
	if(a=="yes")
	{
		cout<<"what is your favorite kind? " << endl;
		makePie = "true";
	}
    else if(a=="no")
	{
		cout << endl;
		cout<<"you don't like pie?!?!?!?!?!\ni'm terminating this process until you do!!! >:( " << endl;
		cout << endl;
		exit (EXIT_SUCCESS);
	}
    else
	{
		cout << endl;
		cout<<"yes or no answer please " << endl;
		cout << endl;
		makePie = "false";
	}
}

	cin >> a;
	if (a=="cherry" || a=="apple" || a=="pumpkin") 
		cout<<"I LOVE "<<a<<" pie!!!!!"<<endl;
	else
		cout <<"I have never heard of "<<a<<" pie"<<endl;
	return 0;
}
Last edited on
o.k. got it, thanks!!!
Here's my take on it (thought I'd use it as practice since I'm also new)

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

using namespace std;

int main()
{
	string a;

	cout<<"Hello world!\n";

	while(a != "y")
	{
		cout<<"Do you like pie (y/n)? ";
		cin>>a;
		if (a == "n")	cout<<"I'm not terminating this process until you like pie! >:(\n"<<endl;

	}
	cout<<"What is your favorite kind, cherry, apple or pumpkin? ";
	cin>>a;

	if(a == "cherry" || a == "apple" || a == "pumpkin")
	{
		cout<<"\nI love "<<a<<" pie!"<<endl;
	}
	else cout<<"\nI haven't heard of "<<a<<" pie :("<<endl;

	cin.ignore();
	cin.get();
	return 0;
}
Topic archived. No new replies allowed.