Soru
Q5) (QUIZ H3) (25 pts.) Assume that you have two files given : answer.txt and key.txt files.answer.txt file includes student number (int)and the answers of the student. The number of students are not known. The file should be read until the end of file, key.txt file includes the answer key for 100 questions. Write a complete C++program that reads the exam answers of the student from the file answer.txt, and then compare these answers with the correct answers in key.txt. Finally display the number of correct answers for each student. Hint: You may use 2 strings (char [] ) to store the student number and the answers. answer.txt : 9999 abdaa __ 8888 bcdcd __ key.txt: abbacaca __ The program output may be displayed as follows: stno : 9999 correct answers : 55 stno : 8888 correct answers : 72
Çözüm
4.2166 Voting
Fikri
Gelişmiş · 1 yıl öğretmeniUzman doğrulaması
Cevap
a complete C++ program that reads the exam answers of the student from the file answer.txt, and then compares these answers with the correct answers in key.txt. Finally, it displays the number of correct answers for each student:<br />```cpp<br />#include <iostream><br />#include <fstream><br />#include <string><br />using namespace std;<br /><br />int main() {<br /> string stno, answer;<br /> int correct = 0;<br /> ifstream answerFile("answer.txt");<br /> ifstream keyFile("key.txt");<br /> if (!answerFile.is_open() ||!keyFile.is_open()) {<br /> cout << "Error: Could not open files." << endl;<br /> return 1;<br /> }<br /><br /> while (answerFile >> stno >> answer) {<br /> keyFile.clear();<br /> keyFile.seekg(0);<br /> while (keyFile >> stno) {<br /> if (stno == stno) {<br /> if (answer == stno) {<br /> correct++;<br /> }<br /> }<br /> }<br /> cout << "stno: " << stno << " correct answers: " << correct << endl;<br /> correct = 0;<br /> }<br /><br /> answerFile.close();<br /> keyFile.close();<br /> return 0;<br />}<br />```<br />This program first opens the answer.txt and key.txt files using the ifstream class. It then reads the student number and answer from the answer.txt file and compares it with the correct answer from the key.txt file. If the answer is correct, it increments the correct variable. Finally, it displays the student number and the number of correct answers.
Derecelendirmek için tıklayın: