-2

Hello I am in the middle of creating a program for my C# class that has us make a GUI program that you enter 4 number grades, then average that out, take the average number and then assign a letter grade to it. right now i got everything working except for the letter grade part. Right now I get the CS0161 error, 'Grade_Calculator. Lettergrade (double)'; not all code paths return a value.

`using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace Average_Test_Grades
{
    public partial class Grade_Calculator : Form
    {
        public Grade_Calculator()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, EventArgs e)
        {
            int Num1;
            int Num2;
            int Num3;
            int Num4;


            Num1 = Convert.ToInt32(Input1.Text);
            Num2 = Convert.ToInt32(Input2.Text);
            Num3 = Convert.ToInt32(Input3.Text);
            Num4 = Convert.ToInt32(Input4.Text);

            int sum = Num1 + Num2 + Num3 + Num4;
            int avg = sum / 4;
            Output1.Text = "" + avg;

        }

        char Lettergrade(double avg)
        {
            if (avg >= 90 && avg <= 100)
                return 'A';
            else if (avg >= 80 && avg <= 89)
                return 'B';
            if (avg >= 70 && avg <= 79)
                return 'C';
            if (avg >= 60 && avg <= 69)
                return 'D';
            if (avg >= 50 && avg <= 59)
                return 'F';
        }
    }
    }
`
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
John DeLuca
  • 137
  • 1
  • 2

1 Answers1

0

What do you think happens if you pass in a value of 101 to the Lettergrade function? It falls through all the if statements and doesn't return anything at all. Since your code is expecting a value between 0 and 100, you could throw an exception or you could return a value to indicate an unknown grade.

char Lettergrade(double avg)
{
    if (avg >= 90 && avg <= 100)
        return 'A';
    else if (avg >= 80 && avg <= 89)
        return 'B';
    if (avg >= 70 && avg <= 79)
        return 'C';
    if (avg >= 60 && avg <= 69)
        return 'D';
    if (avg >= 50 && avg <= 59)
        return 'F';

    throw new Exception("Invalid grade was supplied");
    //or
    return '?';
}
DavidG
  • 113,891
  • 12
  • 217
  • 223
  • Not sure why take the time to answer a question with hundreds of duplicates – Camilo Terevinto Apr 18 '18 at 23:52
  • @John DeLuca: adding to @DavidG's answer - the opposite is also true: anything below 50 would be the same problem. But, ANYTHING < 60 is technically an 'F', correct? So, you were on the right track using `else if`. Add the same before the 70 and 60 conditions and then add an else at the end that returns 'F' (no need to check the 50 range). Then a return for `all code paths` will exist, or you can keep the exception shown above. With a `if .. else if .. else if .. else` construct, the conditions that you have can be simplified by removing the `&& avg ...` from the right side of each condition. – Paul T. Apr 19 '18 at 02:09