1,105,320 Community Members

compile problem!!!

Member Avatar
bandit711
Newbie Poster
22 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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 .

Member Avatar
mitrmkar
Posting Virtuoso
1,766 posts since Nov 2007
Reputation Points: 1,056 [?]
Q&As Helped to Solve: 403 [?]
Skill Endorsements: 9 [?]
 
0
 

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.

Member Avatar
Salem
Posting Sage
7,177 posts since Dec 2005
Reputation Points: 5,138 [?]
Q&As Helped to Solve: 970 [?]
Skill Endorsements: 41 [?]
Team Colleague
 
1
 

http://sourceforge.net/apps/mediawiki/cpwiki/index.php?title=Indentation
Your code is basically unreadable without a lot of effort on our part.
The kind of effort which makes us find something else to do.

Member Avatar
bandit711
Newbie Poster
22 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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 . . .

Member Avatar
bandit711
Newbie Poster
22 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

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;
 
}
Member Avatar
bandit711
Newbie Poster
22 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

please help.

Member Avatar
bandit711
Newbie Poster
22 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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;
 
}
Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

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.

Member Avatar
bandit711
Newbie Poster
22 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

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.

Member Avatar
bandit711
Newbie Poster
22 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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 . . .

Member Avatar
bandit711
Newbie Poster
22 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Question Answered as of 4 Years Ago by WaltP, mitrmkar and Salem
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: