Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
truecrafting

C++ Who want's to be a millionare

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");
}

 

Recommended Posts

Posted · Original PosterOP

So I have a bit of a dilemma I am currently trying to make a Who wants to be a millionaire on c++ and the problem is how do I loop it or what loop should I use so i can repeat the question if they answered the question wrong

also how do i make it so that i can have lifelines I am still looking but too no avail

 

 

 

#include<iostream>
#include<stdlib.h>

using namespace std;

main()
{


//input
 
char answer;
string name;
int x;





//INTRODUCTION
cout<<"Enter player 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;
cin>>answer;
if (answer=='B'||answer=='b'){
cout<<"Correct"<<endl<<endl;
}
else{
cout<<"Wrong!"<<endl<<endl;
}




//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;
cin>>answer;
if (answer=='a'||answer=='A'){
cout<<"Correct"<<endl<<endl;
}
else{
cout<<"Wrong!"<<endl<<endl;
}








//QUESTION 3
cout<<"In Computer Hardware what does GPU mean?"<<endl;
cout<<"A-Game Processing Unit"<<endl;
cout<<"B-Gamma Processing Unit"<<endl;
cout<<"C-Grayscale Processing Unit"<<endl;
cout<<"D-Graphics Processing Unit"<<endl;
cin>>answer;
if (answer=='d'||answer=='D'){
cout<<"Correct"<<endl<<endl;
}
else{
cout<<"Wrong!"<<endl<<endl;
}

//QUESTION 4
cout<<"In the 2018 Oscar's What Movie won BEST PICTURE'"<<endl;
cout<<"A-The Favourite"<<endl;
cout<<"B-Bohemian Rhapsody"<<endl;
cout<<"C-Green Book"<<endl;
cout<<"D-Shape Of Water"<<endl;
cin>>answer;
if (answer=='C'||answer=='c'){
cout<<"Correct"<<endl<<endl;
}
else{
cout<<"Wrong!"<<endl<<endl;
}


//QUESTION 5
cout<<"As of now what airline is the oldest"<<endl;
cout<<"A-Qantas"<<endl;
cout<<"B-Delta"<<endl;
cout<<"C-Avianca"<<endl;
cout<<"D-KLM"<<endl;
cin>>answer;
if (answer=='D'||answer=='d'){
cout<<"Correct"<<endl<<endl;
}
else{
cout<<"Wrong!"<<endl<<endl;
}

//QUESTION 6
 
  
cout<<"Currently What Car is the fastest production car"<<endl;
cout<<"A-Koenisegg Regera"<<endl;
cout<<"B-Buggati Chiron"<<endl;
cout<<"C-Mclaren Senna"<<endl;
cout<<"D-Buggati Veyron"<<endl;
cin>>answer;
if (answer=='b'||answer=='B'){
cout<<"Correct"<<endl<<endl;
}
else{
cout<<"Wrong!"<<endl<<endl;
}











system("pause");
}

 

 

replies much appreciated 

Link to post
Share on other sites

for example

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;
cin>>answer;
while (answer!='B'||answer!='b'){
cout<<"Wrong! try again"<<endl;
cin>>answer;
}
cout<<"Correct"<<endl;

 

Link to post
Share on other sites

first of all why not use a database or Array to store the questions?

If you just want to know if the answer was wrong make a array of byte and use three states: not asked, wrong answer, right answer

Link to post
Share on other sites
Posted · Original PosterOP
1 hour ago, Technomancer__ said:

for example


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;
cin>>answer;
while (answer!='B'||answer!='b'){
cout<<"Wrong! try again"<<endl;
cin>>answer;
}
cout<<"Correct"<<endl;

 

yes i would like to  attach smthn that will ask the question again if it is wrong

 

Link to post
Share on other sites
Posted · Original PosterOP
1 hour ago, James Evens said:

first of all why not use a database or Array to store the questions?

If you just want to know if the answer was wrong make a array of byte and use three states: not asked, wrong answer, right answer

for simplicity i just decided to not add databases that have more chances of not working

 

Link to post
Share on other sites
2 minutes ago, truecrafting said:

for simplicity i just decided to not add databases that have more chances of not working

Array of struct makes your life easier.

Fill the array and then just loop through it in the way you want.

Link to post
Share on other sites
Posted · Original PosterOP
1 hour ago, James Evens said:

Array of struct makes your life easier.

Fill the array and then just loop through it in the way you want.

can you give me an example so i can try to mimic it 

Link to post
Share on other sites
Posted · Best Answer
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");
}

 

Link to post
Share on other sites
Posted · Original PosterOP
17 hours ago, Unimportant said:

How do you envision this lifeline, what should it look like ?

simple you input something like 50-50,Skip and Call a friend (for this it's a simple cout)

Link to post
Share on other sites
2 hours ago, truecrafting said:

simple you input something like 50-50,Skip and Call a friend (for this it's a simple cout)

You're going to have to switch to a more automated approach or it'll get messy quick.

What's your level? Do you know classes, std::vector, ...?

Link to post
Share on other sites
Posted · Original PosterOP
1 hour ago, Unimportant said:

You're going to have to switch to a more automated approach or it'll get messy quick.

What's your level? Do you know classes, std::vector, ...?

im just i would say a beginner to average I just attend my mandatory computer classes at my school

 

Link to post
Share on other sites
Posted · Original PosterOP
On 3/14/2019 at 6:39 PM, Unimportant said:

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");
}

 

another question how do i set a limit for only 3 overall mistakes and goes ahead and stops the program when it reaches 3 mistakes in the program

 

Link to post
Share on other sites
23 minutes ago, truecrafting said:

another question how do i set a limit for only 3 overall mistakes and goes ahead and stops the program when it reaches 3 mistakes in the program

As already mentioned, you're going to have to redesign the entire program to a more automated design in order to facilitate your lifelines. Such a design could also easily facilitate a maximum number of wrong answers.

 

Your current linear design is at it's limits, you can't keep retrofitting a poor design without it becoming a mess.

 

This problem is not as simple as it looks, I could whip up an example but it would quickly include advanced language features for which I think you're not ready.

Link to post
Share on other sites
Posted · Original PosterOP
1 hour ago, Unimportant said:

As already mentioned, you're going to have to redesign the entire program to a more automated design in order to facilitate your lifelines. Such a design could also easily facilitate a maximum number of wrong answers.

 

Your current linear design is at it's limits, you can't keep retrofitting a poor design without it becoming a mess.

 

This problem is not as simple as it looks, I could whip up an example but it would quickly include advanced language features for which I think you're not ready.

If it's not a burden on you can you show me because i also want to further or rxpand my knowledge on c++

Link to post
Share on other sites
22 hours ago, truecrafting said:

If it's not a burden on you can you show me because i also want to further or rxpand my knowledge on c++

Something along these lines, could be made more pretty but that would require going pretty deep.

#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <random>
#include <limits>
#include <cctype>

class Question
{
public:

    /* Template factory function where template parameter is the correct answer index.
     * This, in combination with private constructor, makes the possibility of a invalid
     * correct answer index a compile time problem. */
    template <int correctAnswer>
    static Question
    Make(std::string text, std::string answer1, std::string answer2,
                 std::string answer3, std::string answer4)
    {
        static_assert(correctAnswer >= 0 && correctAnswer <= 3, "Invalid correctAnswer!");
        return Question(std::move(text), {answer1, answer2, answer3, answer4}, correctAnswer);
    }

    const std::string&
    GetText() const
    {
        return mText;
    }

    const std::vector<std::string>&
    GetAnswers() const
    {
        return mAnswers;
    }

    bool
    CheckAnswer(int answer) const
    {
        return answer == mCorrectAnswer;
    }

    /* Randomly removes 2 answers while updating the correct answer index.
     * Makes sure the correct answer is not removed.*/
    void
    CullAnswers()
    {
        if (!mIsCulled)
        {
            mIsCulled = true;

            std::random_device rd;
            std::uniform_int_distribution<> dist(0, 3);
            while (int(mAnswers.size()) > 2)
            {
                RemoveAnswer(dist(rd));
            }
        }
    }

private:

    /* Private constructor, force usage of Make function.*/
    Question(std::string text, std::vector<std::string> answers, int correctAnswer) :
        mText(std::move(text)),
        mAnswers(std::move(answers)),
        mCorrectAnswer(correctAnswer)
    {}

    /* Removes the answer at the given index. Does nothing if index is out of range
     * or index is the correct answer. Updates the correct answer index. */
    void
    RemoveAnswer(int index)
    {
        if (index >= int(mAnswers.size()) || index == mCorrectAnswer)
        {
            return;
        }
        if (index < mCorrectAnswer)
        {
            --mCorrectAnswer;
        }
        mAnswers.erase(mAnswers.begin() + index);
    }

    std::string
    mText;

    std::vector<std::string>
    mAnswers;

    int
    mCorrectAnswer;

    bool
    mIsCulled = false;
};

void
PrintQuestion(const Question& question)
{
    std::cout << question.GetText() << '\n';
    char letter = 'A';
    for (const auto& answer : question.GetAnswers())
    {
        std::cout << letter++ << " - " << answer << '\n';
    }
}

void
PrintLifeLines(bool skipAvailable, bool fiftyFiftyAvailable)
{
    if (skipAvailable)
    {
        std::cout << "1 - Skip Question\n";
    }
    if (fiftyFiftyAvailable)
    {
        std::cout << "2 - 50/50\n";
    }
}

char
GetUserInput()
{
    char userInput;
    std::cin >> userInput;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    return toupper(userInput);
}

int
main()
{
    const std::vector<Question> questions =
    {
        Question::Make<1>("Who is the Current Chancellor of Germany?",
                          "Wilhem II",
                          "Angela Merkel",
                          "Franz Hamburg",
                          "Hanz Zimmerman"),
        Question::Make<0>("Who is the competitor Wolfgang Amadeus Mozart?",
                          "Antonio Sallieri",
                          "Ludwig Beethoven",
                          "Johann Pachelbel",
                          "Johann Sallieri")
        /* ...etc... */
    };

    bool skipAvailable = true;
    bool fiftyFiftyAvailable = true;
    int numWrongAnswers = 0;

    for (auto currentQuestion : questions)
    {
        while (1)
        {
            PrintQuestion(currentQuestion);
            PrintLifeLines(skipAvailable, fiftyFiftyAvailable);
            const auto userInput = GetUserInput();

            if (userInput == '1' && skipAvailable)
            {
                skipAvailable = false;
                break;
            }

            if (userInput == '2' && fiftyFiftyAvailable)
            {
                fiftyFiftyAvailable = false;
                currentQuestion.CullAnswers();
            }
            else
            {
                if (currentQuestion.CheckAnswer(userInput - 'A'))
                {
                    std::cout << "Correct!\n";
                    break;
                }
                else
                {
                    std::cout << "Wrong!\n";
                    if (++numWrongAnswers >= 3)
                    {
                        std::cout << "3 wrong answers given!\n";
                        return 0;
                    }
                }
            }
        }
    }

    return 0;
}

 

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×