Click here to Skip to main content
15,904,877 members
Home / Discussions / C#
   

C#

 
AnswerRe: Display Target Platform Pin
Luc Pattyn8-May-12 14:56
sitebuilderLuc Pattyn8-May-12 14:56 
GeneralRe: Display Target Platform Pin
Apocalypse Now8-May-12 15:22
Apocalypse Now8-May-12 15:22 
AnswerRe: Display Target Platform Pin
Bernhard Hiller8-May-12 0:16
Bernhard Hiller8-May-12 0:16 
QuestionBest practice Pin
mo5andes7-May-12 5:42
mo5andes7-May-12 5:42 
AnswerRe: Best practice Pin
Eddy Vluggen7-May-12 5:58
professionalEddy Vluggen7-May-12 5:58 
AnswerRe: Best practice Pin
Not Active7-May-12 6:12
mentorNot Active7-May-12 6:12 
GeneralRe: Best practice Pin
PIEBALDconsult7-May-12 6:14
mvePIEBALDconsult7-May-12 6:14 
GeneralRe: Best practice Pin
Jeremy Hutchinson7-May-12 6:55
professionalJeremy Hutchinson7-May-12 6:55 
GeneralRe: Best practice Pin
Not Active7-May-12 7:29
mentorNot Active7-May-12 7:29 
AnswerRe: Best practice Pin
PIEBALDconsult7-May-12 6:13
mvePIEBALDconsult7-May-12 6:13 
AnswerRe: Best practice Pin
Jeremy Hutchinson7-May-12 7:03
professionalJeremy Hutchinson7-May-12 7:03 
GeneralRe: Best practice Pin
PIEBALDconsult7-May-12 7:38
mvePIEBALDconsult7-May-12 7:38 
GeneralRe: Best practice Pin
Jeremy Hutchinson7-May-12 8:39
professionalJeremy Hutchinson7-May-12 8:39 
GeneralRe: Best practice Pin
Not Active7-May-12 10:14
mentorNot Active7-May-12 10:14 
AnswerRe: Best practice Pin
mo5andes7-May-12 7:14
mo5andes7-May-12 7:14 
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 

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.