Home > Net >  my while loops and else if loops dont work
my while loops and else if loops dont work

Time:10-04

i am trying to have the letter show with the appropriate grade. then i would like the program to ask me over and over my midterm and my final score. then give the appropriate grade for that score as of now it only give me the scores of the first run. here is my code i'm a nub sorry im dumb.

enter code here#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int midterm;
    int final;


    cout << "please enter midterm grade: ";
    cin >> midterm;
    while (midterm < 0 || midterm > 200)
    {
        cout << "Please enter valid amount must be 0 or greater or 200 or less. please enter grade: ";
        cin >> midterm;
    }
    cout << " please enter final grade: ";
    cin >> final;
    while (final < 0 || final > 200)
    {
        cout << "Please enter valid amount must be 0 or greater or 200 or less. Please enter grade";
        cin >> final;
    }
    
    int total;
    total = final   midterm;
    cout << "total";

    if (total > 360 || total <=400)
    {
        cout << "your letter grade is A";
    }   
    else if (total > 320 || total < 359)
    {
        cout << "your letter grade is B";
    }
    else if (total > 280 || total < 319)
    {
        cout << "your letter grade is C";
    }
    else if (total > 279 || total < 241)
    {
        cout << "your letter grade is D";
    }
    else if (total < 240)
    {
        cout << "your letter is F";
        
    }
    

}

CodePudding user response:

As mentioned in the comments, you need to use the && operator instead of ||. || Says if any of my sides is true, the whole expression is true. which means: (true || true) => true. (true || false) => true. (false || true) => true.

so now lets take the first expression in the if else statements with a total of 1 for example and evaluate the expression.

(total > 360 || total <=400)
(1 > 360 || 1 <= 400)
(false || true)
(true)

Thats not the intended behaviour as you want a number between those two values, lets swap it with && which requires both sides to be true to be evaluated to true.

(total > 360 && total <=400)
(1 > 360 && 1 <= 400)
(false && true)
(false)

Now thats what you tried to achieve, the program will continue to the next if else.

CodePudding user response:

Fixed few logical errors in if statements and few other errors (explanation is at the end). I hope this modified code does the required task:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int midterm=0;
    int final=0;

    cout << "please enter midterm grade: ";
    cin >> midterm;
    while (midterm < 0 || midterm > 200)
    {
        cout << "Please enter valid amount must be 0 or greater or 200 or less. please enter grade: ";
        cin >> midterm;
    }
    cout << "please enter final grade: ";
    cin >> final;
    while (final < 0 || final > 200)
    {
        cout << "Please enter valid amount must be 0 or greater or 200 or less. Please enter grade";
        cin >> final;
    }

    int total;
    total = final   midterm;
    cout << "total: "<<total << endl;  

    if (total > 360 && total <= 400)
    {
        cout << "your letter grade is A";
    }
    else if (total > 320 && total <= 360)
    {
        cout << "your letter grade is B";
    }
    else if (total > 280 && total <= 320)
    {
        cout << "your letter grade is C";
    }
    else if (total > 240 && total <= 280)
    {
        cout << "your letter grade is D";
    }
    else if (total <= 240)
    {
        cout << "your letter is F";
    }
}

Sample Output:

please enter midterm grade: 100
please enter final grade: 150
total: 250
your letter grade is D

For explanation, I haven't done much, I just replaced the || inside if statements with && and also modified the values so that no value is left out and also changed cout << "total" to cout << "total: "<<total << endl; so that it shows the output and prints next output (grade) in next line and i also initialised the variables with 0

  • Related