Simple C++ Need help please!

Started by
8 comments, last by DevFred 15 years, 8 months ago
this is my second ever c++ code so bare with me if its really obvious... ----------------------------------------------- int score; char grade; cout<<"Please enter your score: "; cin>>score; if(score >= 90) { grade='A'; cout<<"You got an A!"; } else { if(score >= 80 && score <= 89) { grade='B'; cout<<"You got a B "; } else { if(score >= 70 && score <= 79) { grade='C'; cout<<"You got a C "; } else { if(score <= 69) { grade='D'; cout<<"You got a D "; } } } } if((grade='A') || (grade='B')) { cout<<"You Aced it! "; } else { if((grade='B') || (grade='C')) { cout<<"You could have done a lot better "; } else { if((grade='C') || (grade='D')) { cout<<"You are a complete Failure! "; } } } ----------------------------------------------- EVERY time I enter a score such as "1", I get "You Aced it!" - well not really... why is this happening?
Advertisement
A single equals sign '=' is assignment, double equals '==' is equality testing. You want to test for equality here.

Code like this:
if(a = 5){    // ...}

What happens is "a" is assigned the value of 5, and then that value is used in the if conditional expression. So from the compilers point of view, you could have written:
a = 5;if(a){   // ...}

The expression "a" is constant, i.e. in this case the branch is always taken. Some compilers will warn you of this with a high warning level. A high warning level is always good.

An example of an unconditionally false condition is "if(a = 0)".
You need to watch out how to compare things:

char grade = 'a';if( grade =<b>=</b> 'a' )    cout >> "You aced it;


When you want to compare things in c++, you need to add a second =, otherwise c++ will do the following:

if( grade = 'a' )// C++ will assign 'a' to grade and then return true


Hope that helps :)
Quote:
if((grade='A') || (grade='B'))
{
cout<<"You Aced it! ";
}
else
{
if((grade='B') || (grade='C'))
{
cout<<"You could have done a lot better ";
}
else
{
if((grade='C') || (grade='D'))
{
cout<<"You are a complete Failure! ";
}
}
}


on these lines you should be using == not =
For the love of god, please tell me that you've just omitted your error checking code for brevity, and you don't really assume that all those functions succeed.
Just skimming over your code your only using one = instead of two ==
Thankyou all i'll try that and get back to you if your interested :) Can;t try it right now.

Thanks again
Besides converting all '=' to '==' there are two other small issues.

The first issue is a matter of english. You "ace" something when you get an 'A', not when you get a 'B'.

The second issue has to do with logic. If you get a 'B', you will always hit the first branch of your if statement, never the second branch. I would suggest you remove the check for a 'B' grade from the first if condition entirely as well as vastly simplify your if-statement:

#include <cassert>...if(grade=='A'){     cout<<"You Aced it! ";}else if( (grade=='B') || (grade=='C') ){     cout<<"You could have done a lot better ";}else if(grade=='D'){     cout<<"You are a complete Failure! ";}else{     assert(false);}


[Edited by - fpsgamer on August 15, 2008 4:43:27 PM]
Quote:Thread title
Simple C++ Need help please!


Quote:Original post by rosshildick
this is my second ever c++ code so bare with me if its really obvious...




It's at the top of the forum list for a reason: to be noticed by people who know their question is "for beginners".
Moved.
Quote:Original post by rip-off
What happens is "a" is assigned the value of 5, and then that value is used in the if conditional expression.

Of course there are corner cases where you have to be more specific:
#include <stdio.h>int main(void){    char c;    if (c = 256)        printf("assignment yielded true");    else        printf("assignment yielded false");}

This topic is closed to new replies.

Advertisement