This program is supposed to read in student's answers from a file, calculate correct and incorrect, display them, also display the student's name. The program will run through put all of the stuff is messed up. Problem with loops? Arrays? I don't know, that's why I am here. I have sought help from my teacher and she only confused me more. She added the part: "the read pointer is at eol. You need to swallow it. You need to consider this below also." I have no idea what that is supposed to mean, so I probably didn't do it right.
-----------------------------------------------------------------------
#include <iostream.h>
#include <stdio.h> //for getchar()
#include <conio.h> //for clrscr()
#include <fstream.h> //contains prototypes and
//implementations for file access
int main()
{
char ch;
int counter1=0;
int counter2=0;
int correctcount=0;
int incorrectcount=0;
int keycounter=0;
int anscounter=0;
int namecount=0;
char answer[15], name[15], key[15];
ifstream file1;
char filename[30]; // Used to hold the input file name
cout << "What input file should be opened? ";
cin.getline (filename, 30, '\n'); //gets the file to open
file1.open(filename); // Try to open that file
while (!file1) // If "not found" try again
{ cout << "That file can't be found. ";
cout << "Re-enter the filename -- >";
cin.getline (filename, 30, '\n'); // Get new filename
file1.open(filename); // Try to open it
}
while (counter1<15) //will execute untill key is loaded
{
file1>>key[counter1]; //loads the answer key array
counter1++;
}
//clrscr();
//the read pointer is at eol. You need to swallow it
//You need to consider this below also.
char eol;
file1.get(eol);
while (keycounter<15) //outputs answer key
{
cout<<key[keycounter];
keycounter++;
}
//runs to here
while (file1)
{
while (anscounter<15) //load ans array
{
file1>>answer[anscounter]; //loads the ans array
anscounter++;
}
cout <<'\t'<<'\t'<<answer;
anscounter=0;
keycounter=0;
for (counter2=0;counter2<15;counter2++)
{
if(key[keycounter]==answer[anscounter])
correctcount++;
else
incorrectcount++;
keycounter++;
anscounter++;
}
cout<<"Incorrect: ";
cout<<incorrectcount<<'\t';
cout<<"Correct: ";
cout<<correctcount<<'\t';//output
incorrectcount=0;
correctcount=0;
keycounter=0;
anscounter=0;
//ch=getchar();
file1.get(eol);
cout<<"Name: ";
while (ch != '\n' && namecount < 15) //load name array
{
file1>>name[namecount]; //loads the name array
namecount++;
}
namecount=0;
while (namecount<15)
{
cout<<name[namecount];
namecount++;
}
file1.get(eol);
}
ch=getchar();
return 0;
}
-------------------------------------------------------------
//The input file:
//This isn't the file she will be using to grade this, so my program must be robust
TTFTTTFFTFTFTTF
TTTFTTFFTFTFTTFEvelyn Adams
TTFTTTTTFFTFTTFJosephine Talbot
TTFTTTFFTFTFTTFJoseph Beatty
TTFTTTFFTFTFTTFKenneth Timmins
TTFTTTFFTFTFTTFLisa Pearson
TTFTTFTTFTTFTTFJacqueline Matesi
TTTFTTFFTFTFTFFDavid Wright
TTTTTFFTTFFFTFTJack McQuead
TTTFFTFTFFTTFTTDonald Crnkovic
-----------------------------------------------------------------------
#include <iostream.h>
#include <stdio.h> //for getchar()
#include <conio.h> //for clrscr()
#include <fstream.h> //contains prototypes and
//implementations for file access
int main()
{
char ch;
int counter1=0;
int counter2=0;
int correctcount=0;
int incorrectcount=0;
int keycounter=0;
int anscounter=0;
int namecount=0;
char answer[15], name[15], key[15];
ifstream file1;
char filename[30]; // Used to hold the input file name
cout << "What input file should be opened? ";
cin.getline (filename, 30, '\n'); //gets the file to open
file1.open(filename); // Try to open that file
while (!file1) // If "not found" try again
{ cout << "That file can't be found. ";
cout << "Re-enter the filename -- >";
cin.getline (filename, 30, '\n'); // Get new filename
file1.open(filename); // Try to open it
}
while (counter1<15) //will execute untill key is loaded
{
file1>>key[counter1]; //loads the answer key array
counter1++;
}
//clrscr();
//the read pointer is at eol. You need to swallow it
//You need to consider this below also.
char eol;
file1.get(eol);
while (keycounter<15) //outputs answer key
{
cout<<key[keycounter];
keycounter++;
}
//runs to here
while (file1)
{
while (anscounter<15) //load ans array
{
file1>>answer[anscounter]; //loads the ans array
anscounter++;
}
cout <<'\t'<<'\t'<<answer;
anscounter=0;
keycounter=0;
for (counter2=0;counter2<15;counter2++)
{
if(key[keycounter]==answer[anscounter])
correctcount++;
else
incorrectcount++;
keycounter++;
anscounter++;
}
cout<<"Incorrect: ";
cout<<incorrectcount<<'\t';
cout<<"Correct: ";
cout<<correctcount<<'\t';//output
incorrectcount=0;
correctcount=0;
keycounter=0;
anscounter=0;
//ch=getchar();
file1.get(eol);
cout<<"Name: ";
while (ch != '\n' && namecount < 15) //load name array
{
file1>>name[namecount]; //loads the name array
namecount++;
}
namecount=0;
while (namecount<15)
{
cout<<name[namecount];
namecount++;
}
file1.get(eol);
}
ch=getchar();
return 0;
}
-------------------------------------------------------------
//The input file:
//This isn't the file she will be using to grade this, so my program must be robust
TTFTTTFFTFTFTTF
TTTFTTFFTFTFTTFEvelyn Adams
TTFTTTTTFFTFTTFJosephine Talbot
TTFTTTFFTFTFTTFJoseph Beatty
TTFTTTFFTFTFTTFKenneth Timmins
TTFTTTFFTFTFTTFLisa Pearson
TTFTTFTTFTTFTTFJacqueline Matesi
TTTFTTFFTFTFTFFDavid Wright
TTTTTFFTTFFFTFTJack McQuead
TTTFFTFTFFTTFTTDonald Crnkovic