繁体   English   中英

从文本文件中读取,然后在程序中使用with

[英]Reading from a text file and then using the input with in the program

我想让一个程序读取包含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

您可以使用std::getline函数一次获取一行。 看起来像这样:

std::string strLine;
int nCount = 0;
while (std::getline(infile, strLine))
{
    stu_answers[nCount++] = strLine.at(0);
}

然后, nCount将为答案编号。

我认为您可以直接阅读字符。 我已经测试了下面的代码。 您可以尝试一下。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM