关于数组和类的数学错误

Mathematical error concerning arrays and classes

本文关键字:错误 于数组 数组      更新时间:2023-10-16

我的代码大部分都在工作(除了数学问题),我只是不知道我的数学哪里出了问题。它为我提供了第一组数据的正确值,但第二组数据还差得很远。这是我的头文件

#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 answert" << "Correct answert" << endl;
    for (int i = 0; i< 20; i++)
    {
        cout << endl;
        cout << ans[i] << "ttt" << correct_ans[i] << "ttt";
        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;
}

这是我的测试仪(包含main)。我的老师提供了这个,所以这里什么都不应该改变。

// 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;
}

您需要在grade()方法开始时将correct重置为0。