Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a file with information that will feed a guessing game. For a guessing ga

ID: 3849662 • Letter: C

Question

Create a file with information that will feed a guessing game.

For a guessing game, you will build a file with fragments of questions and matching answers. For example:

animal dog

color red

food chocolate

country Greece

Your program will read each pair of words from the file, then use the first word to make a question and use the second word as the correct answer. For example, your program could print something like this on the screen:

I'm thinking of an animal. Guess what animal it is...

Then match the user's answer with dog. If the user guesses the answer that is recorded in the file (dog), print a message saying the answer was right, otherwise, say the answer was wrong. Keep track of how many right and wrong answers.

Create a file with 10-15 pairs.

Explanation / Answer

The name of the file containing fragments of questions and answers should be "test.txt" (without quotes).

Following is the code that accomplishes the task.

Hope it helps. Thanks...

#include<iostream>
#include<string>
#include<fstream>
#include<stack>
using namespace std;
int main()
{
stack <string> s1,s2;
string word , ans, fname = "test.txt";
int flag=1, correct=0,incorrect=0;
ifstream in(fname);
if(!in)
{
cout<<"File Does not Exist";
return 0;
}
cout<<" ";
while(in >> word)
{
if(flag)
{
s1.push(word);
flag=0;
}
else
{
s2.push(word);
flag=1;
}
}
while(!s2.empty())
{
cout<<"I'm thinking of an " <<s1.top()<<" Guess what "<<s1.top()<<" it is...";
cin>>ans;
if(ans==s2.top())
{
cout<<"The answer was correct"<<endl;
correct++;
}
else
{
cout<<"The answer was Incorrect"<<endl;
incorrect++;
}
s1.pop();
s2.pop();
}
cout<<"Correct answers = "<<correct<<endl;
cout<<"Inorrect answers = "<<incorrect<<endl;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote