Issue with an Input file
Sep 23, 2010 at 8:16pm UTC
I can't seem to get my program to read in data from an input file correctly. Here's my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
//Jordan Kugler
void calcAvg(ifstream&,ofstream&,float &);
char calcGrade(float );
int main()
{
ifstream inFile;
ofstream outFile;
inFile.open ("/HW#5 Input.txt" );
outFile.open ("/HW#5 Output.txt" );
string name;
float classAvg = 0;
float classSum;
float courseAvg;
char grade;
outFile<<"Student Test1 Test2 Test3 Test4 Test5 Avg Grade\n" ;
for (int i = 0; i <= 10; i++)
{
inFile>>name;
outFile<<setw(10)<<name;
calcAvg(inFile, outFile, courseAvg);
outFile<<setw(6)<<courseAvg;
grade = calcGrade(courseAvg);
outFile<<grade<<endl;
classSum += courseAvg;
}
classAvg = classSum / 10;
outFile<<"Class Average: " <<classAvg;
inFile.close();
outFile.close();
}
void calcAvg(ifstream& inF,ofstream& outF,float & courseAvg)
{
int score,i,sum = 0;
for (i = 1;i <= 5;i++)
{
inF>>score;
outF<<setw(4)<<score<<" " ;
sum += score;
}
courseAvg = sum / 5;
}
char calcGrade(float courseAvg)
{
char grade;
if (courseAvg >= 90)
grade = 'A' ;
else
if (courseAvg >= 80)
grade = 'B' ;
else
if (courseAvg >= 70)
grade = 'C' ;
else
if (courseAvg >= 60)
grade = 'D' ;
else
grade = 'F' ;
return grade;
}
The only thing that prints to the output file is the first outFile statement with the column headers. My input file is a list of 10 names and 5 test scores next to each.
Sep 23, 2010 at 8:37pm UTC
You might want to check whether inFile is good().
Sep 28, 2010 at 7:42pm UTC
My compiler's error message looks like this:
HW#5(4151) malloc: *** error for object 0x10000c940: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
I have no idea what this means. Please help. :(
Topic archived. No new replies allowed.