C++ Who want's to be a millionare
Go to solution
Solved by Unimportant,
1 hour ago, truecrafting said:<snip>
There's another gotcha that you overlooked. What happens when the user enters multiple characters as an answer to a question? (try it).
You need to clear the input stream after each input operation to flush any additional nonsense the user may have typed.
Also note that `system("pause")` is not portable.
Modified example:
#include <iostream> #include <stdlib.h> #include <limits> #include <cctype> using namespace std; void CheckAnswer(char correctAnswer) { while (1) //endless loop. { char answer; std::cin >> answer; /* Clear the cin input stream! */ cin.ignore(numeric_limits<streamsize>::max(), '\n'); answer = toupper(answer); if (answer == correctAnswer) { std::cout << "Correct!\n"; break; //break from endless loop. } else { std::cout << "Wrong!\n"; } } } main() { /* place the introduction in a scope of it's own, this limits the scope of std::string name so it does not pollute the entire main function. */ { //INTRODUCTION cout<<"Enter player name: "; string name; cin>>name; cout<<"WELCOME " <<name<< " TO WHO WANTS TO BE A MILLIONARE"<<endl; cout<<"INTRODCING YOU'RE HOST C++"<<endl<<endl; system("pause"); cout<<"RULES"<<endl; cout<<"1.ANSWER IN ALL CAPS"<<endl; cout<<"2.YOU HAVE 3 LIFELINES CALL A FRIEND,50-50 and skip question"<<endl; cout<<"3.NO CHEATING"<<endl <<endl <<endl; cout<<"WITHOUT FURTHER A DO WHO WANT TO BE A MILLIONARE?"<<endl; system("pause"); } //QUESION //question 1 cout<<"Who is the Current Chancellor of Germany"<<endl; cout<<"A-Wilhem II"<<endl; cout<<"B-Angela Merkel"<<endl; cout<<"C-Franz Hamburg"<<endl; cout<<"D-Hanz Zimmerman"<<endl; CheckAnswer('B'); //QUESTION 2 cout<<"Who is the competitor Wolfgang Amadeus Mozart?"<<endl; cout<<"A-Antonio Sallieri"<<endl; cout<<"B-Ludwig Beethoven"<<endl; cout<<"C-Johann Pachelbel"<<endl; cout<<"D-Johann Sallieri"<<endl; CheckAnswer('A'); /*...etc...*/ system("pause"); }
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now