关于数组和类的数学错误
Mathematical error concerning arrays and classes
我的代码大部分都在工作(除了数学问题),我只是不知道我的数学哪里出了问题。它为我提供了第一组数据的正确值,但第二组数据还差得很远。这是我的头文件
#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。
相关文章:
- 错误:字符数组的初始值设定项太多
- 错误:数组下标的类型"int[int]"无效
- 将二进制操作员应用于数组中的每个元素
- 最小的正乘数,当应用于数组时,使数组成为积分
- 错误:数组大小表达式必须具有整型或无作用域枚举类型,而不是'double'
- 更改了错误的数组变量
- 将函数应用于数组向量的元素
- std::堆栈和堆栈溢出错误的数组分配
- 指向成员的指针的语法糖适用于数组,但不适用于 std::vector
- C++ 错误错误:数组下标的类型"双精度 [10][10][double]"无效
- 错误:数组下标的类型'int[int]'无效
- 错误:数组下标'double*[double]'的类型无效
- 将功能应用于数组中的每个元素
- C++输入错误的数组
- 在代码块错误中收到此错误:数组下标'main()::fields[int]'的类型无效
- 如何将函数应用于数组中的所有元素(在C++模板类中)
- 获取错误的数组值
- 错误:"数组":符号不明确
- 显示结构/类错误的数组C++
- 错误 C2075:数组初始化需要大括号