problem with program

hello, I'm having problem with my input file and using the while loop. I'm suppose to read in the the input file then use a while loop to read in the input file into array counter. but when i start without debugging, I'm suppose to type in the file name but i get garbage when i do.

this is what my entire program looks like

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

void readFile1(char[],int);
void readFile(char[], int);
void compareAnswers(char[], char[], int, int &);
void displayResults(int, int);

int main()
{
const int question=20;
int questionMissed;
char instructorAnswer[20];
char studentAnswer[20];

readFile(instructorAnswer,question);
readFile1(studentAnswer,question);
compareAnswers(instructorAnswer,studentAnswer,question,questionMissed );
displayResults(questionMissed, question);
return 0;
}
void readFile1(char studentAnswer[], int question)
{
char filename[51];
cout<<"enter file"<<endl;
cin>>filename;
int counter=0;
ifstream inputFile;
inputFile.open(filename);

while(counter<question && inputFile)
{
inputFile >> studentAnswer;
counter++;
}
inputFile.clear();
inputFile.close();



}
void readFile(char instructorAnswer[], int question)
{

char filename[51];
cout<<"enter file"<<endl;
cin>>filename;
int counter=0;
ifstream inputFile;
inputFile.open(filename);

while(counter<question && inputFile )
{
inputFile >> instructorAnswer;
counter++;
}
inputFile.clear();
inputFile.close();

}

void compareAnswers(char instructorAnswer[], char studentAnswer[], int question, int & questionMissed )
{
questionMissed=0;

cout<<"checking the Answers"<<endl;
for(int index=0; index<20; index++)
{
if(studentAnswer[index] != instructorAnswer[index])
questionMissed++;
}
cout<<"student"<<studentAnswer<<endl;
cout<<"teacher"<<instructorAnswer<<endl;



}
void displayResults(int questionMissed, int question)
{
int correctAnswers;
double percentage;

cout<<"the number of correct answers"<<question-questionMissed<<endl;
correctAnswers=question-questionMissed;
cout<<"the percentage"<<correctAnswers/question<<endl;
percentage=correctAnswers / question;
cout<<"missed question 4. student answer:d corect answer:c"<<endl;
cout<<"missed question 12. student answer:a corect answer:d"<<endl;
cout<<"missed question 17. student answer:c corect answer:d"<<endl;
cout<<fixed<<showpoint<<setprecision(2)<<percentage<<endl;

if(percentage>70)
cout<<"student pass"<<endl;
else
cout<<"student faii"<<endl;
}
and this is the output I'm getting

enter file
c:\\datafiles\\studentanswers.txt
enter file
c:\\datafiles\\correctanswers.txt
checking the Answers
student╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
teacher╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
the number of correct answers20
the percentage1
missed question 4. student answer:d corect answer:c
missed question 12. student answer:a corect answer:d
missed question 17. student answer:c corect answer:d
1.00
student faii
Press any key to continue . . .

the while loop is where I think where I'm having the problem. I think i did the while loop wrong and its not reading the file

sorry if i didn't explain myself very well. I haven't been doing this for long
closed account (zwA4jE8b)
that symbol you are getting I believe is a uninitialized position in your char[].
 
void readFile1(char 'name of variable[]',int);


i believe that is correct.

parameters need names.
Last edited on
You really dont want to use >> to insert into a c-string, use getline()

Also, there is really no reason to have a readFile() and a readFile1(), they both do the exact same thing.

@CreativeMFS His declaration is OK, adding a name isn't required but can help readability.
Topic archived. No new replies allowed.