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!!!
// just for fun and lulz btw
#include <iostream>
#include <string>
#include <math.h>
usingnamespace 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;}
elseif(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;
}
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.
#include <iostream>
#include <string>
usingnamespace 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;
}