我的代码大部分工作(除了数学问题),我只是不知道我的数学出错了。它为第一组数据提供了正确的值,但第二组却没有。 这是我的头文件
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class TestGrader
{
private:
char correct_ans[20];
int correct = 0;
public:
void setCorrect_answers(char cans[]);
void grade(char ans[]);
};
这是我的.cpp文件
#include "TestGrader.h"
using namespace std;
/*******************************************************
* setCorrect_answers *
* This member function receives a 20-character string *
* and copies its information into the answers array. *
*******************************************************/
void TestGrader::setCorrect_answers(char cans[])
{
for (int i = 0; i< 20 ; i++)
correct_ans[i] = cans[i];
}
/*******************************************************
* grade *
* The grade function receives a 20-character array *
* holding the test taker's answers and compares each *
* of their answers to the correct one. The function *
* then calculates and displays the results. *
*******************************************************/
void TestGrader::grade(char ans[])
{
cout << "User answer\t" << "Correct answer\t" << endl;
for (int i = 0; i< 20; i++)
{
cout << endl;
cout << ans[i] << "\t\t\t" << correct_ans[i] << "\t\t\t";
if (ans[i] == correct_ans[i])
{
correct++;
}
else
cout << "X";
}
cout << "\n";
if (correct > 15)
cout << "Congrats! You have passed the exam" << endl;
else
cout << "You have failed the exam. Better luck next time!" << endl;
cout << "Number of correct answers: " << correct << endl;
cout << "Number of wrong answers: " << 20 - correct << endl;
}
这是我的测试人员(包含主要部分)。我的老师提供了这个,所以不应该在这里改变任何东西。
// Chapter 8 - Programming Challenge 11, Driver's License Exam
// This program utilizes a TestGrader class to grade the written portion of a
// driver's license exam. The class grade function compares the applicant's
// answers, stored in one array, to the correct answers, stored in another array.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
#include "TestGrader.h"
// ***************** Class tester ***************
int main()
{
const int NUM_QUESTIONS = 20;
int correct = 0;
// Create a TestGrader object & set its key with correct answers
char correct_ans[] = { 'B', 'D', 'A', 'A', 'C',
'A', 'B', 'A', 'C', 'D',
'B', 'C', 'D', 'A', 'D',
'C', 'C', 'B', 'D', 'A' };
// Array to hold test taker's answers
char user1_ans[20] = { 'A', 'D', 'A', 'A', 'C',
'A', 'B', 'A', 'C', 'D',
'A', 'C', 'D', 'A', 'D',
'A', 'C', 'B', 'D', 'A' };
char user2_ans[20] = { 'A', 'D', 'D', 'A', 'C',
'A', 'B', 'D', 'C', 'D',
'A', 'C', 'D', 'C', 'C',
'A', 'A', 'B', 'D', 'A' };
TestGrader DMVexam;
DMVexam.setCorrect_answers(correct_ans);
//------------- Grade grade User 1's answers ---------
cout << "------------ User # 1 -------------\n";
DMVexam.grade(user1_ans);
//------------- Grade grade User 2's answers ---------
cout << "\n------------ User # 2 -------------\n";
DMVexam.grade(user2_ans);
system("pause");
return 0;
}
答案 0 :(得分:1)
您需要在correct
方法的开头将grade()
重置为0.