数组比较(.Txt文件到用户输入)C ++

时间:2015-04-29 18:01:51

标签: c++ arrays

这是一个程序,用于将答题纸(.txt文件)与用户输入进行比较。意思是我正在比较两个数组,但我有一个PITA时间使它工作。它符合正常但它只是不将用户输入数组与.txt文件数组进行比较并将所有内容都计为错误,即使我在用户输入端手动输入正确答案也是如此。任何建议或意见将不胜感激! PS:我不能使用矢量,只能使用数组。这不是个人选择,而是一项要求。

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    const int CHOICE = 20;
    const char KEY = 20;
    char correctAnswers[KEY];
    char A, B, C, D;
    char userAnswers[CHOICE];
    char rightOrWrong[CHOICE];
    int totalMissed;
    int sum = 0;
    int count = 0;

    ifstream inputFile;

    inputFile.open("CorrectAnswers.txt");

    while (count < KEY && inputFile >> correctAnswers[count])
        count++;

    inputFile.close();

    for (int qst = 0; qst < 20; qst++)
    {
        cout << "Please put an answer for the question: " << endl;
        cin >> userAnswers[qst];
        cout << endl;

        if (userAnswers[qst] == correctAnswers[qst])
        {
            rightOrWrong[qst] = 'C';
        }
        else
        {
            rightOrWrong[qst] = 'I';
        }
    }

    for (int qst = 0; qst < 20; qst++)
    {
        if (rightOrWrong[qst] == 'C')
        {
            sum += 1;
        }
        else
        {
            cout << "Answer #" << qst << " is not correct" << endl;
        }
    }

    totalMissed = 20 - sum;
    cout << "This is your final score: " << endl;
    cout << "You missed " << (20 - sum) << "/20 of the questions" << endl;
    cout << "You overall percentage is " << (sum / 20) << "%." << endl;

    if (sum < 14 && sum >= 0)
    {
        cout << "You have not passed the exam. You must have a 70% or higher to pass. Study harder next time!" << endl;
    }
    else
    {
        cout << "Pat yourself on the back! You've passed the test!" << endl;
    }

    return 0;
}

如果这看起来很糟糕,我很抱歉,它通常看起来不像这样。

0 个答案:

没有答案