Sign in to follow this  
BenThereDoneThat

Problem while testing C++ pregram.

Recommended Posts

Ok, so I'm "re-learning" C++ after a long period of not using it, and now a little grade calculator prgram I've made isn't working. It works when you type in a grade that would be an A or a B, but for anything lower it only shows output for a B. Can anyone give me a pointer here? Here's the code:
#include <iostream>
using namespace std;

int main()
{
    int grade;
    
    cout<< "Please type in your grade: \n";
    cin>> grade;
    cin.ignore();
    if ( grade >= 90) {
         cout<< "Congratulations! You have an A! \n";
         }
         else if ( grade > 80 <= 89 ) {
              cout<< "You have a B! Well done! \n";
              }
              else if ( grade > 70 <= 79 ) {
                   cout<< "You have a C! You're just average... \n";
                   }
                   else if ( grade > 60 <= 69 ) {
                        cout<< "You have a D! You need to work harder... \n";
                        }
                        else {
                             cout<< "You have a F. You fail. \n";
                             }
                             cin.get();
                             }

Share this post


Link to post
Share on other sites
All of your nested if's should include logical AND (&&):

else if ( grade > 80 <= 89 )
Should be:

else if ( grade > 80 && grade <= 89 )

Share this post


Link to post
Share on other sites
The syntax you're using to check domain might be your problem.


if (grade >= 90) {
cout << "You've gotten an A!";
} else if (grade >= 80 && grade < 90) {
cout << "You've gotten a B.";
} else if (grade >= 70 && grade < 80) {
cout << "You've gotten a C...";
} else if (grade >= 60 && grade < 70) {
cout << "You barely passed with a D...";
} else cout << "You suck.";

Share this post


Link to post
Share on other sites
Generally speaking, compound conditionals in C/C++ must each be a complete statement that can be evaluated as 'true' or 'false', and must be joined by logical operators such as && (logical and) and || (logical or). In short, your conditionals are not being evaluated in the way that you expect.

Also, the entries in an if-else sequence should generally be indented by the same amount, reflecting the fact that they're all 'at the same level' in terms of the logical flow of the program.

'Hope that helps.

[Bah.]

Share this post


Link to post
Share on other sites
Ok, now I get an error saying "expected primary expression before '<' token" on all my else-if's. Here's the new code:

#include <iostream>
using namespace std;

int main()
{
int grade;

cout<< "Please type in your grade: \n";
cin>> grade;
cin.ignore();
if ( grade >= 90) {
cout<< "Congratulations! You have an A! \n";
}
else if ( grade >= 80 && < 90 ) {
cout<< "You have a B! Well done! \n";
} else if ( grade >= 70 && < 80 ) {
cout<< "You have a C! You're just average... \n";
} else if ( grade >= 60 && < 70 ) {
cout<< "You have a D! You need to work harder... \n";
}else {
cout<< "You have a F. You fail. \n";
}
cin.get();
}


Share this post


Link to post
Share on other sites
This should work..

#include <iostream>
using namespace std;

int main()
{
int grade;

cout<< "Please type in your grade: \n";
cin>> grade;
cin.ignore();
if ( grade >= 90) {
cout<< "Congratulations! You have an A! \n";
}
else if ( grade >= 80 && grade < 90 ) {
cout<< "You have a B! Well done! \n";
} else if ( grade >= 70 && grade < 80 ) {
cout<< "You have a C! You're just average... \n";
} else if ( grade >= 60 && grade < 70 ) {
cout<< "You have a D! You need to work harder... \n";
}else {
cout<< "You have a F. You fail. \n";
}
cin.get();
}


Notice that I changes all of your else..if expressions from

else if ( grade >= 80 && < 90 ) {

to:

else if ( grade >= 80 && grade < 90 ) {


I fixed your code to the correct expression statements.

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