Every time i compile this code i come up with a additional set of numbers, but i don't know why.

#include <iostream>
 #include <stdlib.h>
 #include <iomanip>
 #include <string>
 #include <fstream>
 
using namespace std;
 
 
 
void calculateAverage(double quiz1, double quiz2, double quiz3, double
test4, double& SAverage);
int calculateGrade(double grade);
 
int main()
{
 
string studentName;
int numberOfStudents = 0;
double classAverage = 0;
double SAverage = 0;
double totalAverage = 0; //To add the average of all student averages
char grade;
double quiz1, quiz2, quiz3, quiz4;
 
ifstream inFile; // input stream variable for the student file
ofstream outFile; // output stream variable

inFile.open("open.txt");
 
cout << setfill(' ') <<setiosflags(ios:: left) << setw(11) <<
"Student" << setw(10) << "Quiz1"
<< setw(8) << "Quiz2" << setw(8) << "Quiz3" << setw(8) << "Quiz4"
<< setw(8) << "Average" << setw(8) << "Grade" <<endl;
 
 
if (!inFile)
{
cout << "Unable to open this file." <<endl;
system("PAUSE");
return 1;
}
 
while(inFile)
{

inFile >> studentName >> quiz1 >> quiz2 >> quiz3 >> quiz4
;
 
calculateAverage(quiz1, quiz2, quiz3, quiz4,SAverage);
 
grade = calculateGrade(SAverage);
 
cout << setfill(' ') << setiosflags(ios::left) << setw(11) <<
studentName << setw (10) << quiz1
<< setw(8) << quiz2 << setw(8) << quiz3 << setw(8) << quiz4
<< setw(8) << SAverage << setw(8) <<
grade<<endl;
 
totalAverage = totalAverage + SAverage;
 
numberOfStudents++;
classAverage = totalAverage / numberOfStudents;
}
 
inFile.close();
 
system("PAUSE");
return 0;
}
 
 
 
//function to calculate the average
void calculateAverage(double quiz1, double quiz2, double quiz3, double
quiz4, double& SAverage)
{
 
SAverage = double (quiz1 + quiz2 + quiz3 + quiz4) / 5.0;
 
}
  
int calculateGrade(double SAverage)
{
char grade;
 
if (SAverage <= 100 && SAverage >= 90)
grade = 'A';
else if (SAverage < 90 && SAverage >= 80)
grade = 'B';
else if (SAverage < 80 && SAverage >= 70)
grade = 'C';
else if (SAverage < 70 && SAverage >= 60)
grade = 'D';
else if (SAverage < 60 && SAverage >= 0)
grade = 'F';
else
cout << "Invalid grade " << endl;
 
return grade;
 
}

This what i get when i debug.

Student Quiz1Quiz2 Quiz3 QuizAverage Grade
70 80 90 95 95 72 C
85 90 80 75 85 66 D
70 80 55 85 50 54 F
70 45 50 40 35 34 F
70 45 50 40 35 34 F
Press any key to continue . . .

Any help will be greatly appreciated .

i come up with a additional set of numbers, but i don't know why.

Line #44 needs rewriting

while(inFile)

Change it to ..

while(inFile >> studentName >> quiz1 >> quiz2 >> quiz3 >> quiz4)
{
  // rest of the code ...
}

Your while(inFile) is a variant to the theme of using .eof() in a loop control. Read Avoid Loop Control Using eof() courtesy of Dave Sinkula.

ok i dont get it rewrote line 44 now i only come up with two rows of numbers.

Student Quiz1 Quiz2 Quiz3 Quiz4 Average Grade
85 90 80 75 85 66 D
70 45 50 40 35 34 F
Press any key to continue . . .

i rewrote line 44 now not getting any numbers.

#include <iostream>
 #include <stdlib.h>
 #include <iomanip>
 #include <string>
 #include <fstream>
 
using namespace std;
 
 
 
void calculateAverage(double quiz1, double quiz2, double quiz3, double
test4, double& SAverage);
int calculateGrade(double grade);
 
int main()
{
 
string studentName;
int numberOfStudents = 0;
double classAverage = 0;
double SAverage = 0;
double totalAverage = 0; //To add the average of all student averages
char grade;
double quiz1, quiz2, quiz3, quiz4;
 
ifstream inFile; // input stream variable for the student file
ofstream outFile; // output stream variable

inFile.open("open.txt");
 
cout << setfill(' ') <<setiosflags(ios:: left) << setw(11) <<
"Student" << setw(10) << "Quiz1"
<< setw(8) << "Quiz2" << setw(8) << "Quiz3" << setw(8) << "Quiz4"
<< setw(8) << "Average" << setw(8) << "Grade" <<endl;
 
 
if (!inFile)
{
cout << "Unable to open this file." <<endl;
system("PAUSE");
return 1;
}
 
{
  std::ifstream file("file.txt");
  if ( file )
  {
     int i;
     while ( file >> i )
     {
        std::cout << i << ' ';
     }
     std::cout << std:: endl;
  }
  system("PAUSE");
  return 0;
}
{
 
inFile >> studentName >> quiz1 >> quiz2 >> quiz3 >> quiz4
;
 
calculateAverage(quiz1, quiz2, quiz3, quiz4,SAverage);
 
grade = calculateGrade(SAverage);
 
cout << setfill(' ') << setiosflags(ios::left) << setw(11) <<
studentName << setw (10) << quiz1
<< setw(8) << quiz2 << setw(8) << quiz3 << setw(8) << quiz4
<< setw(8) << SAverage << setw(8) <<
grade<<endl;
 
totalAverage = totalAverage + SAverage;
 
numberOfStudents++;
classAverage = totalAverage / numberOfStudents;
}
 
inFile.close();
 
system("PAUSE");
return 0;
}
 
 
 
//function to calculate the average
void calculateAverage(double quiz1, double quiz2, double quiz3, double
quiz4, double& SAverage)
{
 
SAverage = double (quiz1 + quiz2 + quiz3 + quiz4) / 5.0;
 
}
  
int calculateGrade(double SAverage)
{
char grade;
 
if (SAverage <= 100 && SAverage >= 90)
grade = 'A';
else if (SAverage < 90 && SAverage >= 80)
grade = 'B';
else if (SAverage < 80 && SAverage >= 70)
grade = 'C';
else if (SAverage < 70 && SAverage >= 60)
grade = 'D';
else if (SAverage < 60 && SAverage >= 0)
grade = 'F';
else
cout << "Invalid grade " << endl;
 
return grade;
 
}

please help.

i even change it to this but still getting additional numbers.

#include <iostream>
 #include <stdlib.h>
 #include <iomanip>
 #include <string>
 #include <fstream>
 
using namespace std;
 
 
 
void calculateAverage(double quiz1, double quiz2, double quiz3, double
test4, double& SAverage);
int calculateGrade(double grade);
 
int main()
{
 
string studentName;
int numberOfStudents = 0;
double classAverage = 0;
double SAverage = 0;
double totalAverage = 0; //To add the average of all student averages
char grade;
double quiz1, quiz2, quiz3, quiz4;
 
ifstream inFile; // input stream variable for the student file

inFile.open("open.txt");
 
cout << setfill(' ') <<setiosflags(ios:: left) << setw(11) <<
"Student" << setw(10) << "Quiz1"
<< setw(8) << "Quiz2" << setw(8) << "Quiz3" << setw(8) << "Quiz4"
<< setw(8) << "Average" << setw(8) << "Grade" <<endl;
 
 
if (!inFile)
{
cout << "Unable to open this file." <<endl;
system("PAUSE");
return 1;
}
 
while (inFile >> studentName >> quiz1 >> quiz2 >> quiz3 >> quiz4)
{
 
calculateAverage(quiz1, quiz2, quiz3, quiz4,SAverage);
 
grade = calculateGrade(SAverage);
 
cout << setfill(' ') << setiosflags(ios::left) << setw(11) <<
studentName << setw (10) << quiz1
<< setw(8) << quiz2 << setw(8) << quiz3 << setw(8) << quiz4
<< setw(8) << SAverage << setw(8) <<
grade<<endl;
 
totalAverage = totalAverage + SAverage;
 
numberOfStudents++;
classAverage = totalAverage / numberOfStudents;
}
 
inFile.close();
 
system("PAUSE");
return 0;
}
 
 
 
//function to calculate the average
void calculateAverage(double quiz1, double quiz2, double quiz3, double
quiz4, double& SAverage)
{
 
SAverage = double (quiz1 + quiz2 + quiz3 + quiz4) / 5.0;
 
}
  
int calculateGrade(double SAverage)
{
char grade;
 
if (SAverage <= 100 && SAverage >= 90)
grade = 'A';
else if (SAverage < 90 && SAverage >= 80)
grade = 'B';
else if (SAverage < 80 && SAverage >= 70)
grade = 'C';
else if (SAverage < 70 && SAverage >= 60)
grade = 'D';
else if (SAverage < 60 && SAverage >= 0)
grade = 'F';
else
cout << "Invalid grade " << endl;
 
return grade;
 
}

please help.

Line 44 is

{

What was it before you rewrite it?

i even change it to this but still getting additional numbers.

Changed what to what? Are we supposed to not only find your bug but also find your changes. And in this unformatted code that Salem already pointed out?

Format your code, ask intelligent questions, don't make us guess, and you'll probably get an answer in 1 post, rather than all this lack of understanding on our part and frustration on your part.

ok line 44 through 48 was originally

}
 
while(inFile)
{
inFile >> studentName >> quiz1 >> quiz2 >> quiz3 >> quiz4

when i compiled it,i come up with a additional of numbers.
Then changed it to

{
  std::ifstream file("open.txt");
  if ( file )
  {
     int i;
     while ( file >> i )
     {
        std::cout << i << ' ';
     }
     std::cout << std:: endl;
  }

but when i compile it i only come up with only two sets of numbers.
But i don't know why. Sorry for the confusing of the posts.

Look at the file and the lines displayed. Which lines are displayed?

It looks like you're reading one line, throwing it away, then reading the next line and using it.
This is repeated.
Then you get to the EOF.

ok
the numbers in the files are:
70 80 90 95 95 85 90 80 75 85 70 80 55 85 50 70 45 50 40 35

when i changed the code to

{
  std::ifstream file("open.txt");
  if ( file )
  {
     int i;
     while ( file >> i )
     {
        std::cout << i << ' ';
     }
     std::cout << std:: endl;
  }

i kept getting this:

Student Quiz1 Quiz2 Quiz3 Quiz4 Average Grade
85 90 80 75 85 66 D
70 45 50 40 35 34 F
Press any key to continue . . .

when i had this as the code:

}
 
while(inFile)
{
 
inFile >> studentName >> quiz1 >> quiz2 >> quiz3 >> quiz4
;

i was getting this
Student Quiz1Quiz2 Quiz3 QuizAverage Grade
70 80 90 95 95 72 C
85 90 80 75 85 66 D
70 80 55 85 50 54 F
70 45 50 40 35 34 F
70 45 50 40 35 34 F
Press any key to continue . . .

sorry i figured it out thank you for all the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.