Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
double avgToFinalLetterGrade, totalKnownGradeWeight, finalOverallScore;
finalOverallScore = 0;
if (desiredGrade == "A" || desiredGrade == "a") {
finalOverallScore = 90;}
else if (desiredGrade == "B" || desiredGrade == "b") {
finalOverallScore = 80; }
else if (desiredGrade == "C" || desiredGrade == "c") {
finalOverallScore = 70;}
else if (desiredGrade == "D" || desiredGrade == "d") {
finalOverallScore = 60; }
else if (desiredGrade == "F" ||desiredGrade == "f") {
finalOverallScore = 0; }

totalKnownGradeWeight = (weightExamOne + weightExamTwo + weightFinalExam + weightLabs + weightProjects + weightAttendance + weightQuizzes);
avgToFinalLetterGrade = ((100 * finalOverallScore) - ((weightExamOne * examOneScore) + (weightExamTwo * examTwoScore) +(weightFinalExam * examFinalScore) + (weightLabs * avgLabGrade) + (weightQuizzes * avgQuizGrade) + (weightProjects * avgProjectGrade) + (weightAttendance * avgAttendanceGrade))
/ (100 - (totalKnownGradeWeight)));

What I have tried:

whenever i run the program (before this section theres a lot more if else and user input) the finalOverallScore doesnt change conditionally with my if else, only goes in as 0. i initialized it as zero so it had a path to go w out the if else or the variable showed up as uninitialized... whats wrong
Posted
Updated 19-Sep-17 13:08pm

We can't tell - too much depends on the content of your data, and when that code actually gets executed: and we have no control over that, nor can we test it here.

So, its going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
I would go for a switch statement rather than a bunch of if...else statements.
Something like:
C#
switch (desiredGrade.ToLower()) {
   case "a":
      finalOverallScore = 90;
      break;
   case "b":
      finalOverallScore = 80;
      break;
   case "c":
      finalOverallScore = 70;
      break;
   case "d":
      finalOverallScore = 60;
      break;
   case "e":
      finalOverallScore = 50;
      break;
   case "f":
      finalOverallScore = 0;
      break;
   default:
      throw new ArgumentException("Wrong grade", nameof(desiredGrade));
}


As for knowing why your variable does not get the correct value, you will have to put a breakpoint on the first line of the method and debug to see why. It could be that desiredGrade is not A, a, B, b, C, c, D, d, F nor f.

Note: I added the "e" case which you did not seem to have taken into account.
Note 2: transforming the value to lower case allows you to have fewer conditions to test.

Most important thing to understand here is that you have to debug your code to understand why it does not behave like you would expect it to.

Kindly.
 
Share this answer
 
That mean that desiredGrade value is not what you think.
the only way to know what is what is to use the debugger, set a breakpoint and inspect desiredGrade value and length.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900