2

In my programming class, we are learning about Arraylists and structs. We were given an assignment where we would store multiple instances of the struct in the arraylist. The struct store a max value and a letter grade that corresponds to that value. For example, I create a struct called F. Its letter is F, and its max value is 299. Lets say I input 399 into the program. That should return C as the grade, but instead it returns F. I'm really unsure of what is causing this problem. Any help would be appreciated.

namespace StructAssignment
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        ArrayList Grades = new ArrayList();

        public struct Grade
        {
             public char letter;
             public int maxPoints;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Grade A;
            A.letter = 'A';
            A.maxPoints = 299;
            Grades.Add(A);

            Grade B;
            B.letter = 'B';
            B.maxPoints = 349;
            Grades.Add(B);

            Grade C;
            C.letter = 'C';
            C.maxPoints = 399;
            Grades.Add(C);

            Grade D;
            D.letter = 'D';
            D.maxPoints = 449;
            Grades.Add(D);

            Grade F;
            F.letter = 'F';
            F.maxPoints = 500;
            Grades.Add(F);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string grade = string.Empty;

            if(int.TryParse(textBox1.Text, out int points))
            {
                if(points < 0 || points > 500)
                {
                    MessageBox.Show("Please enter a number greater than 0 and less than 500");
                }
                else
                {
                    foreach(Grade gr in Grades)
                    {
                        if(points < gr.maxPoints)
                        {
                            grade = gr.letter.ToString();
                        }
                    }

                    MessageBox.Show(grade);
                }
            }
            else
            {
                MessageBox.Show("Please enter a valid number!");
            }
        }
    }
}
stuartd
  • 70,509
  • 14
  • 132
  • 163
  • 2
    399 is less than 500, so it will return F. Your loop doesn't do anything when the condition is met other than assign the value. That means it will evaluate the next entry in Grades, too, and so on. – ProgrammingLlama Apr 25 '18 at 01:21
  • I recommend setting a breakpoint (F9) on the line `if (points < gr.maxPoints)` and then using F11 to step through your program with the debugger, so that you can understand how it is being executed. – ProgrammingLlama Apr 25 '18 at 01:24

2 Answers2

3

I think you need to break the loop once the condition is true. and You should use <= condition to match exact number user enters.

foreach(Grade gr in Grades)
{
   if(points <= gr.maxPoints)
   {
      grade = gr.letter.ToString();
      break;
   }
}
MessageBox.Show(grade); 
Gaurang Dave
  • 3,956
  • 2
  • 15
  • 34
0

I'm surprised an input of 399 is returning F; the way it is coded I would expect D. You should check for less than or equal to, not just less than.

Jamie Bisotti
  • 2,605
  • 19
  • 23