Do you want to continue? if/else ---HELP

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;

if(ans !='Y'&& ans != 'y')
{
cout<<"OK\n";
}

else if(ans =='s')
{
cout<<"OKAY\n";
}

else if(ans !='w')
{
cout<<"OKAY\n";
}
else if (ans !='N' && ans != 'n')
{
cout<<"Terminating\n";
}
else
{
cout<<"Bad input";
}

return 0;
}
First off "int ans;" accepts an integer, you want "char ans;."
Last edited on
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
#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";
    }
    else if(ans == 's')
    {
        cout<<"OKAY";
    }
    else if(ans == 'w')
    {
        cout<<"OKAY";
    }
    else if (ans == 'N' || ans == 'n') //same
    {
        cout<<"Terminating";
    }
    else
    {
        cout<<"Bad input";
    }
    
    system("pause");
    return 0;
} 
Thanks so much!!! I don't know what I was thinking!
Topic archived. No new replies allowed.