Sign in to follow this  
rosshildick

Simple C++ Need help please!

Recommended Posts

rosshildick    109
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?

Share this post


Link to post
Share on other sites
rip-off    10979
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)".

Share this post


Link to post
Share on other sites
SiS-Shadowman    359
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 :)

Share this post


Link to post
Share on other sites
Cambo_frog    855
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 =

Share this post


Link to post
Share on other sites
fpsgamer    856
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]

Share this post


Link to post
Share on other sites
Zahlman    1682
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".

Share this post


Link to post
Share on other sites
DevFred    840
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");
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this