in C++. Please correct the following program so it works as the following: The p
ID: 3591678 • Letter: I
Question
in C++. Please correct the following program so it works as the following:
The program is reading the text file and displaying correctly, It just needs to know which line says correct, make that line == answer and compare the useranswer to the real answer. Right now the answer checking is not working.
in C++, create a program that reads a text file (with the content and format as shown below, MC stands for multiple choice and separates questions between them):
MC
How many digits does a binary systems use
1
incorrect
2
correct
3
incorrect
4
incorrect
MC
What is the binary value for 5
010
incorrect
100
incorrect
011
incorrect
101
correct
MC
What is the binary value for 7
010
incorrect
101
incorrect
111
correct
011
incorrect
MC
What is the binary value for 2
010
correct
000
incorrect
101
incorrect
111
incorrect
For each question read, you should create a nice display as follows:
Question: How many digits does a binary systems use
Answers:
a) 1
b) 2
c) 3
d) 4
Enter your answer:
After the user enters the answer, you should give feedback such as:
your answer is correct/incorrect
Correct answer is: …
The program should keep doing this as long as there are questions in the file.
Source Code:
# include <iostream>
# include <fstream>
using namespace std;
int main()
{
const int size = 50;
char mc[size];
char Question[ size ];
char a[ size ];
char ar[ size ];
char b[ size ];
char br[ size ];
char c[ size ];
char cr[ size ];
char d [ size ];
char dr[ size ];
char answer[size];
char useranswer[size];
ifstream infile;
infile.open("Question.txt");
while ( infile )
{
if (infile == false)
cout<<"Could not open the input file"<<endl;
// read another record
infile.getline( mc, size );
infile.getline( Question, size );
infile.getline( a, size );
infile.getline( ar, size );
infile.getline( b, size );
infile.getline( br, size );
infile.getline( c, size );
infile.getline( cr, size );
infile.getline( d, size );
infile.getline( dr, size );
// //
//if(ar == ("correct")){
//answer="a)";
//else if(br==("correct")==0)
//answer="b)";
//else if(cr==("correct")==0)
//answer="c)";
//else if(dr==("correct")==0)
//answer="d)";}
// display
cout << "Question: " << Question << endl <<
"Answers: " <<endl <<
"a)" << a << endl <<
"b)" << b << endl <<
"c)"<< c << endl <<
"d)"<< d << endl << endl;
cout << "Enter your answer: " << endl;
cin>> useranswer;
if (useranswer[size] == answer[size])
cout << "Your answer is correct: " << endl;
else
cout << "Your answer is not correct, correct answer is: " << answer << endl;
}// end while
// system ("pause");
}
Current Ouput:
Desired Output:
Thank You
Question: How many digits does a binary systems use Answers: Enter your answer: b) Your answer is correct: Question: What is the binary value for 5 Answers: a) 010 b) 100 c) 011 d) 101 Enter your answer: Your answer is correct: Question: What is the binary value for 7 Answers: a) 010 b) 101 c) 111 d) 011 Enter your answer: 3 Your answer is correct: Question: What is the binary value for 2 Answers: a) 010 b) 000 c) 101 d) 111Explanation / Answer
Use method strcmp(str1, str2) method to compare 2 strings i.e., actual answer and use entered answer.
{
print: Correct answer
}
else
{
print: Incorrect Answer and show the correct answer.
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.