Search code examples
c#loopsmessageboxdo-while

C# Do while loop


I'm new to programming. I want to create a program that adds 10 integers and will display a message box indicating how many correct answers i got. I just used 1 label box and a button. Here's the code I used. It's working perfectly until the message box shows me that I only got 1 correct answer. What am I doing wrong? Thank you.

    }
    int a;
    int b; 
    string sagot;

    private void button1_Click(object sender, EventArgs e)
    {
        int i = 1;
        do
        {
            a = i + i;
            label1.Text = i.ToString() + " + " + i.ToString() + "=";
            sagot = Interaction.InputBox("Please type your answer:");
            i++;

        } while (i <= 10);

        {
            if (a == Convert.ToInt32(sagot))
                    {
                    b++;
                    }
                }
                MessageBox.Show("Number of correct answers: " + b.ToString());
            }
        }
        }

Solution

  • Lets look at your code:

    int a; 
    int b;  
    string sagot; 
    
    private void button1_Click(object sender, EventArgs e) 
    { 
        int i = 1; 
        do 
        { 
            a = i + i; 
            label1.Text = i.ToString() + " + " + i.ToString() + "="; 
            sagot = Interaction.InputBox("Please type your answer:"); 
            i++; 
    
        } while (i <= 10); 
    
        { 
            if (a == Convert.ToInt32(sagot)) 
                    { 
                    b++; 
                    } 
                } 
                MessageBox.Show("Number of correct answers: " + b.ToString()); 
            } 
        } 
        } 
    

    So.

    You start at 1, and a= i*2, you ask them to enter i+i. you get their response, and increment i.

    First issue is here.. You arent testing their response!! Once you go round the loop again, a and "sagot" is no longer remembered to test for later, so if you dont test now.. its gone.

    Once you do that once i=10 it will come out the loop ..

    So..

    Now you ask it, if a is the same as the answer they gave, add one to b

    well you only do that once.. and it only applies to the last answer..

    Therefore, yes, you will get a score of 1 out of 10.....

    You have a few to many braces. However..

    So, having told you where you went wrong.. and no Im not going to point out any issues such as if you write "FRED" as an answer.. your app will barf..

    Heres your code shuffled to get what you meant it to do - but in your way of doing it - not mine.

    int a; 
    int b;  
    string sagot; 
    
    private void button1_Click(object sender, EventArgs e) 
    { 
        int i = 1; 
        do 
        { 
            a = i + i; 
            label1.Text = i.ToString() + " + " + i.ToString() + "="; 
            sagot = Interaction.InputBox("Please type your answer:"); 
            i++; 
                 if (a == Convert.ToInt32(sagot)) 
                    { 
                    b++; 
                    } 
        } while (i <= 10); 
    
                MessageBox.Show("Number of correct answers: " + b.ToString());