简体   繁体   中英

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

I want to have a program read a text file with 20 single characters, that are on separate lines. I want to then have those characters compared against preprogrammed letters in the program where it will output the wrong ones and the total wrong. I have successfully constructed the program where I can manually enter the letters and it will do as I wanted, but my knowledge of using files within the code is limited. I have read about vectors but I would like to keep it simple first and get the hang of it before I try to learn something else. I have tried to set up a some code that resembles what I think it should probably look like.

I'm not getting any errors now... How do I get the text from the file to the program? I've constructed a somewhat code that almost does it but can't figure out the last steps to connect it. Is it possible for someone to help steer me in the right direction. Thanks in advance. With the help of this forum I am learning much more than I ever thought was possible.

#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;
}

The text file is just a notepad .txt file named "key" with the following:

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

You can use std::getline function to get one line at a time. It looks like this:

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

Then nCount will be the answer number.

I think you can directly read in the characters. I've tested the code below. You may have a try.

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

count is still the number of student answers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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