*Need some C++ help*

cbrsurfr

Golden Member
Jul 15, 2000
1,686
1
81
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 << &quot;What input file should be opened? &quot;;
cin.getline (filename, 30, '\n'); //gets the file to open
file1.open(filename); // Try to open that file

while (!file1) // If &quot;not found&quot; try again
{ cout << &quot;That file can't be found. &quot;;
cout << &quot;Re-enter the filename -- >&quot;;
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<<&quot;Incorrect: &quot;;
cout<<incorrectcount<<'\t';
cout<<&quot;Correct: &quot;;
cout<<correctcount<<'\t';//output

incorrectcount=0;
correctcount=0;
keycounter=0;
anscounter=0;

//ch=getchar();
file1.get(eol);
cout<<&quot;Name: &quot;;
while (ch != '\n' &amp;&amp; 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

 

cbrsurfr

Golden Member
Jul 15, 2000
1,686
1
81
My teacher also said:&quot;I suggest you use getline (name,30,'\n') to read the names.&quot; How would I incorporate something like that, instead of the while loop I have?
 

cbrsurfr

Golden Member
Jul 15, 2000
1,686
1
81
It is supposed to output: # correct, # incorrect and student name. It outputs that but its messed up like,
correct: 1 Incorrect: 14 Name: TTFTJoh
nSmithTTTF.... The output is totally messed up. I have been told it is a problem with my name array. I guess I am not supposed to use an array and instead use getline? I don't know. I am about ready to just give up on it.
 

sciencewhiz

Diamond Member
Jun 30, 2000
5,885
8
81
You have two problems that I can see.

Like you teacher says, it is easier to use getline, if you also use the string class. To do this you need to #include <string.h> and then replace your name array with a variable declared as a string type. Then you can use the getline to get the name, after you have read the answers.

This brings up your bigger problem. The way that you have your code now (unless I am reading it wrong) is you are trying to read in all the answers and then all the names. This will not work because the files are read sequentially (unless you want to learn to love the ignore function).

There are two things that you can do, depending on what your teacher likes in terms of speed, memory requirments etc.

The first is to read in all the answers for one student, calculate the number correct, and get the name and output everything for that one line.

The second is to read the answers into one array and the names into another array and the do all the calculations and output at one time.

Hope this helps
 

cbrsurfr

Golden Member
Jul 15, 2000
1,686
1
81
Well I went to see my teacher today. The way I had my code was probably different from above. The one main problem was that she told everyone to use getline (name, 30, '\n') Everyone had name arrays of only size 15 (which the problem only wanted us to read in names up to 15 chars long) which created the bad output. Also by using getline I don't have to use a loop to output the name, I can just cout<<name

Thanks for your help.