C++----比较两个数组中的元素并显示不相似的值

C++---- Comparing the the elements in the 2 arrays and displaying what are the values that are not similar

我需要:
1. 向用户询问测试答案并存储在数组中,然后将答案与正确答案进行比较。
2. 将正确答案的个数作为参数,将用户输入的错误和正确答案显示出来。
3. 显示他们的测试结果(通过或失败)。 15/20 是及格分数。
4. 只接受小写或大写的 A,B,C,D 作为答案。

到目前为止,这就是我所做的。但我不知道如何比较每个 elements/value 数组并显示错误回答和正确回答的问题。另外,我怎样才能接受 仅 A、B、C、D(无论用户输入 是大写还是小写, 两者, --应该可以接受) 字母 ?

请帮忙!非常感谢您的回答:)

 #include <iostream>
#include < iomanip>
#include <string>
using namespace std;

const int NUM_QUESTIONS = 20;
char correctAnsBig[NUM_QUESTIONS] = {   'B', 'D', 'A', 'A', 'C', 
                                    'A', 'B', 'A', 'C', 'D',
                                    'B', 'C', 'D', 'A', 'D',
                                    'C', 'C', 'D', 'D', 'A'};

char correctAnsSmall[NUM_QUESTIONS] = { 'b', 'd', 'a', 'a', 'c',
                                    'a', 'b', 'a', 'c', 'd',
                                    'b', 'c', 'd', 'a', 'd',
                                    'c', 'c', 'd', 'd', 'a' };
char ans[NUM_QUESTIONS];

bool ansEqual = true;
int count = 0;

void inputAnswers(char input[], int size)
{
   for (int cnt = 0; cnt < NUM_QUESTIONS; cnt++)
   {
       cout << "Enter your answer for #" << (cnt + 1) << " (A, B, C, or D): ";
       cin >> &ans[cnt];
   }

}

void checkAnswers()
{
   int count = 0;
   while (ansEqual && count < NUM_QUESTIONS)
   {
       if (ans[NUM_QUESTIONS] != correctAnsBig[NUM_QUESTIONS])
           ansEqual = false;
        count++;
   }

   if (ansEqual)
       cout << "Congratulations! You got it all right!\n";
   else
       cout << "You're a dumbass motherf*cker! A dishonor to your famiru!\n";
}

 void rightAns()
 {
    cout << "The correct answers are: \n";
    for (int cnt = 0; cnt < NUM_QUESTIONS; cnt++)
 {
       cout << (cnt + 1) << "." << "\t";
       cout << correctAnsBig[cnt] << endl;
 }
}

int main()
{

   inputAnswers(ans, NUM_QUESTIONS);
   rightAns();

   system("pause");
}

只需使用规范表示:

if (toupper(ans[count]) != correctAnsBig[count])

您需要了解以下几点: - 数组将通过引用传递,所以你不需要 &ans[cnt] - 用户输入可以是小写或大写。不要紧。你的函数内部有什么问题,你将如何比较它们。您可以在比较之前将用户输入转换为 lower 或 upper。 - 如果不需要,尽量不要使用全局变量。在这里,要跟踪正确答案的数量,您可以使用局部变量来完成,只需将其传递给函数即可。

#include <iostream>
#include <string>

using namespace std;

const int NUM_QUESTIONS = 20;
char correctAnsBig[NUM_QUESTIONS] = { 'B', 'D', 'A', 'A', 'C',
'A', 'B', 'A', 'C', 'D',
'B', 'C', 'D', 'A', 'D',
'C', 'C', 'D', 'D', 'A' };

char ans[NUM_QUESTIONS];

// check for user input
bool ValidateInput(char input)
{
    input = toupper(input);
    return (input == 'A' || input == 'B' || input == 'C' || input == 'D');
}

void inputAnswers(char input[], int size)
{
    for (int cnt = 0; cnt < NUM_QUESTIONS; cnt++)
    {
        bool valid = false;

        do
        {
            cout << "Enter your answer for #" << (cnt + 1) << " (A, B, C, or D): ";
            cin >> ans[cnt];

            valid = ValidateInput(ans[cnt]);
        } while (!valid);       
    }
}

int checkAnswers()
{
    int count = 0;

    for (int i = 0; i < NUM_QUESTIONS; i++)
    {
        // first, you need to convert all the character in the array to uppercase
        // then you will compare them
        // if they are equal, increase count of correct answer by 1
        // otherwise, do nothing
        if (toupper(ans[i]) == toupper(correctAnsBig[i]))
            count++;
    }
    return count;
}

void rightAns()
{
    cout << "The correct answers are: \n";
    for (int cnt = 0; cnt < NUM_QUESTIONS; cnt++)
    {
        cout << (cnt + 1) << "." << "\t";
        cout << correctAnsBig[cnt] << endl;
    }
}

int main()
{
    int numOfCorrectAnswer = 0;

    inputAnswers(ans, NUM_QUESTIONS);
    rightAns();
    numOfCorrectAnswer = checkAnswers();

    cout << "You got " << numOfCorrectAnswer << " corrects" << endl;
    if (numOfCorrectAnswer == 20)
        cout << "You got it all right" << endl;
    else if (numOfCorrectAnswer > 15)
        cout << "You passed" << endl;
    else
        cout << "You failed" << endl;
}