| | |
C++ Baby steps
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2006
Posts: 3
Reputation:
Solved Threads: 0
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>
usingnamespace 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";
}
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>
usingnamespace 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
Thank you, and welcome to DaniWeb
•
•
Join Date: Oct 2006
Posts: 3
Reputation:
Solved Threads: 0
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.
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.
Last edited by Harkua; Oct 12th, 2006 at 10:34 pm.
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:
Fixed up the code a bit, and added a third option:
C++ Syntax (Toggle Plain Text)
#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; }
Please anyone, correct me if I am wrong. It is the best way for me to learn.
•
•
•
•
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.
•
•
Join Date: Oct 2006
Posts: 3
Reputation:
Solved Threads: 0
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:
This clearly doesn't work.
I know it's a laughable mistake, but how can I fix it?
Thank you again.
I tried:
C++ Syntax (Toggle Plain Text)
while (choice!='y' || choice!='Y' || choice!='n' || choice!='N') { cout << "Try again\n"; cin >> choice; }
I know it's a laughable mistake, but how can I fix it?
Thank you again.
Something like
C++ Syntax (Toggle Plain Text)
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 ; }
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- How can fix the problem? (JavaScript / DHTML / AJAX)
- Error comparing strings from arrays (PHP)
- how long does it take to learn assembler and how difficult is it? (Assembly)
- cgi (HTML and CSS)
- General advice wanted (Growing an Online Community)
- Long Island Guide (Website Reviews)
- Please help: Troj Stlien.A virus plus.... (Viruses, Spyware and other Nasties)
- need help installing mytop (*nix Software)
Other Threads in the C++ Forum
- Previous Thread: Problem with execv argument passing
- Next Thread: serial port programming between two sytems
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






