0

This is the assignment I'm working on:

Write a program that prompts the user to enter the points earned for a course and determines the grade. The program should display the grade and the message string for the grade. Use 'if else' statements to determine the grade and the switch statement to print the message string.

I wrote the code and it executes without any debugging errors. The problem is, no matter what I input as my PointsEarned, I only get the output for F! For example, if I input "90":

Try Harder Next Time.
GRADE: F

What's the problem? Am I putting my switch statement in the wrong place? Here's the code:

#include <iostream>
using namespace std;
int main()
{
    int PointsEarned;
    char Grade;
    cout << "Please input the points earned in the course: ";
    cin >> PointsEarned;
    if (0 <= PointsEarned < 60) {
        Grade = 'F';
    }
    else if (60 <= PointsEarned < 70) {
        Grade = 'D';
    }
    else if (70 <= PointsEarned < 80) {
        Grade = 'C';
    }
    else if (80 <= PointsEarned < 90) {
        Grade = 'B';
    }
    else if (90 <= PointsEarned) {
        Grade = 'A';
    }
    else{
        cout << "That is not a  valid input.";
    }
    switch (Grade)
    {
    case 'F':
    case 'D':
        cout << "Try Harder Next Time." << endl;
        break;
    case 'C':
        cout << "Good." << endl;
        break;
    case 'B':
        cout << "Very good." << endl;
        break;
    case 'A':
        cout << "Excellent." << endl;
        break;
    default:
        cout << "Please choose a valid input to receive your grade." << endl;
    }
    cout << "GRADE: " << Grade << endl;
}
Naomi Jacobson
  • 107
  • 1
  • 1
  • 10

5 Answers5

5

Comparisons of the form 0 <= PointsEarned < 60 work in Python but not C++. You should use 0 <= PointsEarned && PointsEarned < 60.

BingsF
  • 1,269
  • 10
  • 15
4

You can't test multiple values like you are:

if (0 <= PointsEarned < 60) {

This is not checking if PointsEarned is between 0 and 60. Only ONE of those < will be evaluated at a time, which means you're effectively testing something mor elike

if (0 <= true/false) {

instead.

You need something more like

 if ((0 <= PointsEarned) && (PointsEarned < 60)) {

instead.

Marc B
  • 356,200
  • 43
  • 426
  • 500
1

This if (0 <= PointsEarned < 60) { is breaking the C++ language, change that for 2 valid comparation if (0 <= PointsEarned && PointsEarned < 60) {

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

I rewrite your code and done few improvements:in the line else if (90 <= PointsEarned) I limited it to 100.Also the other commenters noted that you can't test multiple values so I rewrite that as they said.It now works fine!

#include <iostream>
using namespace std;
int main()
{
    int PointsEarned;
    char Grade;
    cout << "Please input the points earned in the course: ";
    cin >> PointsEarned;
    if ((0 <= PointsEarned) && (PointsEarned < 60)) {
        Grade = 'F';
    }
    else if ((60 <= PointsEarned) && (PointsEarned < 70)){
        Grade = 'D';
    }
    else if ((70 <= PointsEarned) && (PointsEarned < 80)) {
        Grade = 'C';
    }
    else if ((80 <= PointsEarned) && (PointsEarned < 90)){
        Grade = 'B';
    }
    else if ((90 <= PointsEarned) && (PointsEarned <= 100)) {
        Grade = 'A';
    }
    else{
        cout << "That is not a  valid input.";
    }
    switch (Grade)
    {
    case 'F':
    case 'D':
        cout << "Try Harder Next Time." << endl;
        break;
    case 'C':
        cout << "Good." << endl;
        break;
    case 'B':
        cout << "Very good." << endl;
        break;
    case 'A':
        cout << "Excellent." << endl;
        break;
    default:
        cout << "Please choose a valid input to receive your grade." << endl;
    }
    cout << "GRADE: " << Grade << endl;
}
Zsombi
  • 84
  • 1
  • 9
0

The expression: if (0 <= PointsEarned < 60) means if ((0 <= PointsEarned) < 60)
which means if ((true/false) < 60)

The above expression is not the same as the expression you probably wanted:
if ((0 <= PointsEarned) && (PointsEarned < 60))

Andreas DM
  • 10,685
  • 6
  • 35
  • 62