0

I am writing a program to calculate the grade of 3 test scores. The lowest of the first 2 scores is dropped and added to the third test score to make the final grade. The 3 test scores cannot be higer than 50, lower than 0 and cannot be a character or string. So far, I have satisified all those requirment but I need to implement decimal grades to the program like for instance 45.5. Also to round the final grade up or down. For example if final grade is 89.5 round up to an A.

#include <iostream>
#include <algorithm>
#include <cstdlib>

using namespace std;

char getGrade(int num) {
  if (num < 60)
    return 'F';
  if (num < 69)
    return 'D';
  if (num < 79)
    return 'C';
  if (num < 89)
    return 'B';
  return 'A';
}

bool isnumeric(string temp) {
  for (char &chr : temp) {

    if ((chr >= '0' and chr <= '9') or chr == '-')
      continue;
    else
      return false;
  }

  return true;
}

int main(int argc, char const *argv[]) {
  cout << "Welcome to the grade calculator.You will input three test "
          "scores.\nThe highest of the first two grades and the third grade "
          "will be\nadded together to determine the numeric grade average for "
          "the\ncourse.Each test score has a maximum of 50 points.\n";

  int arr[3];
  int ctr = 0;
  string temp;
  int num;
  while (ctr < 3) {
    cout << "\nPlease enter test score " << (ctr + 1) << ":  ";
  label1:
    cin >> temp;
    if (isnumeric(temp)) {
      num = atoi(temp.c_str());

      if (num > 50) {
        cout << "\nTest scores cannot be higher than 50, try again: ";
        goto label1;

      } else if (num < 0) {
        cout << "\nTest scores cannot be negative, try again: ";
        goto label1;

      } else {
        arr[ctr++] = num;
      }

    } else {
      cout << "\nInvalid test score entered, try again: ";
      goto label1;
    }
  }

  int average = 0;
  average = max(arr[0], arr[1]);

  average = average + arr[2];

  cout << "\nThe average for the course = " << average << "\n";
  cout << "The letter grade = " << getGrade(average);
  cout << "\n\n\nThank you for using this program\n";

  return 0;
}
Biffen
  • 6,249
  • 6
  • 28
  • 36
  • 4
    You explained what you did and what you have to do, but what is your specific question? – 463035818_is_not_an_ai Mar 07 '22 at 15:33
  • 1
    `goto label1;` -- Use a `while` loop, not `goto`. The second thing is that `std::stoi` does everything your `isnumeric` does, *plus* converts the string to an integer. Thus there would be no need for `atoi`. – PaulMcKenzie Mar 07 '22 at 15:46

1 Answers1

3

Just changed a couple of things to make it work with decimals:

1. Added chr == '.' to the isNumeric() function:

bool isnumeric(string temp) {

    for (char& chr : temp) {

        if ((chr >= '0' and chr <= '9') or chr == '-' or chr == '.')
            continue;
        else return false;
    }

    return true;
}

2. Changed variable types:

double arr[3]{};
int ctr = 0;
std::string temp;
double num;

3. Removed goto: (You can just use continue)

while (ctr < 3) {

    std::cout << "\nPlease enter test score " << (ctr + 1) << ":  ";

    std::cin >> temp;
    if (isnumeric(temp)) {
        num = atof(temp.c_str());

        if (num > 50) {
            std::cout << "\nTest scores cannot be higher than 50, try again: ";
            continue;

        }
        else if (num < 0) {
            std::cout << "\nTest scores cannot be negative, try again: ";
            continue;

        }
        else {
            arr[ctr++] = num;
        }

    }
    else {
        std::cout << "\nInvalid test score entered, try again: ";
        continue;
    }
}

4. For rounding off, you can use std::round() as such:

double average = 0;
average = std::max(arr[0], arr[1]);

average = std::round(average + arr[2]);

You can also change your cout statements:

std::cout << "\nThe average for the course = " << average;
if (std::round(average) != average) std::cout << ", rounded off to = " << std::round(average);
std::cout << ".\nThe letter grade = " << getGrade(average);
std::cout << "\n\n\nThank you for using this program\n";

Just make all these changes and your program will successfully work with decimals.

Also, consider not using the following in your code:

using namespace std;

..as it's considered as a bad practice. For more info on why, look up to Why is using namespace std considered as a bad practice.

Edit: To accomplish your requirement, you can just change the while loop as such:

while (ctr < 3) {

    if (temp.size() == 0)
    {
        std::cout << "\nPlease enter test score " << (ctr + 1) << ":  "; 
        std::cin >> temp;
    }
    
    if (isnumeric(temp)) {
        num = atof(temp.c_str());

        if (num > 50) {
            std::cout << "\nTest scores cannot be higher than 50, try again: ";
            std::cin >> temp;
            continue;

        }
        else if (num < 0) {
            std::cout << "\nTest scores cannot be negative, try again: ";
            std::cin >> temp;
            continue;

        }
        else {
            arr[ctr++] = num;
            temp.clear();
        }

    }
    else {
        std::cout << "\nInvalid test score entered, try again: ";
        std::cin >> temp;
        continue;
    }
}

The above code works as you said.

The Coding Fox
  • 1,488
  • 1
  • 4
  • 18