C++/C++ program and I am stuck!
Expert: Zlatko - 3/27/2010
QuestionQUESTION: Here is my program. I cannot get it to work ..following that are the instructions..any advice would be appreciated
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<cstdlib>
using namespace std;
// function prototypes
void calculateAverage (ifstream&, ofstream& , double&);
void calculateGrade (double);
int main()
{
// Declare Vatriables
double avg, average = 0;
int count =0;
string name;
// Declare file streams
ifstream infile;
ofstream outfile;
// opens the input file
infile.open ("Ch7_Ex9Data.txt");
// Display error if file was not opened
if ( !infile.is_open() )
{
cout << "Error in opening file.";
cout << "Program terminates." << endl;
return 1;
}
// opens the output file
outfile.open ("Ch7_ExOut.txt");
outfile << fixed << showpoint;
outfile << setprecision(2);
outfile << "Student Test1 Test2 Test3 "
<< "Test4 Test5 Average Grade" << endl;
// read name from input file
infile >> name;
//break loop when all data is read
while ( !infile )
{
}
// write the name to the output file
outfile << setw (10) << left << name;
//read and then write the test scroes from file
calculateAverage ( infile, outfile, avg );
// write average and grade to output file
outfile << setw (10) << right << avg;
outfile << setw (6) <<right << calculateGrade ( avg ) << endl;
// Keep track of class count and Average
count++;
average += avg;
// Compute class average
average /=count;
// write class average to output file
outfile << endl << "Class Average = " << average;
//Close file Streams
infile.close ();
outfile.close ();
return 0;
}
// returns the average
void calculateAverage ( ifstream& in, ofstream& out, double& avg );
// declare variable
int score;
// initialize the average
int avg = 0;
// read scores from input file ,write to output
// and compute the scores sum
for ( int i = 0 ; i < 5; i++ )
{
inp >> score;
outp << setw( 7 ) << right << score;
avg += score;
// compute average of the student
avg /= 5.0;
}
// returns the grade of the student
void calculateGrade (double myAvg);
{
if (myAvg < 60)
return 'F';
else
if (myAvg < 70)
return 'D';
else
if (myAvg < 80)
return 'C';
else
if (myAvg < 90)
return 'B';
else
return 'A';
}
Your program should read and write files that, by default, are located in the same folder as your .cpp program. Your input file should be named "Ch7_Ex9Data.txt". This file has already been created and a copy of it may be found in the "Templates, Input Files, ..." Angel folder under the "Lessons" tab. Your output file should be named "Ch7_ExOut.txt".
Read and write a student's name from within "main". Read and write a student's test scores from within "calculateAverage".
You may assume that the input file does not contain data errors, however, your program must be able to process any number of input records.
also here is the text file
Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 73
Gupta 92 83 30 69 87
Blair 23 45 96 38 59
Clark 60 85 45 39 67
Kennedy 77 31 52 74 83
Bronson 93 94 89 77 97
Sunny 79 85 28 93 82
Smith 85 72 49 75 63
ANSWER: Hello Jason.
My advice to you is to first get the program to compile. Start with the first error. Fix it, then recompile. Often fixing one compiler error will eliminate many messages. You obviously know enough of the language to fix the errors. Anyway, below is your program with all the compiler errors removed. The program almost works. It creates an output file with one student. Try to read through the main function to see why it's behaving as it is. If you know how to use a debugger, try stepping through the program to see how it behaves.
The calculateAverage function has an error. It's giving an average that's much too low. Try to see what your error is.
You're almost there.
Best regards
Zlatko
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<cstdlib>
using namespace std;
// function prototypes
void calculateAverage (ifstream&, ofstream& , double&);
char calculateGrade (double);
int main()
{
// Declare Vatriables
double avg, average = 0;
int count =0;
string name;
// Declare file streams
ifstream infile;
ofstream outfile;
// opens the input file
infile.open ("Ch7_Ex9Data.txt");
// Display error if file was not opened
if ( !infile.is_open() )
{
cout << "Error in opening file.";
cout << "Program terminates." << endl;
return 1;
}
// opens the output file
outfile.open ("Ch7_ExOut.txt");
outfile << fixed << showpoint;
outfile << setprecision(2);
outfile << "Student Test1 Test2 Test3 "
<< "Test4 Test5 Average Grade" << endl;
// read name from input file
infile >> name;
//break loop when all data is read
while ( !infile )
{
}
// write the name to the output file
outfile << setw (10) << left << name;
//read and then write the test scroes from file
calculateAverage ( infile, outfile, avg );
// write average and grade to output file
outfile << setw (10) << right << avg;
outfile << setw (6) <<right << calculateGrade ( avg ) << endl;
// Keep track of class count and Average
count++;
average += avg;
// Compute class average
average /=count;
// write class average to output file
outfile << endl << "Class Average = " << average;
//Close file Streams
infile.close ();
outfile.close ();
return 0;
}
// returns the average
void calculateAverage ( ifstream& in, ofstream& out, double& avg )
{
// declare variable
int score;
// initialize the average
avg = 0;
// read scores from input file ,write to output
// and compute the scores sum
for ( int i = 0 ; i < 5; i++ )
{
in >> score;
out << setw( 7 ) << right << score;
avg += score;
// compute average of the student
avg /= 5.0;
}
}
// returns the grade of the student
char calculateGrade (double myAvg)
{
if (myAvg < 60)
return 'F';
else
if (myAvg < 70)
return 'D';
else
if (myAvg < 80)
return 'C';
else
if (myAvg < 90)
return 'B';
else
return 'A';
}
---------- FOLLOW-UP ----------
QUESTION: Okay I got it run and thereare no errors but I hit a wall with the out put
Here is how it needs to look ( Im just showing the firat 2 lines, The number mean nothing. I just typed them out )
Student Test1 Test2 Test3 Test4 Test5 Average Grade
Johnson 85 83 77 91 76 85 B
Aniston 80 90 74 95 48 85 B
And here is the ERROR FREE progam ( again it works its just the output)
// include statement(s).
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<cstdlib>
// using namespace statement.
using namespace std;
// function prototypes
void calculateAverage(ifstream& _inFile, ofstream& _outFile, double& _runningAverage);
char calculateGrade( double _grade);
const int numTests = 5;
int main()
{
// Declare Vatriables
int count = 0;
double runningAverage =0;
string studentName;
// Declare file streams
ifstream inFile;
ofstream outFile;
// opens the input file
inFile.open ("Ch7_Ex9Data.txt");
// Display error if file was not opened
if ( !inFile.is_open() )
{
cout << "Error in opening file.";
cout << "Program terminates." << endl;
return 1;
}
// Opens the output file
outFile.open ("Ch7_ExOut.txt");
outFile << fixed << showpoint;
outFile << setprecision (2);
outFile << "Student" << setw(10) << "Test1" << setw(10) << "Test2" << setw(10)
<< "Test3" << setw(10) <<"Test4" << setw(10) << "Test5" << setw(10)
<< "Average" << setw(10) << "Grade" << setw(10) <<endl;
// Read and write a students name from the input file
inFile >> studentName;
while(inFile)
{
count++;
outFile << setw(10) << studentName;
calculateAverage(inFile, outFile, runningAverage);
inFile >> studentName;
}
outFile << "Class Average= " << (runningAverage/count);
//Close file Streams
inFile.close ();
outFile.close ();
return 0;
}
// deterime the average of the 5 test scores for each student
// Read and write a students test scores
void calculateAverage(ifstream& _inFile, ofstream& _outFile, double& _runningAverage)
{
int testsTotal = 0;
int test = 0;
double testsAverage = 0;
for(int i=0; i<numTests; i++)
{
_inFile >> test;
testsTotal += test;
_outFile << setw(10) << test;
}
testsAverage = testsTotal / numTests;
_runningAverage += testsAverage;
_outFile << setw(10) << testsAverage;
_outFile << setw(10) << calculateGrade(testsAverage) << "\n";
}
// determines and returns the students letter grade
char calculateGrade (double _grade)
{
if (_grade < 60)
return 'F';
else if (_grade < 70)
return 'D';
else if (_grade < 80)
return 'C';
else if (_grade < 90)
return 'B';
else
return 'A';
}
Thank you SO much in advance
AnswerHello Jason
Well, I think I got the output you want. I had to change many of the setw calls, and left justify the student names. My changes are marked with //XXX . For the test scores, I shortened the field to 5 characters and printed a space after the score. For the average, I rounded to the nearest integer and printed the integer. That eliminated the decimal point.
You should understand that the setw call formats the next thing being printed, not the previous. So, where you had:
outFile << "Student" << setw(10) << "Test1" << setw(10) << "Test2" << setw(10)
<< "Test3" << setw(10) <<"Test4" << setw(10) << "Test5" << setw(10)
<< "Average" << setw(10) << "Grade" << setw(10) <<endl;
The first setw should be before "Student", not after. The last setw has no effect in the above line.
Honestly, I don't use output formatting at all in my work, so I hope my answer is satisfactory.
Here is the entire program again:
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<cstdlib>
// using namespace statement.
using namespace std;
// function prototypes
void calculateAverage(ifstream& _inFile, ofstream& _outFile, double& _runningAverage);
char calculateGrade( double _grade);
const int numTests = 5;
int main()
{
// Declare Vatriables
int count = 0;
double runningAverage =0;
string studentName;
// Declare file streams
ifstream inFile;
ofstream outFile;
// opens the input file
inFile.open ("Ch7_Ex9Data.txt");
// Display error if file was not opened
if ( !inFile.is_open() )
{
cout << "Error in opening file.";
cout << "Program terminates." << endl;
return 1;
}
// Opens the output file
outFile.open ("Ch7_ExOut.txt");
outFile << fixed << showpoint;
outFile << setprecision (2);
outFile << std::left << setw(10) << "Student" << std::right << setw(6) << "Test1" << setw(6) << "Test2" << setw(6)
<< "Test3" << setw(6) <<"Test4" << setw(6) << "Test5" << setw(8)
<< "Average" << setw(6) << "Grade" << endl; // XXX
// Read and write a students name from the input file
inFile >> studentName;
while(inFile)
{
count++;
outFile << std::left << setw(10) << studentName << std::right; // XXX
calculateAverage(inFile, outFile, runningAverage);
inFile >> studentName;
}
outFile << "Class Average= " << (runningAverage/count);
//Close file Streams
inFile.close ();
outFile.close ();
return 0;
}
// XXX added nearestInt function
int nearestInt(double x)
{
return (int)(x+0.5);
}
// deterime the average of the 5 test scores for each student
// Read and write a students test scores
void calculateAverage(ifstream& _inFile, ofstream& _outFile, double& _runningAverage)
{
int testsTotal = 0;
int test = 0;
double testsAverage = 0;
for(int i=0; i<numTests; i++)
{
_inFile >> test;
testsTotal += test;
_outFile << setw(5) << test << ' '; //XXX
}
testsAverage = testsTotal / numTests;
_runningAverage += testsAverage;
_outFile << setw(6) << nearestInt(testsAverage); //XXX
_outFile << setw(6) << calculateGrade(testsAverage) << "\n"; // XXX
}
// determines and returns the students letter grade
char calculateGrade (double _grade)
{
if (_grade < 60)
return 'F';
else if (_grade < 70)
return 'D';
else if (_grade < 80)
return 'C';
else if (_grade < 90)
return 'B';
else
return 'A';
}