所以我一直在努力解决这个问题。我的所有代码目前都可以工作,除了我的最后一个模块,我试图比较两个数组的差异并将问题编号记录到一个新数组中。我不确定如何设置它。任何帮助将不胜感激。
主要问题是我正在创建的数组不正确。我不知道如何设置一个随着输入值而变大的空白数组。除非我真的把这个概念搞砸了。当我尝试不同的事情时,我有当前注释掉的问题。
#include <iostream>
using namespace std;
const int COLS = 20;
void input_data(char [], int);
void compare_data(char [], int, char [], int);
int main()
{
char letters[COLS];
char answers[] = { 'A', 'D', 'B', 'B',
'C', 'B', 'A', 'B',
'C', 'D', 'A', 'C',
'D', 'B', 'D', 'C',
'C', 'A', 'D', 'B'};
input_data(letters, COLS);
compare_data(letters, COLS, answers, COLS);
}
void input_data(char letter[], int size)
{
cout << "Please enter the student's answers for each of the questions. \n";
cout << "Press Enter after typing each answer. \n";
cout << "Please enter only an A, B, C, or D for each question. \n";
for (int i = 1; i <= size; i++)
{
cout << "Question " << i << ": ";
cin >> letter[i - 1];
while (letter[i - 1] != 'A' &&
letter[i - 1] != 'B' &&
letter[i - 1] != 'C' &&
letter[i - 1] != 'D')
{
cout << "Please enter only A, B, C, or D \n";
cout << "Question " << i << ":";
cin >> letter[i - 1];
}
}
}
void compare_data(char letter[], int size, char answer[], int cols)
{
int ans_correct = 0;
int ans_wrong = 0;
//int incorrect[20];
for (int i = 1; i <= size; i++)
{
if (letter[i] == answer[i])
ans_correct += 1;
else
{
ans_wrong += 1;
//incorrect[i-1] = i;
}
}
if (ans_correct >= 15)
cout << "The student passed the exam. \n";
else
cout << "The student did not pass the exam. \n";
cout << "Correct Answers: " << ans_correct << endl;
cout << "Incorrect Answers: " << ans_wrong << endl << endl;
cout << "Questions that were answered incorrectly: \n";
for (int i = 1; i < size; i++)
{
//cout << incorrect[i-1] << endl;
}
}