簡體   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