Thread: another exercise question

  1. #1
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    another exercise question

    question from book =

    use the next method from an object of type Random to produce 2 positive one digit integers. It should display a question in the status bar, such as: How much is 6 times 7?

    The student should then type the answer into a textbox. Your program should check the students answer. If correct, draw the string "Very Good" in a read only textbox then ask another multiplication question. If the answer is wrong, draw the string "NO, please try again" in the same read only textbox, then let the student try the same question again untill the student finally gets it right. A separate method should be used to generate each new question. This method shoud be called once when the program begins execution and each time the user answers the question correctly.

    the following code is what i have written so far(i have included the windows generated code so you can compile),
    Code:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    
    namespace Ex6_14
    {
    	
    	public class Form1 : System.Windows.Forms.Form
    	{
    		private System.Windows.Forms.StatusBar statusBar1;
    		private System.Windows.Forms.TextBox textBox1;
    		private System.Windows.Forms.Label label1;
    		private System.Windows.Forms.TextBox textBox2;
    		private System.ComponentModel.Container components = null;
    		private Random ranObj1 = new Random(unchecked((int)DateTime.Now.Ticks));
    		private Random ranObj2 = new Random();
    		private int ranNum1, ranNum2, compAns, userAns;
    		
    		public Form1()
    		{
    			InitializeComponent();
    			genQuestion();
    		}
    
    		
    		protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if (components != null) 
    				{
    					components.Dispose();
    				}
    			}
    			base.Dispose( disposing );
    		}
    
    		#region Windows Form Designer generated code
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		private void InitializeComponent()
    		{
    			this.statusBar1 = new System.Windows.Forms.StatusBar();
    			this.textBox1 = new System.Windows.Forms.TextBox();
    			this.label1 = new System.Windows.Forms.Label();
    			this.textBox2 = new System.Windows.Forms.TextBox();
    			this.SuspendLayout();
    			// 
    			// statusBar1
    			// 
    			this.statusBar1.Location = new System.Drawing.Point(0, 251);
    			this.statusBar1.Name = "statusBar1";
    			this.statusBar1.Size = new System.Drawing.Size(292, 22);
    			this.statusBar1.TabIndex = 0;
    			// 
    			// textBox1
    			// 
    			this.textBox1.Location = new System.Drawing.Point(24, 56);
    			this.textBox1.Name = "textBox1";
    			this.textBox1.TabIndex = 1;
    			this.textBox1.Text = "";
    			this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
    			// 
    			// label1
    			// 
    			this.label1.Location = new System.Drawing.Point(24, 32);
    			this.label1.Name = "label1";
    			this.label1.TabIndex = 2;
    			this.label1.Text = "Enter Answer";
    			// 
    			// textBox2
    			// 
    			this.textBox2.Location = new System.Drawing.Point(24, 136);
    			this.textBox2.Name = "textBox2";
    			this.textBox2.ReadOnly = true;
    			this.textBox2.Size = new System.Drawing.Size(176, 20);
    			this.textBox2.TabIndex = 3;
    			this.textBox2.Text = "";
    			// 
    			// Form1
    			// 
    			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    			this.ClientSize = new System.Drawing.Size(292, 273);
    			this.Controls.Add(this.textBox2);
    			this.Controls.Add(this.label1);
    			this.Controls.Add(this.textBox1);
    			this.Controls.Add(this.statusBar1);
    			this.Name = "Form1";
    			this.Text = "Multiplication Test";
    			this.ResumeLayout(false);
    
    		}
    		#endregion
    
    		
    		static void Main() 
    		{
    			Application.Run(new Form1());
    		}
    
    		public void genQuestion()
    		{
    			ranNum1 = ranObj1.Next(1, 10);
    			ranNum2 = ranObj2.Next(1, 10);
    			compAns = ranNum1 * ranNum2;
     			statusBar1.Text = "\tHow much is " + ranNum1 + " times " + ranNum2 + " ?";
    			
    		}
    
    		
    		private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    		{
    			if( e.KeyCode == Keys.Enter )
    			{
    				userAns = Int32.Parse(textBox1.Text);
    				if(userAns != compAns)
    					textBox2.Text = "NO, please try again";
    				else 
    					textBox2.Text = "Well Done!";
    
    				genQuestion();
    			}
    		}
    	}
    }
    im stuck with the logic of how to loop around when the user gets the answer wrong. If i use a while loop where the if is
    Code:
    if(userAns != compAns)
    					textBox2.Text = "NO, please try again";
    				else 
    					textBox2.Text = "Well Done!";
    the program hangs

    feedback would be greatly appreciated



    luigi

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Think about what you want to happen:
    Code:
    If the answer is right
       display "Well Done!"
       generate a new question
       (I also like clearing the entry text box here)
    else
       display "NO, please try again"
    Write it out plainly first, then translate that into code.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    thank you

    yes your right pianorain

    i think i was sitting in front of the pc too long without a break. my mind was going numb.

    this is what i came up with after 2 minutes
    Code:
    private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    		{
    			if( e.KeyCode == Keys.Enter )
    			{
    				userAns = Int32.Parse(textBox1.Text);
    				if(userAns == compAns)
    				{
    					textBox2.Text = "Well Done!";
    					textBox1.Text = "";
    					genQuestion();
    				}
    				else 
    				{
    					textBox2.Text = "NO, please try again";
    					textBox1.Text = "";
    				}
    				
    			}
    thanks for your feedback

    luigi

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Heh...almost exactly how I changed your code. However, you might want to re-think the idea of clearing the input box if the answer is wrong. It might help the user to see the wrong answer...maybe the user just mistyped it.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Line of data input method
    By larry_2k4 in forum C Programming
    Replies: 2
    Last Post: 04-28-2009, 11:34 PM
  2. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM