Advertisement

I have a question about if statements.

Started by April 14, 2002 03:59 PM
2 comments, last by mx1 22 years, 5 months ago
I am trying to make a little program just for educational purposes. A program that asks the user a question then the user enters the answer and if the user is correct it will say correct and go to the next question, if the user is not correct it will say incorrect try again. I am trying to do it with a bunch of if and else statements but I can''t figure out how to be able to enter words for the correct or incorrect answer. I have been able to enter numbers though, for example, I want to be able to ask a question like, waht is the largest state in the USA? then if they enter Alaska it will print correct if they enter anything else it will print incorrect try again. Can someone write me some example code? I cant figure out how to do it with words only numbers. Example I can do this, int StatesInTheUSA; cout << "How many states are in the USA?\n"; cin >> StatesInTheUSA; if ( StatesInTheUSA== 50) cout << "Correct\n"; else cout << "Incorrect\n"; And that works because the user enters a number (50) but I want to ask for names, places, states etc. not numbers. Someone please help me out.
For C style strings:
http://www.cplusplus.com/doc/tutorial/tut3-2.html

For C++ style strings search for a STL reference and then find the string class in it.

Advertisement

    #include <iostream>#include <string>int main(){  using namespace std;  string reply;  cout << "What's my name?"  getline( cin, reply );  if( reply == "Snoop Doggy Dogg" )    cout << "Hell, yeah biatch!" << endl;  else    cout << "Wr-zzong, sucka, now git g-zzone!" << endl;  return 0;}    


[Edit: Perfected my West Coat LBC sl-zzang, for r-zzeal!]

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions | Internet Acronyms ]
Thanks to Kylotan for the idea!

[edited by - Oluseyi on April 14, 2002 5:09:32 PM]
LOL, thanks for the replies.
I knew it was something like that but I couldn''t quite figure it out on my own.
Thanks again.

This topic is closed to new replies.

Advertisement