# Why can't I get my code to print the correct letter grade for my test scores

 Number Double07 · May 17, 2015 I want my code to print letter grades for my test scores from three exams, quiz, midterm, and a final.  An A is 90 and above, B is 70 to 89.9999999, C is 50 to 69.999999, the rest is failing.  To summarize what I have done, I created two functions, one to calculate the average of the test scores, and the other to give a letter grade corresponding to that average.  However, my letter grade keeps giving me the @ symbol.  I've been stuck on this for 35 minutes now.  ``#include using namespace std;float finalScore (int x, int y, int z);char letterGrade (float x);float finalScore (int x, int y, int z){   float average;   float total_score = static_cast (x+y+z);   float tests = 3;   average = total_score/ tests;   return average;}char letterGrade (float x){   char Grade;   if (x >= 90)       char Grade = 'A';   else if (x >= 70 && x < 90)       char Grade = 'B';   else if (x >= 50 && x < 70)       char Grade = 'C';   else       char Grade = 'F';   return Grade;}int main(){/*2. Write a program that determines a student’s grade.The program will read three types of scores (quiz, mid-term, and final scores)and determine the grade based on the following rules:-if the average score =90% =>grade=A-if the average score >= 70% and  grade=B-if the average score>=50% and grade=C-if the average scoregrade=F*/int nquiz;int nmid_term;int nfinal;float faverage;char Grade;cout  nquiz;cout  nmid_term;cout  nfinal;faverage = finalScore(nquiz, nmid_term, nfinal);cout ``

## Replies

- page 1

 Michael Bradford · May 19, 2015 Couple things first sumeet is right in that you're not assigning anything your values, that shouldn't give you null it will probably give you w/e junk is inside the memory space that's why you should almost ALWAYS initialize your variables even if your going to define them later.@ is definitely not null for a char. It's a specific character, why your getting it over and over 1, you never initialize Grade in Main you just say char Grade, and you never give it a value by calling your letterGrade function. So most likely whatever junk is in the memory space your compiler is using for the Grade variable would result in an '@' character on implicit conversion. Also about that letterGrade function, STOP redefining Grade over itself by saying char Grade = 'A' char Grade = 'B' just say Char Grade; once then call it by it's identifier "Grade", so Grade = 'A'; etc. Michael Bradford · May 19, 2015 Also could you repaste your code a lot of it seems to be missing because the tags are interpretting it. Sengngy Kouch · May 17, 2015 Hi there!let me restate your question. Assuming your Average function is working as intended.Assuming you are doing thing right with your main function.However, when you try get print out the letter, it print out only @. So here is my solution.First of all, try using double instead of float. Float should never be use unless you want something really big and precise.``double finalScore (int x, int y, int z){ double average; double total_score = (double) (x+y+z); double tests = 3; average = total_score/ tests; return average;}``Then, try using string instead of char.``string letterGrade (double x){ string Grade; if (x >= 90) string Grade = "A";; else if (x >= 70 && x < 90) string Grade = "B"; else if (x >= 50 && x < 70) string Grade = "C"; else string Grade = "F"; return Grade;}``I hope this help! Sengngy Kouch · May 18, 2015 @Number Double07:  Nice, I am glad that you learn something new!By the way, if I am not wrong, @ mean null in Char.  In other words, when you have  Char grade. ``Char Grade; // declare, but not assign to anything.``Because you assign it to nothing, when it returns, it just returns null . which is @ symbol. This mean that your if and else statement doesn't work.I don't know if this make sense.Try assign it to something, and you will see.``char letterGrade (float x){ char Grade = 'Z'; if (x >= 90) char Grade = 'A'; else if (x >= 70 && x < 90) char Grade = 'B'; else if (x >= 50 && x < 70) char Grade = 'C'; else char Grade = 'F'; return Grade;}``Try it, and the result might be Z. Sengngy Kouch · May 19, 2015 @Michael:  lol, I do not know why I declare Char Grade so many times in the Letter Function, but yes, I should just declare it once, and assign it to different value.For the @ symbol, you state that we never initialize/Assign Grade to anything and that is why we get @. This seem to be that it implies that Grade is null, Michael Bradford · May 20, 2015 No read the bit where I say ," it will probably give you w/e junk is inside the memory space that's why you should almost ALWAYS initialize your variables even if your going to define them later" When you declare a variable in c++ and don't assign it a value (initialize it) it DOES NOT give you a null value it gives you whatever may have been present in the memory space that it was designated, so if that memory space was some random int c++ would in this case probably implicitly convert that int value ( or w/e value was there ) to a char. [ It could also have just been an implicit conversion of the hexidecimal location of the memory space I'm not entirely sure if it will do that ] Either way not initializing a variable will not give you NULL it will give you an unpredictable, unwanted result. A null char is just that... null and when you assign a char to NULL then call it out you'll get nothing.. (try it)Now this is where you should be asking yourself "If it's really some unpredictable random garbage left in the memory space, how come we get the same result every time?" The answer? The compilers actually pretty smart and as you rebuild and rerun the program it will remember and reuse the place in memory it used a second ago to fit that variable getting you the same output. However if you declare ANOTHER uninitialized char and call it out you will get something different (try it)This is a VERY important concept in c++ with the ability to control so close what happens in memory you also don't get to take things for granted like uninitialized variables defaulting to NULL values.I'm not sure if you've learned about pointers yet but when you do you'll realize that you don't actually destroy the values in the memory you just make it so that they aren't accessible by that  pointer anymore and then the compiler knows it can reuse and write-over that piece of memory, so if you don't tell it to write-over (but do assign that memory spot a variable) and then access it, you'll get whatever was left. Michael Bradford · May 20, 2015 So I find since were talking about memory and what happens to uninitialized variables in c++ some crude drawings might help ILLLUSTRATE what I'm talking about (did you guys get the pun?).The image above shows what happens when you declare a variable (in this case on the stack memory), the compiler goes "Ok so you've given me an identifier 'X' ill make that identifier point to a spot in memory for itself." Now one of the reasons not initializing is unpredictable is because the compiler could choose whatever spot in memory (in bytes) it can manage to fit your variable in [a char variable happens to be 1 byte, so it can basically be plopped in wherever there's just 1 byte of free memory]. The point is we dont KNOW where its going to point so predicting what will be left in that memory spot is fruitless.As far as the picture is concerned though let's presume that the compiler decided on the middle memory spot which has the carrot character ( '^' ) left over from something else. At this point we have the pitcure below.At this point we have declared char x; The compiler pointed the identifier of 'x' to the memory spot for a char and then we call out the identifier 'X' to see what we get, the compiler says "ok call out x, x is pointing to memory spot (insert some hexidecimal number that represents the memory spot), ok" then it grabs the info at that memory spot and spits it to the output. What we get in output is not an expected result we get the '^' that was left over from some other program.Now if we had initialized the variable (You should at least initialize to NULL if you plan on figuring out what to put in the variable later) we get the picture below.With this one we declare the variable x and immediately set it to the value of NULL, that way when the compiler points it to that memory space from before instead of leaving in the '^' it writes over it with NULL, this is important because if the compiler had chosen either other memory space we still would end up with NULL in the memory space assosciated with our identifier 'X'. Now when we ask the compiler to call out 'X' the compiler looks up X says "Ok it's pointing at (insert hexadecimal memory location)" then gets the value (which we immediately set (or initialized) to NULL) and then calls that out... giving us well NULL nothing is really output.The important thing to remember is that with this method there are no surprise results we KNOW that X has NULL in it because we told it to have NULL if X get assigned anything other than NULL it's because of something else happening within the program.I hope these drawings helped a bit, I find crude drawings help quite a lot when trying to understand pointers and memory in C++. Number Double07 · May 20, 2015 Michael!!!  I haven't covered pointers yet, geez!  lol, just kidding, I'm not mad, I'll just start reading over pointers tonight so I can understand your pretty artwork.  I'm inspired now.Oh, and sorry, I realize the code did not paste correctly, I will resend it when I get home so you guys can try out my initial jacked up code, lol.  I need more work with putting  returning chars via a function.  Like I said earlier, I was able to get my code to work returning an int from a function for the letterGrade and then using static_cast to convert the ints to char in the main function.  But, I'd like to know how to do it the original way I intended to too.  1 more thing, the null character is an unprintable character, so I would not see anything if I printed it out.  The @ symbol is Ascii code 64, 1 code away from one of my target ascii code 65 which stands for 'A'.  Coincedence or not, hmmmmmm.Yea buddy,Gucci Mane Number Double07 · May 17, 2015 Thanks bro!  You absolutely helped me!  The string works very well, but also, I now realize that if i return an int instead of a char in my second function and then static_cast the ints in my main function using the ascii code for the letter grades, I can get the letter grades that way as well.  ``int letterGrade (float x){    int Grade;    if (x >= 90)        Grade =65;    else if (x >= 70 && x < 90)        Grade = 66;    else if (x >= 50 && x < 70)        Grade = 67;    else        Grade = 70;    return Grade;}``in main function, i simply use static_cast(Grade).  However, I still wonder why the char function didn't work though?  I still do not understand why I was only returning @ for my letter grades. sumeet prasad · May 18, 2015 buddy i suggest you to put the prototypes before using namespace std;oh simpy dont use it since you are putting your function before main. it might solve your problem. if not then let me know..ill help you out.
• 1
• 2

## C++

129,920 followers