Problem while testing C++ pregram.

Started by
5 comments, last by BenThereDoneThat 17 years, 1 month ago
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();
                             }

----------------------------My site: www.sudoexec.net
Advertisement
All of your nested if's should include logical AND (&&):
else if ( grade > 80 <= 89 ) 
Should be:
else if ( grade > 80 && grade <= 89 ) 
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.";
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.]
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();                             } 
----------------------------My site: www.sudoexec.net
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.
Thanks for pointing that out! I see now that that was in the other posts, I simply missed it. Lesson learned: pay closer attention to small details.
----------------------------My site: www.sudoexec.net

This topic is closed to new replies.

Advertisement