C++ reading/output files

Status
Not open for further replies.

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Hey back again, okay so the program is to read in a file that has 10 students names with 5 test scores for each of them. The file looks like this:

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 74 63

Then I am suppose to write it to a new file which is suppose to look like this:

Student Test1 Test2 Test3 Test4 Test5 Average Grade
( the names and test scores with the average and the average turned into a letter grade)


I started off with like 35+ errors, got it down to like 2, well here's the program and the errors:


Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

void calcAvg(ifstream& in_file, ofstream& out_file, float& avg);
char calc_grade(float avg);

int main() {

	ifstream inp;
	ofstream outp;
	
	inp.open("Ch07_Ex09Date.txt");
	outp.open("Ch07_Ex09OUT.txt");

	outp << "Student	Test1	Test2	Test3	Test4	Test5	Average	Grade";

	string name;
	float classAvg = 0;
	char grade;
	int k;
	float avg;

	for (k=1; k<=10; k++)
	{
		inp >> name;
		outp << setw(10) << name;


		calcAvg(inp, outp, avg);
		outp << setw(6) << avg;
		classAvg = classAvg + avg;
		grade = calc_grade(avg);
		outp << setw(5) << " " << grade << endl;
	}

	classAvg = classAvg / 10;
	outp << endl << "Class Average = " << classAvg;

	
	return 0;
}

void calcAvg(ifstream& inp, ofstream& outp, float& avg)
{
	int score, k;
	float sum = 0.0;

	for (k=1; k<=5; k++)
	{
		inp >> score;
		outp << setw(4) << score << " ";
		sum = sum + score;
	}
	avg = sum / 5;
}

char calc_grade(total)
{
	
	if (total >= 0)
		
		if (total >= 60)
	
			if (total >= 70)
			
				if (total >= 80)
				
					if (total >= 90)
					
						if (total > 100)
							cout << "Grade can not exceed 100" << endl;
						else
							cout << "A" << endl;
					else
						cout << "B" << endl;
				else
					cout << "C" << endl;
			else
				cout << "D" << endl;
		else
			cout << "F" << endl;
	else
		cout << "Grade can not be a negative number" << endl;
}






ERRORS:

1>c:\users\welcome back\documents\visual studio 2008\projects\file\file\file input.cpp(59) : error C2065: 'total' : undeclared identifier
1>c:\users\welcome back\documents\visual studio 2008\projects\file\file\file input.cpp(60) : error C2448: 'calc_grade' : function-style initializer appears to be a function definition
 
Solution
OK, I've found your problem now. The input file is never being opened. It's down to the working directory when the program is being debugged. In my case that directory was:

C:\Users\Ian\Documents\Visual Studio 2010\Projects\test101\test101

You either need to put the input file in this directory or, if they are in another directory provide a full path name. So if you want the input and output files to be in C:\tmp (for instance), you need to open them as:

Code:
inp.open("C:\\tmp\\Ch07_Ex09Date.txt" );
outp.open("C:\\tmp\\Ch07_Ex09OUT.txt" );

This then produces an output file filled with figures. I'll leave it up to you to check whether the results are correct.

The value you were getting for avg was just a random one because you...

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
ohh ok,yea i was like I thought when I put total as the parameter it was declaring it but I guess i forgot i need to say what kind of variable it is.

How would I return the letter grade? It wouldn't be return total right because that would just return the number grade right?


hmmm would it be instead of COUTing the letter grade I would set them to a value like grade = 'A' for a char then return grade?

Code:
                                                 else
							grade = 'A';
					else
						grade = 'B';
				else
					grade = 'C';
			else
				grade = 'D';
		else
			grade = 'F';


return grade;
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
okay here's the new version, it runs but right away shows me this error, with attach a image of it, but here's the code:

Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

void calcAvg(ifstream& in_file, ofstream& out_file, float& avg);
char calc_grade(float avg);

int main() {

	ifstream inp;
	ofstream outp;
	
	inp.open("Ch07_Ex09Date.txt");
	outp.open("Ch07_Ex09OUT.txt");

	outp << "Student	Test1	Test2	Test3	Test4	Test5	Average	Grade";

	string name;
	float classAvg = 0;
	char grade;
	int k;
	float avg;

	for (k=1; k<=10; k++)
	{
		inp >> name;
		outp << setw(10) << name;


		calcAvg(inp, outp, avg);
		outp << setw(6) << avg;
		classAvg = classAvg + avg;
		grade = calc_grade(avg);
		outp << setw(5) << " " << grade << endl;
	}

	classAvg = classAvg / 10;
	outp << endl << "Class Average = " << classAvg;

	
	return 0;
}

void calcAvg(ifstream& inp, ofstream& outp, float& avg)
{
	int score, k;
	float sum = 0.0;

	for (k=1; k<=5; k++)
	{
		inp >> score;
		outp << setw(4) << score << " ";
		sum = sum + score;
	}
	avg = sum / 5;
}

char calc_grade(float total)
{
	char letter;
	
	if (total >= 0)
		
		if (total >= 60)
	
			if (total >= 70)
			
				if (total >= 80)
				
					if (total >= 90)
					
						if (total > 100)
							cout << "Grade can not exceed 100" << endl;
						else
							letter = 'A';
					else
						letter = 'B';
				else
					letter = 'C';
			else
				letter = 'D';
		else
			letter = 'F';
	else
		cout << "Grade can not be a negative number" << endl;

	return letter;
}


ERROR:
Untitled-1.png

 

Ijack

Distinguished
The dialog is arising becasue you haven't assigned any value to "letter" in the cases that "total" is negative or >100. The console shows that in this case "total" was negative (this may be indicative of another bug).

In line 62, when you declare "letter" assign some value to it. Then that value will be returned in the two error cases. E.g.

62. char letter = ' ';

Now you just need to determine why "total" was negative. In cases like this I'd recommend using the debugger to single-step through the program, or parts of it, and watch what values are assigned to the variables. It should be pretty obvious when things go wrong.
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
I think I found out how to do it but when I get to where i declared classAvg = 0.0 it is say the value is -1.0737418e+008. Everything is using like that type of value.... I am so confused, is my program wrong or something?
 

Ijack

Distinguished
No, I think that's just a normal floating-point error. Since floating-point numbers can't be represented exactly in a computer there are always going to be errors of this level of magnitude. (Although I'm surprised that it happens for 0.0.)

If it matters, it's always better to test whether A - B < 1e+007 (that's just an example) rather than testing whether A = B. In really important calculations (e.g. financial ones) you would use fixed-point numbers rather than floating-point ones to avoid this sort of error.
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
avg is never changing???? its value is always -8.5899347e+008.... is my file being read in?? and my grade is always 113 and the same value that is set in the declaring statement of 'q'.
 

Ijack

Distinguished
OOps. I misread the exponential as e-008 when you said e+008. That is not a rounding error!

I'm not quite sure without checking it out what's going wrong here. Have you tried single-stepping through calcAvg to see what's happening?

BTW, I would have defined calcAvg as a function of two parameters returning the value avg, but that's just a matter of choice.

I haven't got Visual C++ on this computer, but if you can't find the problem I'll fire up my other one and have a look.
 

Ijack

Distinguished
OK, I've found your problem now. The input file is never being opened. It's down to the working directory when the program is being debugged. In my case that directory was:

C:\Users\Ian\Documents\Visual Studio 2010\Projects\test101\test101

You either need to put the input file in this directory or, if they are in another directory provide a full path name. So if you want the input and output files to be in C:\tmp (for instance), you need to open them as:

Code:
inp.open("C:\\tmp\\Ch07_Ex09Date.txt" );
outp.open("C:\\tmp\\Ch07_Ex09OUT.txt" );

This then produces an output file filled with figures. I'll leave it up to you to check whether the results are correct.

The value you were getting for avg was just a random one because you never initialized the variable. In the function calAvg the variable score was again uninitialized, so had the same value, which was then being assigned to avg. It's good practice to initialize all variable to some known value; some compilers automatically initialize variables to 0, but most will just leave some random value in the variable.
 
Solution

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Haha yes it works, yea i remembered in my first C++ class we just glanced at files but we put ours on our flashdrive and we had to specify the full path of the flashdrive for the file to be found, just totally forgot about that. THANK YOU!!!! now just gotta figure out how to get everything nice and neat inside the file.
 
Status
Not open for further replies.