Hi all!! I've just written some code for an assignment, and I have NO idea where I've gone wrong! Please help!!

Pretty self explanatory. I'll keep this short and get straight to it.

Here's the assignment: http://imgur.com/HPPqFFr

and 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
70
71
72
73
74
75
76
77
#include <iostream>
#include <fstream>
using namespace std;

int highestGrade(int grade1, int grade2, int grade3); // Prototypes
char letterGrade(int high);

int main() // Main Function
{
	string name; // Necessary variables
	int grade1;
	int grade2;
	int grade3;
	int highest;
	char letter;
	int count;

	ifstream inputFile; // Opens Grades.txt to retrieve data
	inputFile.open("Grades.txt");

	ofstream outputFile; // Opens Results.txt to write data
	outputFile.open("Results.txt");

	for int count = 0; count < 4; count++) // Loop to handle all four students
	{

		inputFile >> name; // Takes the name and the assigns it to a variable, name
		string name = name;
		inputFile >> grade; // Takes the first grade and assigns it to a variable, grade1
		int grade1 = grade;
		inputFile >> grade; // Takes the second grade and assigns it to a variable, grade2
		int grade2 = grade;
		inputFile >> grade; // Takes the third grade and assigns it to a variable, grade3
		int grade3 = grade;
		
		int highestGrade(int grade1, int grade2, int grade3); // Calls the value returning function highestGrade, with grade1/2/3 as int inputs
		
		char letterGrade(int highest); // Calls the value returning function letterGrade, with highest as an int input

		outputFile << name; // Writes the name to the file Results.txt
		outputFile << letter; // Writes the letter to the file Results.txt
		
		return 0;
	}



} // Ends the main function

int highestGrade(int grade1, int grade2, int grade3) // Defines the function highestGrade, and calculates which of the three variables (grade1,2,3) has the highest value and returns it
{
	int grade1;
	int grade2;
	int grade3;

	int highest = grade1;
	if (grade2 > grade 1)
		highest = grade2;
	if (grade3 > highest)
		highest = grade3;
	return highest;
}
	
char letterGrade(highest) // Defines the function letterGrade, and calculates the letter grade based on the returned value 'highest' from the highestGrade function, and returns that value with the variable letter
{
	if (highest >= 90)
		letter = A;
	if (highest >= 80, and not (highest >= 90))
		letter = B;
	if (highest >= 70, and not (highest >= 80))
		letter = C;
	if (highest >= 60, and not (highest >= 70))
		letter = D;
	else
		letter = F;
	return letter;
}


It seems like it should be working to me, and I can't figure out where I've gone wrong. I need somebody to critique this for me so I can understand where I've made a misstep. Thanks a lot guys!!
There is a huge amount of errors within this program. Quite frankly most of them are self explanatory.
Alright lets begin.
#1. You never included <string>

#2. On line 6 you have a completely different prototype compared to the one you actually initialized

#3. On line 19 you never close the file("Grades.txt") and then proceed to open another file.

#4. Lines 27-34 it isn't necessary to input a variable from the fstream and then proceed to reassignt again to itself...

#5 Not only that but lines 27-34 are taking in input from the txt file Result.txt due to the fact that is the one you have currently opened which I doubt is what you wanted. I assume you wanted to take in input from Grades.txt

#6 Line 29-30 the variable grade doesn't exist.

#7 Line 36 and 38 aren't doing anything. All your doing is calling and not even storing the value they returned.

#8 Line 40-41 You should open result.txt before that and it should work.

#9 Not really an error but you should define your parameters as const for better readibilty in the future

#10 Lines 52-54 these are useless variables and only cause ambiguity due to the fact they are named the same as the variables within the function parameters

#11 Line 64 HUGE ERRORS in here. First of all the function isn't the same as the prototyped one. Second of all its parameter highest has no type.

#12 Lines 66-76 The variable letter doesn't exist. Even if it did exist you aren't assigning it to a character, you are instead assigning to something that doesn't even exist. To assign a char to a character you enclose it in single quotes ' '.

EDIT: I spotted another pretty important error you have. On line 43 you are returning 0 within the for loop in main which will immediately cause the program to shut thus making the loop only run once. Move the return 0 statement outside of the loop.
There might be more errors but you should at least fix all those extremely important errors first.
Last edited on
There is a huge amount of errors within this program. Quite frankly most of them are self explanatory.
Alright lets begin.

I would mostly ignore that particular post since most of the "self explanatory errors" are not errors at all.

Your code will not compile as is. Begin with the first compiler error generated. Fix it. Try to compile again. More errors? Repeat with the first compiler error until you have no more.

If you want help and your code doesn't compile.. present the compiler errors with your code. If you want help and your code does compile, please describe what you expect your code to do and how what that differs from what it actually does.

Nobody wants to guess at what you're trying to accomplish.
As far as I'm concerned, I was always taught to close files upon program exit. This could be an issue you're having if I'm not mistaken.
Topic archived. No new replies allowed.