What can I add to the previous C++ program that we\'ve been working on in the \"
ID: 3625262 • Letter: W
Question
What can I add to the previous C++ program that we've been working on in the "ASK" function and score calculation in INT main that will default to the next player if the first player gets the question wrong?
For example, if player 1 buzzes in first, he gets to answer the question. If he gets it wrong, the question point value is subtracted from his score THEN the other player is given the chance to answer and gain or lose points based on the answer. Whoever is correct is next to pick from the question board. If both are wrong, then the last person to have a right answer is next to pick from the question board. Just lilke Jeopardy.
This should be it. I so appreciate the help!
Explanation / Answer
please rate - thanks
I think I got it.--there are changes in ask and main
#include<iostream>
#include<fstream>
#include<string >
#include<iomanip>
using namespace std;
void winner(string p[],short s[])
{cout<<"AND THE WINNER IS ";
if(s[0]>s[1])
cout<<p[0]<<" with "<<s[0]<<" points ";
else if(s[0]<s[1])
cout<<p[1]<<" with "<<s[1]<<" points ";
else
cout<<"amazingly a tie. Both players have "<<s[0]<<" points ";
}
void letters(string QL[][5],char filename[]) // This adds all the letters to the matrix. u can also use a this to add all the questions to their matrix and answers to theirs.
{
ifstream fin (filename, ios::in);
for(short i=0;i<5;i++)
{
for(short j=0;j<5;j++)
{
fin >> QL[i][j];
}
}
fin.close();
fin.clear();
}
void sentence(string QL[][5],char filename[]) // This adds all the letters to the matrix. u can also use a this to add all the questions to their matrix and answers to theirs.
{
ifstream fin (filename, ios::in);
for(short i=0;i<5;i++)
{
for(short j=0;j<5;j++)
{
getline(fin, QL[i][j]);
}
}
fin.close();
fin.clear();
}
void points(string Q[][5]) // This adds all the points to the matrix. u can also use a this to add all the questions to their matrix and answers to theirs.
{ string p;
ifstream fin ("points.txt", ios::in);
for(short i=0;i<5;i++)
{ fin>>p;
for(short j=0;j<5;j++)
{
Q[i][j]=p;
}
}
fin.close();
fin.clear();
}
void ask(string q,string a,int &knows,string p[],short S[],int pts )
{
string ans;
char c;
int prev=knows;
cout<<q<<endl<<p[0]<<" enter 0, "<<p[1]<<" enter 1 to indicate you know the answer: ";
cin>>c;
knows=c-'0';
while(knows<0||knows>1)
{cin>>c;
knows=c-'0';
}
while ((getchar()) != ' ');
cout<<"OK "<<p[knows]<<" what is the answer: ";
getline(cin,ans);
if(a.compare(ans)==0)
{
cout<<"That is correct ";
S[knows]+=pts;
}
else
{
cout<<"That is incorrect ";
S[knows]-=pts;
if(knows==0)
knows=1;
else
knows=0;
cout<<p[knows]<<" do you know the answer enter 0 if no, enter 1 if yes: ";
cin>>c;
while(c!='0'&&c!='1')
cin>>c;
while ((getchar()) != ' ');
if(c=='1')
{cout<<"Enter your answer: ";
getline(cin,ans);
if(a.compare(ans)==0)
{
cout<<"That is correct ";
S[knows]+=pts;
}
else
{
cout<<"That is incorrect ";
S[knows]-=pts;
knows=prev;
}
}
else
knows=prev;
}
for(int i=0;i<400000000;i++);
}
int main()
{string Q[5][5],QL[5][5];//QL is being used to store the letters this way we can compare the position of the letter in the matrix to the position of the question in the Q matrix.
string ans[5][5],quest[5][5];
string a, b;
int turn=0,count=25;
string P[2]; // player names
short S[2]={0,0}; // score variables
int i,j;
char l;
letters(QL,"letters.txt");
points(Q);
sentence(ans,"answers.txt");
sentence(quest,"questions.txt");
cout << "Welcome to Jeopardy!" << endl;
cout << "Enter the name for Player 1: " << endl;
cin >> P[0];
cout << "Enter the name for Player 2: " << endl;
cin >> P[1];
system("cls");
cout << "Welcome " << P[0] << " and " << P[1] << "! Let's play Jeopardy!" << endl;
for(i=0;i<800000000;i++);
system("cls");
do{
system("cls");
cout << "|===========================================================| ";
cout << "| C++ | Sports | Celebrity | Music | Movies | ";
cout << "|===========|===========|===========|===========|===========| ";
for(i=0;i<5;i++)
{for(j=0;j<5;j++)
{cout<<"| "<<Q[i][j];
if(QL[i][j].compare(" ")==0)
cout<<" "<<QL[i][j]<<" ";
else
cout<<" ("<<QL[i][j]<<") ";
}
cout<<"| ";
if(i!=4)
cout << "|-----------|-----------|-----------|-----------|-----------| ";
}
cout << "|===========|===========|===========|===========|===========| ";
for(i=0;i<2;i++)
{cout << P[i]<<" Score: " << S[i];
if(i==0)
cout<<" |||||| | ";
else
cout<<"| ";
}
do
{
cout << P[turn]<<" Please choose a letter from the board ";
cin>>l;
l=tolower(l);
while(l<'a'||l>'y')
{cout << P[turn]<<" Please choose a letter from the board ";
cin>>l;
l=tolower(l);
}
i=(l-'a')/5;
j=(l-'a')%5;
}while(QL[i][j].compare(" ")==0);
ask(quest[i][j],ans[i][j],turn,P,S,(i+1)*100);
Q[i][j]=" ";
QL[i][j]=" ";
}while(--count>0);
winner(P,S);
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.