using namespace std;
float finalScore (int x, int y, int z);
char letterGrade (float x);
float finalScore (int x, int y, int z)
float total_score = static_cast (x+y+z);
float tests = 3;
average = total_score/ tests;
char letterGrade (float x)
if (x >= 90)
char Grade = 'A';
else if (x >= 70 && x < 90)
char Grade = 'B';
else if (x >= 50 && x < 70)
char Grade = 'C';
char Grade = 'F';
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
faverage = finalScore(nquiz, nmid_term, nfinal);
Why can't I get my code to print the correct letter grade for my test scores
· 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.
Post a Reply
Replies- page 1
|Oldest Newest Rating|
· 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.
· May 19, 2015
Also could you repaste your code a lot of it seems to be missing because the <code> tags are interpretting it.
· May 17, 2015
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.
Then, try using string instead of char.
I hope this help!
· 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.
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.
Try it, and the result might be Z.
· 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,
· 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.
· 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++.
· 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.
· 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.
in main function, i simply use static_cast<char>(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.
· 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.
Used in many types of software including music players, video games, and many large scale applications.
|Bucky Roberts Administrator|