从文本文件中读取内容并将其作为程序输入使用

4
我想让程序读取一个包含20个单字符的文本文件,每个字符都在独立的一行。然后我想把这些字符与程序中预设的字母进行比较,从而输出不匹配的字符和总不匹配数。我已经成功地构建了手动输入字母的程序,它可以按照我的意愿执行,但是我对代码内部使用文件的知识有限。我已经阅读了关于向量的资料,但我想先保持简单,掌握基础知识,然后再尝试学习其他东西。我尝试设置了一些代码,看起来应该是正确的,但我无法弄清楚连接的最后几个步骤。请问有没有人能够指导我朝正确的方向前进呢?谢谢!借助这个论坛的帮助,我正在学到比我想象中更多的东西。
#include <iostream>
#include <conio.h>
#include <cctype>
#include <fstream>
#include <string>

using namespace std;

void input(char[], int); //Function prototype
void checkAnswers(char[], char[], int, int);

int main()
{

const int NUM_QUESTIONS = 20;
const int MIN_CORRECT = 15;
int correctAnswers = 0;  //Accumulator for number of correct answers
int incorrectAnswers = 0;    //Accumulator for number of incorrect answers
char answers[NUM_QUESTIONS] = { 'B', 'D', 'A', 'A', 'C',
'A', 'B', 'A', 'C', 'D',
'B', 'C', 'D', 'A', 'D',
'C', 'C', 'B', 'D', 'A'};

char stu_answers[NUM_QUESTIONS];

ifstream infile;
infile.open("key.txt");

//Check for Error
if (infile.fail()) 
{
    cerr << "Error Opening File" << endl;
    exit(1);
}

string s1;
int count = 0;

// Reads file to the end
while (!infile.eof()) {
    infile >> s1 >> stu_answers[NUM_QUESTIONS]; 
    count++;
}
cout << count << " Students Answers" << endl;


checkAnswers(answers, stu_answers, NUM_QUESTIONS, MIN_CORRECT);
system ("pause");
 return 0;
}

void checkAnswers(char answers1[], char stu_answers1[], int NUM_QUESTIONS, int
MIN_CORRECT)
{
cout << "max: " << NUM_QUESTIONS;
int correctAnswers = 0; 
int incorrectAnswers = 0;
int wrongAnswers[]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int j = 0;

//Check the student's replies against the correct answers
for (int i = 0; i < NUM_QUESTIONS; i++)
{
if (answers1[i] == stu_answers1[i])
correctAnswers++;
else if (answers1[i] != stu_answers1[i])
{
incorrectAnswers++;
wrongAnswers[j] = i + 1;
j++;
}
}
//Did they pass or fail?
if (correctAnswers >= MIN_CORRECT)
{
cout << "\nYou must have at least 15 correct to pass.";
cout << "\nStudent passed the exam\n\n";
}
else
{
cout << "\nYou must have at least 15 correct to pass.";
cout <<"\nStudent failed the exam\n\n";
}

//Display a list of the questions that were incorrectly answered.
cout << "The list below shows the question numbers of the incorrectly"; 
cout << " answered questions.\n";
for (int i = 0; i < NUM_QUESTIONS; i++)
{
if (wrongAnswers[i] != 0)
cout << "Question # " << wrongAnswers[i] << " is incorrect." << endl;
}

//Display the number of correct and incorrect answers provided by the student.
cout << "\nCorrect Answers = " << correctAnswers << endl; 
cout << "Incorrect Answers = " << incorrectAnswers << endl;
}

这个文本文件只是一个名为“key”的记事本.txt文件,其中包含以下内容:

C
D
A
A
C
A
D
C
C
D
B
C
D
A
D
C
A
A
D
A

2
虽然我不必提交,但这是我们课程所使用的书籍内容。我正在参加一门在线课程,下周我们将有一个类似的项目问题。 - WillyJoe
当你逐步执行代码时,调试器会告诉你什么?如果你不能解释实际问题是什么,我们就无法帮助你解决它。我们不会为了弄清楚你在问什么而调试你的代码;你必须自己做到这一点,然后针对问题本身提出具体的问题。提示:你正在将单独的行读入单独的数组元素中,因此你需要逐个读取每行并将其放入相应的数组元素中。这将需要一个循环和计数器(例如,在现有的 while!infile.eof() 循环中的 count 中,该计数器是数组索引)。 - Ken White
谢谢,那正是我正在寻找的方向指引。 - WillyJoe
2个回答

1
你可以使用std::getline函数逐行获取数据,它的用法如下:
std::string strLine;
int nCount = 0;
while (std::getline(infile, strLine))
{
    stu_answers[nCount++] = strLine.at(0);
}

然后,nCount将是答案数字。

1

我认为你可以直接读取字符。我已经测试了下面的代码。你可以试试。

while (!infile.eof() && count < NUM_QUESTIONS) {
    infile >> stu_answers[count++]; 
}

"count仍然是学生答案的数量。"

那就是我一直在寻找的缺失之处。谢谢你抽出时间来查看并帮助一个困难的新手。 - WillyJoe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接