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


i want have program read text file 20 single characters, on separate lines. want have characters compared against preprogrammed letters in program output wrong ones , total wrong. have constructed program can manually enter letters , wanted, knowledge of using files within code limited. have read vectors keep simple first , hang of before try learn else. have tried set code resembles think should like.

i'm not getting errors now... how text file program? i've constructed code can't figure out last steps connect it. possible steer me in right direction. in advance. of forum learning more ever thought 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 number of correct answers int incorrectanswers = 0;    //accumulator 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 error if (infile.fail())  {     cerr << "error opening file" << endl;     exit(1); }  string s1; int count = 0;  // reads file 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 student's replies against correct answers (int = 0; < num_questions; i++) { if (answers1[i] == stu_answers1[i]) correctanswers++; else if (answers1[i] != stu_answers1[i]) { incorrectanswers++; wronganswers[j] = + 1; j++; } } //did pass or fail? if (correctanswers >= min_correct) { cout << "\nyou must have @ least 15 correct pass."; cout << "\nstudent passed exam\n\n"; } else { cout << "\nyou must have @ least 15 correct pass."; cout <<"\nstudent failed exam\n\n"; }  //display list of questions incorrectly answered. cout << "the list below shows question numbers of incorrectly";  cout << " answered questions.\n"; (int = 0; < num_questions; i++) { if (wronganswers[i] != 0) cout << "question # " << wronganswers[i] << " incorrect." << endl; }  //display number of correct , incorrect answers provided student. cout << "\ncorrect answers = " << correctanswers << endl;  cout << "incorrect answers = " << incorrectanswers << endl; } 

the text file notepad .txt file named "key" following:

c d a c d c c d b c d d c a d 

i think can directly read in characters. i've tested code below. may have try.

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

count still number of student answers.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -