Click here to Skip to main content
16,018,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey, I'm about a week into coding and am quite confused, I am getting an error on the lettergrade variable that says its unassigned, and I'm not sure how to fix it. Thanks yall!

What I have tried:

C#
<pre>

float exams = float.Parse(txtboxexams.Text.Trim());
            float termpaper = float.Parse(txtboxtermpaper.Text.Trim());
            

            float examgrade;
            float termpapergrade; 



            examgrade = exams / 250;
            termpapergrade = termpaper / 50;

            string lettergrade;


            if (radbutunder.Checked == true)
            {
                if (examgrade >= 0.93)
                {
                    if (termpapergrade >= 0.93)
                    {
                        lettergrade = "A";
                    }
                    else lettergrade = "A-";
                }
                else if (examgrade >= 0.9)
                {
                    if (termpapergrade >= 0.9)
                    {
                        lettergrade = "A-";
                    }
                    else lettergrade = "B+";
                }
                else if (examgrade >= 0.87)
                {
                    if (termpapergrade >= 0.87)
                    {
                        lettergrade = "B+";
                    }
                    else lettergrade = "B";
                }
                else if (examgrade >= 0.83)
                {
                    if (termpapergrade >= 0.83)
                    {
                        lettergrade = "B";
                    }
                    else lettergrade = "B-";
                }
                else if (examgrade >= 0.8)
                {
                    if (termpapergrade >= 0.8)
                    {
                        lettergrade = "B-";
                    }
                    else lettergrade = "C+";
                }
                else if (examgrade >= 0.77)
                {
                    if (termpapergrade >= 0.77)
                    {
                        lettergrade = "C+";
                    }
                    else lettergrade = "C";
                }
                else if (examgrade >= 0.73)
                {
                    if (termpapergrade >= 0.73)
                    {
                        lettergrade = "C";
                    }
                    else lettergrade = "C-";
                }
                else if (examgrade >= 0.7)
                {
                    if (termpapergrade >= 0.7)
                    {
                        lettergrade = "C-";
                    }
                    else lettergrade = "D+";
                }
                else if (examgrade >= 0.67)
                {
                    if (termpapergrade >= 0.67)
                    {
                        lettergrade = "D+";
                    }
                    else lettergrade = "D";
                }
                else if (examgrade >= 0.63)
                {
                    if (termpapergrade >= 0.63)
                    {
                        lettergrade = "D";
                    }
                    else lettergrade = "D-";
                }
                else if (examgrade >= 0.6)
                {
                    if (termpapergrade >= 0.6)
                    {
                        lettergrade = "D-";
                    }
                    else lettergrade = "F";
                }

                lblfinalgrade.Text = lettergrade;
Posted
Updated 14-Oct-22 15:43pm

1 solution

You have a very large outer if / else if missing an else, so not all paths result in initialising the lettergrade variable. Initializing with an empty string should resolve it:
C#
lettergrade = "";

A side comment: I would suggest simplifying your tests.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900