Hi yall,

Absolute C++ newbie here.

I would like to know how to prompt a user to enter yes or no, and then display a different msg depending on whether y or n was pressed. I am trying to set char y=1 or true and char n=0 or false. But then I am stuck at how to compare the input variable to y and n. Right now, this bad code only makes the program display the "yes" message no matter what key is pressed. Can anyone please gimme some tips? Thank you very much.

#include <iostream>
using namespace std;
void main ()
{char y,
n,
x;
y=1;
n=0;

cout<<"\nPlease enter either y or n\n";
cin>>x;
if (x=1)
cout <<"you pressed yes\n";
else
cout <<"you pressed no\n";
}

Please read this thread about how to post code inside code tags so that spaces and tabs are preserved. There are also other helpful threads for newbes at the top of this board that contain links to other useful information.

Thank you, and welcome to DaniWeb:)

Okay... Well... >.<

Seems like you just started learning a few days... maybe one or two? Unless you're learning by yourself. Heh. I'm no pro (I just started a few weeks ago) but if you're following a class, I reccommend learning by them until you feel comftorable enough to surpass what is being taught... Anyway...

do something like
char choice;
cout << "Please enter either Y or N.";
cin >> choice;
if (choice=='Y')
cout << "\nYou entered Yes.";
if (choice=="N')
cout << "\nYou entered No.";
return 0;
}
But then again, this is very basic, and if you want, you can go google up things like iostream/c++ guide... its best if you have a textbook to follow by, and then search the internet for when you can't find it from reference.

I am taking a university C++ class, but this is my very first attempt to write a program on my own.

Thanks again. That makes sense. I didn't realize I could set the IF condition to make the input equal to a char. Thought it had to be an int value.

If conditions can be any valid boolean expression (IE one which evaluates as either TRUE or FALSE).
Fixed up the code a bit, and added a third option:

#include <iostream>
using namespace std;
int main (){
    char y,
    n,
    x;
    y=1;
    n=0;

    char choice;
    cout << "Please enter either Y or N.";
    cin >> choice;
    if (choice=='Y')
       cout << "\nYou entered Yes.";
    else if (choice=='N')
         cout << "\nYou entered No.";
    else
        cout << "\nYou did not enter a valid character.";
    return 0;
    }

I am taking a university C++ class, but this is my very first attempt to write a program on my own.

Thanks again. That makes sense. I didn't realize I could set the IF condition to make the input equal to a char. Thought it had to be an int value.

In C and C++ the char data type is an int -- it is a one-byte integer. There really is no such thing as a true character data type, the characters you see on the screen are represented internally as integers, for example the letter 'A' has a numeric value of 65. You can find out the values of all of them in an ascii chart..

Another question. Is it possible to put multiple conditional statements in a WHILE loop? For example, if the user enters anything but n, N, y, or Y, I want the program to prompt him to re-type his choice until he enters one of those 4 values, how can I do so using just one loop?

I tried:

while (choice!='y' || choice!='Y' || choice!='n' || choice!='N')
{
cout << "Try again\n";

cin >> choice;
}

This clearly doesn't work.

I know it's a laughable mistake, but how can I fix it?

Thank you again.

Something like

int main( void )
{
    char choice[2] = {'\0'} ;
    do
    {
        fgets( choice, 2, stdin ) ;
        choice[0] = tolower(choice[0]) ;
    }
    while ( choice[0] != 'y' && choice[0] != 'n' ) ;

    return 0 ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.