Click here to Skip to main content
15,885,985 members
Home / Discussions / C#
   

C#

 
AnswerRe: Best practice Pin
BobJanova8-May-12 0:22
BobJanova8-May-12 0:22 
QuestionDictionary object (need logic) in C# Pin
Member 89377326-May-12 23:05
Member 89377326-May-12 23:05 
GeneralRe: Dictionary object (need logic) in C# Pin
harold aptroot7-May-12 0:38
harold aptroot7-May-12 0:38 
AnswerRe: Dictionary object (need logic) in C# Pin
Maciej Los7-May-12 1:22
mveMaciej Los7-May-12 1:22 
GeneralRe: Dictionary object (need logic) in C# Pin
Manfred Rudolf Bihy7-May-12 2:30
professionalManfred Rudolf Bihy7-May-12 2:30 
QuestionVertexBuffer for collection of vertex Pin
ITSparrow6-May-12 6:56
ITSparrow6-May-12 6:56 
AnswerRe: VertexBuffer for collection of vertex Pin
Luc Pattyn6-May-12 10:25
sitebuilderLuc Pattyn6-May-12 10:25 
QuestionLooping based on user input Pin
abollmeyer6-May-12 4:08
abollmeyer6-May-12 4:08 
Hello,

I am working on a math program for my daughter in C# using a Form. Here is the gist of my program:

1) Create random numbers.
2) Add numbers together. Compare to user input.
3) Tell user if question was answered correctly.

I can get the program to cycle through once successfully. I can't figure out how to loop through a series of 50 questions though. When I've tried to loop it, I either end up on question 50 or suffer through an infinite loop.

The idea is to use the 'checkAnswer' button to let the user know if the question was answered correctly and then move on to the next question. However, I have yet to figure out a way to signal to the rest of the program (return value from button click?) that the button has been pressed. I have included the code.

If anyone could point me in the right direction, I would greatly appreciate it. Also, this is the first time I've posted in a programming forum so I've left out any pertinent info please let me know!!!

C#
namespace Math
{
    public partial class NewMathForm : Form
    {
        public NewMathForm()
        {
            InitializeComponent();
            int questionCounter = 1;

            for (; questionCounter < 50; )
            {
                addition(ref questionCounter);
            }
        }


        private void sumTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            sumTextBox.SelectionStart = 0;
        }

        public void shuffle(ref int[] array)
        {
            Random rng = new Random();

            // The number of items left to shuffle (loop invariant).
            int count = array.Length;         

            while (count > 1)
            {
                // 0 <= k < n.
                int k = rng.Next(count);           
                count--;
                // count is now the last pertinent index;
                int temp = array[count];
                // swap array[n] with array[k] (does nothing if k == n).
                array[count] = array[k];
                array[k] = temp;
            }
        }

        private void parseNumbers(ref int[] array)
        {
            double addend1 = 0;
            double addend2 = 0;

            string number;

            char num1;
            char num2;

            for (int count = 0; count < 50; count++)
            {
                number = array[count].ToString();
                num1 = number[0];

                int length = number.Length;

                if (length < 2)
                {
                    char zero = '0';
                    num2 = zero;
                }

                else
                    num2 = number[1];

                addend1 = System.Char.GetNumericValue(num1);
                addend2 = System.Char.GetNumericValue(num2);

                array[count] = Convert.ToInt32(addend1);
                count++;
                array[count] = Convert.ToInt32(addend2);
            }
        }

        private void addition(ref int tickUp)
        {
            int[] numbers = new int[100];
            int counter = 0;
            int questionCounter = 1;
            int numberOfQuestions = 50;

            // Read numbers 0 to 99 into an array.
            for (counter = 0; counter < 100; counter++)
            {
                numbers[counter] = counter;
            }

            // Randomly shuffle the array of numbers
            shuffle(ref numbers);

            // Parse the first 50 numbers into tens and ones digits, then use those numbers for the problem.
            parseNumbers(ref numbers);

            //for (counter = 0; counter < 50; )
            //{
                if (sumTextBox.Text == "")
                {
                    // Write addends to label.
                    addendOneLabel.Text = numbers[questionCounter].ToString();
                    addendTwoLabel.Text = numbers[questionCounter + 1].ToString();
                }

                tickUp++;


           // }
        }

        private void checkAnswerButton_Click(object sender, EventArgs e)
        {
            checkAnswer();
        }

        public void checkAnswer()
        {

            int sum = Convert.ToInt32(sumTextBox.Text);
            int number1 = Convert.ToInt32(addendOneLabel.Text);
            int number2 = Convert.ToInt32(addendTwoLabel.Text);
            int answer = number1 + number2;

             if (answer == sum)
                    correctAnswer();
             else
                    incorrectAnswer(answer);

            sumTextBox.Text = "";
        }  

        private void correctAnswer ()
        {
            MessageBox.Show ("That is correct!");
        }

        private void incorrectAnswer (int sum)
        {
            MessageBox.Show ("That is incorrect! The correct answer is " + sum + ".");
        }

    }
}

AnswerRe: Looping based on user input Pin
Richard MacCutchan6-May-12 4:21
mveRichard MacCutchan6-May-12 4:21 
NewsN Queen algorithm design Pin
ShomaL University of AMOL5-May-12 22:45
ShomaL University of AMOL5-May-12 22:45 
AnswerRe: N Queen algorithm design Pin
Sandeep Mewara5-May-12 23:21
mveSandeep Mewara5-May-12 23:21 
GeneralRe: N Queen algorithm design Pin
Richard MacCutchan6-May-12 1:38
mveRichard MacCutchan6-May-12 1:38 
GeneralMy Vote of 1 PinPopular
Keith Barrow6-May-12 2:30
professionalKeith Barrow6-May-12 2:30 
GeneralRe: N Queen algorithm design Pin
PIEBALDconsult6-May-12 5:35
mvePIEBALDconsult6-May-12 5:35 
QuestionImage Encryption Pin
Danial C5-May-12 21:41
Danial C5-May-12 21:41 
AnswerRe: Image Encryption Pin
Sandeep Mewara5-May-12 23:24
mveSandeep Mewara5-May-12 23:24 
QuestionProblems with PathGeometry Pin
gabriel1235-May-12 18:28
gabriel1235-May-12 18:28 
AnswerRe: Problems with PathGeometry Pin
Sandeep Mewara6-May-12 1:23
mveSandeep Mewara6-May-12 1:23 
Questionchoosing a instalation directory fo aplications Pin
smartradio5-May-12 7:48
smartradio5-May-12 7:48 
AnswerRe: choosing a instalation directory fo aplications Pin
Ravi Bhavnani5-May-12 12:56
professionalRavi Bhavnani5-May-12 12:56 
GeneralRe: choosing a instalation directory fo aplications Pin
smartradio7-May-12 14:00
smartradio7-May-12 14:00 
GeneralRe: choosing a instalation directory fo aplications Pin
Ravi Bhavnani7-May-12 14:09
professionalRavi Bhavnani7-May-12 14:09 
QuestionEvent/Appointment Calendar Pin
danieltm34-May-12 23:32
danieltm34-May-12 23:32 
AnswerRe: Event/Appointment Calendar Pin
Ravi Bhavnani5-May-12 13:01
professionalRavi Bhavnani5-May-12 13:01 
Questionstreaming text on panels (sheets) Pin
Danzy834-May-12 11:08
Danzy834-May-12 11:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.