Click here to Skip to main content
15,903,385 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: CRichEditCtrl interface question [modified] Pin
enhzflep6-May-12 10:14
enhzflep6-May-12 10:14 
GeneralRe: CRichEditCtrl interface question Pin
enhzflep6-May-12 11:01
enhzflep6-May-12 11:01 
GeneralRe: CRichEditCtrl interface question Pin
ForNow7-May-12 7:24
ForNow7-May-12 7:24 
QuestionCRichEditCtrl FindText problem Pin
ForNow5-May-12 20:24
ForNow5-May-12 20:24 
AnswerRe: CRichEditCtrl FindText problem Pin
enhzflep5-May-12 21:44
enhzflep5-May-12 21:44 
GeneralRe: CRichEditCtrl FindText problem Pin
ForNow6-May-12 5:41
ForNow6-May-12 5:41 
GeneralRe: CRichEditCtrl FindText problem Pin
enhzflep6-May-12 5:54
enhzflep6-May-12 5:54 
QuestionLooping when you need user input Pin
abollmeyer5-May-12 19:25
abollmeyer5-May-12 19:25 
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.

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#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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


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

        public void shuffle(ref int[] array)
        {
            Random rng = new Random();       // i.e., java.util.Random. 
            int count = array.Length;        // The number of items left to shuffle (loop invariant). 

            while (count > 1)
            {
                int k = rng.Next(count);     // 0 <= k < n. 
                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()
        {
            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);

            //int counter = 0;
            addendOneLabel.Text = numbers[questionCounter].ToString();
            addendTwoLabel.Text = numbers[questionCounter + 1].ToString();
        }

        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);          
        }  

        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 when you need user input Pin
Code-o-mat5-May-12 23:08
Code-o-mat5-May-12 23:08 
GeneralRe: Looping when you need user input Pin
abollmeyer6-May-12 3:37
abollmeyer6-May-12 3:37 
GeneralRe: Looping when you need user input Pin
enhzflep6-May-12 4:15
enhzflep6-May-12 4:15 
GeneralRe: Looping when you need user input Pin
abollmeyer6-May-12 7:32
abollmeyer6-May-12 7:32 
GeneralRe: Looping when you need user input Pin
enhzflep6-May-12 7:40
enhzflep6-May-12 7:40 
GeneralRe: Looping when you need user input Pin
Code-o-mat7-May-12 4:03
Code-o-mat7-May-12 4:03 
GeneralWelcome Pin
Code-o-mat5-May-12 23:11
Code-o-mat5-May-12 23:11 
Question64 bit problem Pin
appollosputnik4-May-12 4:41
appollosputnik4-May-12 4:41 
AnswerRe: 64 bit problem Pin
Albert Holguin4-May-12 9:58
professionalAlbert Holguin4-May-12 9:58 
AnswerRe: 64 bit problem Pin
«_Superman_»4-May-12 16:43
professional«_Superman_»4-May-12 16:43 
QuestionHelp for algorithm Pin
Falconapollo3-May-12 23:53
Falconapollo3-May-12 23:53 
QuestionRe: Help for algorithm Pin
Maximilien4-May-12 2:38
Maximilien4-May-12 2:38 
AnswerRe: Help for algorithm Pin
«_Superman_»4-May-12 3:15
professional«_Superman_»4-May-12 3:15 
GeneralRe: Help for algorithm Pin
Falconapollo5-May-12 20:12
Falconapollo5-May-12 20:12 
AnswerRe: Help for algorithm Pin
Albert Holguin4-May-12 10:12
professionalAlbert Holguin4-May-12 10:12 
GeneralRe: Help for algorithm Pin
Falconapollo5-May-12 20:29
Falconapollo5-May-12 20:29 
QuestionRe: Help for algorithm Pin
CPallini4-May-12 10:47
mveCPallini4-May-12 10:47 

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.