### Funtion calls? Variable not initialized?

Im trying to use functions to display the letter grade and percentage in a course. When i run my program it says that the variable 'OverallPercent' and 'letterGrade' are being used without being initialized. Any help as to where i'm going wrong?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687`` ``````void getMarks(int&, int&, int&); // Gets midterm and final marks void displayGrade(int, char); // Displays course % and letter grade int calcGrade(int, int, int); // Calculates course percentage char CalcletterGrade (int); // Calculates letter grade based on % //************************************* // Main Program //************************************* int main() { // Variables int mt1Mark, mt2Mark, finMark; // Midterm 1, midterm 2 and final marks int OverallPercent; // Percentage in course char letterGrade; // Letter grade in course // Intro cout << "Hello and welcome to the grade calculator" << endl; // Calling functions getMarks(mt1Mark, mt2Mark, finMark); // Calls function to get marks calcGrade(mt1Mark, mt2Mark, finMark); // Calls function to calculate overall % CalcletterGrade(OverallPercent); // Calls function to calculate letter displayGrade(OverallPercent, letterGrade); system ("pause"); return 0; } //************************************* // Function Definitions //************************************* void getMarks(int& mt1Mark, int& mt2Mark, int& finMark) { while ((mt1Mark < 0) || (mt1Mark > 100)) { cout << "Enter midterm 1 exam percentage: "; cin >> mt1Mark; } while ((mt2Mark < 0) || (mt2Mark > 100)) { cout << "Enter midterm 2 exam percentage: "; cin >> mt2Mark; } while ((finMark < 0) || (finMark > 100)) { cout << "Enter final exam percentage: "; cin >> finMark; } } int calcGrade(int mt1Mark, int mt2Mark, int finMark) { int OverallPercent; OverallPercent = ((mt1Mark * 0.3) + (mt2Mark * 0.3) + (finMark * 0.4)); return OverallPercent; } char CalcletterGrade(int OverallPercent) { char letter; if (OverallPercent >= 84) letter = 'A'; else if ((OverallPercent <= 83) || (OverallPercent >= 71)) letter = 'B'; else if ((OverallPercent <= 70) || (OverallPercent >= 59)) letter = 'C'; else if ((OverallPercent <= 58) || (OverallPercent >= 55)) letter = 'D'; else letter = 'F'; return letter; } void displayGrade (int OverallPercent, char letterGrade) { cout << "Your percentage in this course is: " << OverallPercent << " %" << endl; cout << "Your letter grade is: " << letterGrade << endl; }``````
Your not assigning a value to overallpercent declared on line 14. I think you mean to do so in the function calcGrade. if so you do it like

`overallpercent = calcGrade(mt1Mark, mt2Mark, finMark);`

Then when you return overallpercent it will assign the value to overallpercent. Just a tip, but try to keep your variable names unique as it gets very confusing to know the scope of each one.
Last edited on
On lines 13-15 you declare your variables.

On lines 21, 23, 25, 27 you send junk values as arguments because no where did you assign values to these variables, which is what initialisation is. The code in the getMarks function only works because the junk value (somewhere in between +/- 2 billion) is likely not between 0 & 100. So you can see this is bad practice

Try declaring & initialising your variables at the same time, 1 per line. Sometimes, zero is not a good choice, but seems to be OK here :

 ``12345`` ``````int MidtermMark1 = 0; // between 0 & 100, 30% of Overall - the first test int MidtermMark2 = 0; // between 0 & 100, 30% of Overall - the second test int FinalMark = 0; // between 0 & 100, 40% of Overall - the last assignment int OverallPercent = 0; // Percentage in course char letterGrade = 'Z'; // can be A, B, C, D or F ``````

You can see I have used better variable names to make the code self documenting. They are initialised, and I have used comments to give important info about valid ranges, and what the variable is used for.

If you work in the IT industry, this stuff is important - the tired old grumpy maintenance programmer looking at your code will immediately see what is going on. It can also be useful when looking at your own code 2 or 3 years later.

The variable OverallPercent should be a double, because it gets promoted to this type on line 62, and returning it on line 63 should attract a warning (loss of precision double to int). You can use `static_cast<int>` to cast it to an int before returning it. Have a look at the reference material top left of this page.

Be careful naming local variables inside a function the same as those in main() - I know you have done it to reinforce the idea that they are the same thing, but now they have different types.

Hope all goes well.
review these:
at lines 40, 46, 52
while ((mt1Mark < 0) || (mt1Mark > 100))

at line 21
getMarks(mt1Mark, mt2Mark, finMark);

at line 38
void getMarks(int& mt1Mark, int& mt2Mark, int& finMark)
Last edited on
I apologize for my coding style. I find programming very fascinating but am still very new to everything.

@pata @TheIdeasMan I will take your advice into consideration, my professor pre assigned variable names hence the difficulty in understanding them. I renamed them as you said and they were much easier to comprehend at first glance.

As for the solution to my question, @pata pointed it out. I made these changes and my program works!

For everyone who replied thank you!! I really appreciate it =)
Topic archived. No new replies allowed.