I made a code that plays a guessing game w/ the user and guess the letter the user is guess in 5 tries or less but, my teacher wants it to were the user can input multiple replies like: y, Y, yes, YES. How would i do that
//Harjasdeep Samra //this program guesses a users letter in 5 of less tries #include<iostream> using namespace std; int main() { char first, mid, last, userResponse; bool answer, answer2=true; first='a'; last='z'; mid=(first+last)/2; cout<<"pick a letter and keep it in your head"<<endl; while(answer2) { cout<<"is your character "<<mid<<"? type y for yes and n for no"<<endl; cin>>userResponse; userResponse=tolower(userResponse); if(userResponse=='n') { answer=false; } else answer=true; if(answer) answer2=false; if(!answer) { cout<<"is your letter higer or lower. Put h for higher and L for lower"<<endl; cin>>userResponse; userResponse=tolower(userResponse); if(userResponse=='h') answer=false; else answer=true; if(answer)//this is for lower { last=mid-1; mid=(first+last)/2; } else//this if for higher { first=mid+1; mid=(first+last)/2; } } } cout<<"your letter is: "<<mid<<endl; return 0; }
you can use such condition if(userResponse=='n' || userResponse=='no') & if((userResponse=='y' || userResponse=='yes')).
try this with you code:- when you run you code:- "pick a letter and keep it in your head is your character m? type y for yes and n for no " give here response other than "yes" or "no" . what will happen? you should put one more condition to check that the responce was proper or not. and according to that print a message ,for example, "give proper response" and ask again for response. you got my point?
you can split your string and look for the character y in the user response...
Join our real-time social learning platform and learn together with your friends!