-1

I keep getting this error and do not understand why something is not returning. I have attempted changing some things around but it does not seem to do anything. I told the program to return 0 back to the user but for some reason this is still an issue. Any help would be greatly appreciated. Thanks!

Code:

#include <iostream>
using namespace std;

char grade(int x)
{
    if (x >= 90) {
        return 'A';
    }
    if (x >= 80) {
        return 'B';
    }
    if (x >= 70) {
        return 'C';
    }
    if (x >= 60) {
        return 'D';
    }
    if (x >= 0) {
        return 'F';
    }
}
int main(void)
{
    int s_grade; // student grade
    cout << "What is the score: ";
    cin >> s_grade;
    cout <<"A score of " << s_grade << " is equal to a grade of " <<
        grade(s_grade) << endl;

    return 0;

}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Josh
  • 1
  • 1
  • 1
    [`using namespace std;` is a bad practice](https://stackoverflow.com/q/1452721/2176813), never use it. Additionally, in C++ you don't have to put `void` in the argument list, if the function has no arguments (not having it is usually considered more idomatic) – tambre Oct 06 '17 at 15:24
  • 5
    What does `grade` return if `x` is less than zero? – 1201ProgramAlarm Oct 06 '17 at 15:25
  • 1
    if none of your `if`s execute you dont return anything – 463035818_is_not_an_ai Oct 06 '17 at 15:25
  • 1
    offtopic: when the conditions are mutually exclusive it is better to use `if - else if` instead of only `if`s. Why better? Because when I read your code the first time I didnt expect them to be mutually exclusive: `x>=90` and `x>=80` are not mutually exclusive and only the fact that you return makes them such, but to see this one has to go one level of nesting deeper – 463035818_is_not_an_ai Oct 06 '17 at 15:39

6 Answers6

3
char grade(int x) {
    if (x >= 90) {
        return 'A';
    }
    if (x >= 80) {
        return 'B';
    }
    if (x >= 70) {
        return 'C';
    }
    if (x >= 60) {
        return 'D';
    }
    if (x >= 0) {
        return 'F';
    }
}

If x is less than 0, this function won't hit a return statement. In C++, this is undefined behavior (but, like you observed, a compiler can emit a warning).

One solution is to do this:

char grade(int x) {
    if (x >= 90) {
        return 'A';
    }
    if (x >= 80) {
        return 'B';
    }
    if (x >= 70) {
        return 'C';
    }
    if (x >= 60) {
        return 'D';
    }
    if (x >= 0) {
        return 'F';
    }
    return 'X';
}

And then your code could interpret an 'x' as an invalid response, resulting from an invalid grade being passed in.

Xirema
  • 19,889
  • 4
  • 32
  • 68
2

The reason of the warning is using a wrong type of the parameter.:)

You declared the parameter as having type int. It means that the parameter can accept not only non-negative values but also negative values in the range

[std::numeric_limits<int>::min(), std::numeric_limits<int>::max()]

or if to use header <climits> in the range

[INT_MIN, INT_MAX]

However the function analyzes only non-negative values.

char grade(int x)
{
    if (x >= 90) {
        return 'A';
    }
    if (x >= 80) {
        return 'B';
    }
    if (x >= 70) {
        return 'C';
    }
    if (x >= 60) {
        return 'D';
    }
    if (x >= 0) {
        return 'F';
    }
}

So if a negative value will be passed to the function then the function has undefined behavior because as the warning says

control reaches end of non-void function

The compiler can not know what values of arguments will be supplied to the function.

So you should declare the parameter as having the type unsigned int.

In this case the function can look like

char grade( unsigned int x)
{
    if (x >= 90) {
        return 'A';
    }
    if (x >= 80) {
        return 'B';
    }
    if (x >= 70) {
        return 'C';
    }
    if (x >= 60) {
        return 'D';
    }
    if (x >= 0) {
        return 'F';
    }
}

or the following way

char grade( unsigned int x)
{
    if (x >= 90) {
        return 'A';
    }
    else if (x >= 80) {
        return 'B';
    }
    else if (x >= 70) {
        return 'C';
    }
    else if (x >= 60) {
        return 'D';
    }
    else /* x >= 0 */{
        return 'F';
    }
}

And in main the variable s_grade also should be declared like

unsigned int s_grade;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

Your function char grade(int x) claims it will return a char, but after the last if it has no return statement. For example, if i =-1, it will not return anything.

Aganju
  • 6,295
  • 1
  • 12
  • 23
0

It looks as if your function char grade(int x), which you have defined to return a value, can in some circumstances not do so. The compiler will check, so far as it can, that there is no path of execution that results in the function returning without specifying a value. It doesn't really matter whether you -- as the programmer -- know this won't happen.

You need to ensure that all possible values of x result in value being returned, or use a pragma or similar to relax the compiler's checks.

Kevin Boone
  • 4,092
  • 1
  • 11
  • 15
0

This warning is similar to the warning described in "Return with no value". If control reaches the end of a function and no return is encountered, GCC assumes a return with no return value. However, for this, the function requires a return value. At the end of the function, add a return statement that returns a suitable return value, even if control never reaches there.

https://www.ibm.com/support/knowledgecenter/en/SSB23S_1.1.0.14/common/m1rhnvf.html

char grade(int x) {
    if (x >= 90) {
        return 'A';
    }
    if (x >= 80) {
        return 'B';
    }
    if (x >= 70) {
        return 'C';
    }
    if (x >= 60) {
        return 'D';
    }

   return 'F'; 
}
Pritesh Patel
  • 678
  • 17
  • 35
0

You can change the last if statement to else and things should work fine.

else return 'F'; instead of

if (x >= 0) { return 'F'; } Checking for negative values can be handled in main itself.

afsd
  • 152
  • 1
  • 9