Home > Software design >  How to properly let the user enter a grade in the first output and show the second output base?
How to properly let the user enter a grade in the first output and show the second output base?

Time:12-03

#include <iostream>
using namespace std;

int main(){
    int grade;
    cout << ("Enter a grade input : ");
    cin >> grade;
    
    if (grade >= 94 && grade <= 100){
        cout << "Your output grade is: 1.00";
    }   
    if (grade >= 88.50 && grade <= 93.39){
        cout << "Your output grade is: 1.25";
    }
    if (grade >= 83 && grade <= 88.49){
        cout << "Your output grade is: 1.50";
    }
    if (grade >= 77.5 && grade <= 82.99){
        cout << "Your output grade is: 1.75";
    }
    if (grade >= 72 && grade <= 77.49){
        cout << "Your output grade is: 2.00";
    }    
    if (grade >= 65.5 && grade <= 71.99){
        cout << "Your output grade is: 2.25";
    }
    if (grade >= 61 && grade <= 65.49){
        cout << "Your output grade is: 2.50";
    }
    if (grade >= 55.5 && grade <= 60.99){
        cout << "Your output grade is: 2.75";
    }
    if (grade >= 50 && grade <= 55.49){
        cout << "Your output grade is: 3.00";
    }
    else if (grade > 100 && grade < 0) 
        cout << (" ! Wrong data ");
}

That's my code, it is a selection control structure using if() condition. My problem is I want to input the grades from 100 to 55.49 and the output is displaying the grades 100 to 55.49, and the second output shows the real grade like, the equivalent of the 94 grade is 1.00. If I type less than 0 and greater than 100 it shows the output "Wrong Data".

CodePudding user response:

First off, you are reading the input using an int, which can't handle floating-point values. For example, if you typed in 55.49, you would receive only 55, leaving .49 in the input buffer waiting to be read. You need to use float or double instead of int.

After fixing that, there are still some other problems.

if (grade > 100 && grade < 0) will always be false, as a number can't be both "greater than 100" AND "less than 0". You need to the || (logical OR) operator, not the && (logical AND) operator.

Also, there is a gap in your logic where grades 93.4-93.99 and < 50 are being ignored. In fact, your ifs can be simplified quite a bit.

Try this instead:

#include <iostream>
using namespace std;

int main(){
    double grade;
    cout << "Enter a grade input : ";
    if (!(cin >> grade)){
        cout << " ! Bad data ";
    }
    else if (grade > 100 || grade < 0){
        cout << " ! Wrong data ";
    }
    else if (grade >= 94){
        cout << "Your output grade is: 1.00";
    }   
    else if (grade >= 88.5){
        cout << "Your output grade is: 1.25";
    }
    else if (grade >= 83){
        cout << "Your output grade is: 1.50";
    }
    else if (grade >= 77.5){
        cout << "Your output grade is: 1.75";
    }
    else if (grade >= 72){
        cout << "Your output grade is: 2.00";
    }    
    else if (grade >= 65.5){
        cout << "Your output grade is: 2.25";
    }
    else if (grade >= 61){
        cout << "Your output grade is: 2.50";
    }
    else if (grade >= 55.5){
        cout << "Your output grade is: 2.75";
    }
    else if (grade >= 50){
        cout << "Your output grade is: 3.00";
    }
    else{
        cout << ...; // <-- add something here!
    }
}

CodePudding user response:

A simple and short solution can be like this:

#include <iostream>

int main( )
{
    double grade;
    std::cout << "Enter a grade input: ";
    std::cin >> grade;

    if ( grade > 100 || grade < 50 )
    {
        std::cout << "Wrong data!\n";
    }
    else
    {
        std::cout << "Your output grade is: ";
    }

    if ( grade >= 94 && grade <= 100 )
    {
        std::cout << "1.00";
    }
    else if ( grade >= 88.50 && grade <= 93.39 )
    {
        std::cout << "1.25";
    }
    else if ( grade >= 83 && grade <= 88.49 )
    {
        std::cout << "1.50";
    }
    else if ( grade >= 77.5 && grade <= 82.99 )
    {
        std::cout << "1.75";
    }
    else if ( grade >=72 && grade <= 77.49 )
    {
        std::cout << "2.00";
    }
    else if ( grade >= 65.5 && grade <= 71.99 )
    {
        std::cout << "2.25";
    }
    else if ( grade >= 61 && grade <= 65.49 )
    {
        std::cout << "2.50";
    }
    else if ( grade >= 55.5 && grade <= 60.99 )
    {
        std::cout << "2.75";
    }
    else if ( grade >= 50 && grade <= 55.49 )
    {
        std::cout << "3.00";
    }

    return 0;
}

CodePudding user response:

Not really a direct answer to your question, but having a lot of if's is fragile code (hard to maintain, it is easy to make a small typo and it will be hard to find etc.. etc..) Learn to look for repeating patterns in your code and try to replace them with functions. The for loops in this code are range based : https://en.cppreference.com/w/cpp/language/range-for. const means the program cannot change the value of the variable.

#include <iostream>
#include <array>

// put all the values at which your function output changes into an array of doubles    
std::array<double, 8> threshold_values{ 94.0, 88.5, 83.0, 77.5, 72.0, 65.5, 61.0, 55.5 }; 

// looking at your code, grade starts at 1.0 and increases by 0.25 at each
// threshold value
const double base_grade = 1.0;
const double grade_step = 0.25;

// so now we need to make a function that uses the real information
// from data above to calculate a grade.

// version of get_grade that will explain itself while it runs.
double get_grade_verbose(const double input)
{
    double grade = base_grade;
    for (const auto& threshold : threshold_values)
    {
        if (input > threshold)
        {
            std::cout << "input : " << input << " is greater then " << threshold << ", grade will be : " << grade << "\n";
            return grade;
        }
        
        std::cout << "input : " << input << " was smaller then " << threshold << " so increasing grage to ";
        grade  = grade_step;
        std::cout << grade << "\n";
    }

    return grade;
}

// version without output    
double get_grade(const double input)
{
    double grade = base_grade;

    for (const auto& threshold : threshold_values)
    {
        if (input > threshold) return grade; // done
        grade  = grade_step;
    }

    return grade;
}


int main()
{
    double input = 84.0;

    if ((input >= 0.0 ) && (input <= 100.0))
    {
        std::cout << "grade = " << get_grade(input) << "\n";
    }
    else
    {
        std::cout << "invalid input\n";
    }

    return 0;
}
  •  Tags:  
  • c
  • Related