Hello, I need help with this program when I press Y,y,s,w,N,n or any letter it always print "OK". I don't know what I'm doing wrong. Please help!
Write a program using if/then/else structure which prints the question “Do you want to continue?” and reads a user input. If the user input is “Y” for Yes, “y” for yes, “s” for sure, or “w” for why not, print “OKAY”. If the user input is “N or n” then print “terminating”, otherwise, print “Bad input”.
#include <iostream.h>
#include <iomanip.h>
int main()
{
int ans;
cout<<"Do you want to continue?\n";
cout<<"\n'Y or y' for yes\n'N or n' for no\n's' for sure\n'w' for why not\n";
cin>>ans;
#include <iostream.h>
#include <iomanip.h>
int main()
{
char ans; //use char not int unless your going to test its ascii value
cout<<"Do you want to continue?\n";
cout<<"\n'Y or y' for yes\n'N or n' for no\n's' for sure\n'w' for why not\n";
cin >> ans;
if(ans == 'Y' || ans == 'y') //do not complicate your logical operators use OR instead of trying to test for what is its not
{
cout<<"OKAY";
}
elseif(ans == 's')
{
cout<<"OKAY";
}
elseif(ans == 'w')
{
cout<<"OKAY";
}
elseif (ans == 'N' || ans == 'n') //same
{
cout<<"Terminating";
}
else
{
cout<<"Bad input";
}
system("pause");
return 0;
}