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.2
(166 Oylar)
Fikri
Gelişmiş · 1 yıl öğretmeni
Uzman 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:```cpp#include #include #include using namespace std;int main() { string stno, answer; int correct = 0; ifstream answerFile("answer.txt"); ifstream keyFile("key.txt"); if (!answerFile.is_open() ||!keyFile.is_open()) { cout > stno >> answer) { keyFile.clear(); keyFile.seekg(0); while (keyFile >> stno) { if (stno == stno) { if (answer == stno) { correct++; } } } cout << "stno: " << stno << " correct answers: " << correct << endl; correct = 0; } answerFile.close(); keyFile.close(); return 0;}```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.