Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 15th, 2005, 4:41 PM   #1
gencor45
Newbie
 
Join Date: Feb 2005
Posts: 20
Rep Power: 0 gencor45 is on a distinguished road
Input validation problem

Im trying to compare a 4-digit number from a textbox as a string to a random 4-digit number i have generated as a string. I need to validate the input to these specifications, but I dont understand how using strings instead of
integers.

Specifications
If it contains one (or more) character(s) that is (are) not a digit, the user should be informed that this is an invalid "guess" and nothing else should happen. If the "guess" is not exactly 4 characters long, it too is invalid and the user should be told. If the "guess" is four digits in length, but the digits are not unique, this is also invalid and the used should be told.

Can anybody please help me understand how to validate this input using strings?
It would be appreciated!
gencor45 is offline   Reply With Quote
Old Feb 15th, 2005, 6:23 PM   #2
bobc
Newbie
 
Join Date: Feb 2005
Posts: 16
Rep Power: 0 bobc is on a distinguished road
If you just want to see if your textbox number matches your random number, you only need to check for a match. If all other conditions are invalid then there is no need to check for non-numbers or the length of the string.

Also... What do you mean by the number being unique? Do you mean none of the 4 digits can match eachother?

And if each digit does have to be unique from all the others... Your random number must have unique digits as well.

I can help you but I need to know exactly what you are trying to do.
bobc is offline   Reply With Quote
Old Feb 15th, 2005, 7:12 PM   #3
gencor45
Newbie
 
Join Date: Feb 2005
Posts: 20
Rep Power: 0 gencor45 is on a distinguished road
Yes all 4-digits need to be unique. My 4-digits in my random number are all unique also.

I cant just compare the two numbers. I need to ensure that the user inputs a valid number. This program is a simple number guessing game. The user input needs to be specified as above. If its not I need to display a appropriate message stating that the user has entered wrong input

For example

If the user enters 4546, a message needs to display saying, "You must enter
non repeating integers"

Do you understand what I am saying
gencor45 is offline   Reply With Quote
Old Feb 15th, 2005, 9:41 PM   #4
bobc
Newbie
 
Join Date: Feb 2005
Posts: 16
Rep Power: 0 bobc is on a distinguished road
Yes. Is it a windows app?
bobc is offline   Reply With Quote
Old Feb 15th, 2005, 11:57 PM   #5
bobc
Newbie
 
Join Date: Feb 2005
Posts: 16
Rep Power: 0 bobc is on a distinguished road
Try this...

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace RandomNumber
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox textBox1;
		private string rndNumber = "";
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		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.button1 = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(176, 200);
			this.button1.Name = "button1";
			this.button1.TabIndex = 1;
			this.button1.Text = "button1";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// label1
			// 
			this.label1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.label1.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(168, 16);
			this.label1.Name = "label1";
			this.label1.TabIndex = 2;
			this.label1.Text = "label1";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// textBox1
			// 
			this.textBox1.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.textBox1.Location = new System.Drawing.Point(168, 56);
			this.textBox1.Name = "textBox1";
			this.textBox1.TabIndex = 3;
			this.textBox1.Text = "";
			this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(456, 266);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.button1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void GenerateRandomString()
		{
			Random rnd = new Random();
			rnd.Next(9);
			for(int i = 0; i < 4; i++)
			{
				for(int ii = 0; ii >= 0; ii++)
				{
					string num = rnd.Next(9).ToString();
					if(rndNumber.IndexOf(num) == -1)
					{
						rndNumber += num;
						break;
					}
				}
			}
		}

		private void CompareStrings()
		{
			char[] chrArray = this.textBox1.Text.ToCharArray();
			if(chrArray.Length == 4)
			{
				bool bDupe = false;
				bool bChar = false;
				for(int i = 0; i < chrArray.Length; i++)
				{
					for(int ii = 0; ii < chrArray.Length; ii++)
					{
						if(chrArray[i] == chrArray[ii] && i != ii)
						{
							bDupe = true;
						}
					}
					if(!Char.IsNumber(chrArray[i]))
					{
						bChar = true;
					}
				}
				if(!bChar)
				{
					if(!bDupe)
					{
						if(this.textBox1.Text == this.rndNumber)
						{
							MessageBox.Show("you guessed right... you win");
						}
						else
						{
							MessageBox.Show("you guessed wrong... try again");
						}
					}
					else
					{
						MessageBox.Show("no two numbers can be the same");
					}
				}
				else
				{
					MessageBox.Show("only numbers are allowed");
				}
			}
			else
			{
				MessageBox.Show("the input must be 4 numbers");
			}
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			this.rndNumber = "";
			this.label1.Text = "";
			this.GenerateRandomString();
			this.label1.Text = rndNumber;
			this.CompareStrings();
		}

	}
}

It seems to work, but I'm getting tired and have been programming all day at work.

Let me know if it helps...

bobc
bobc is offline   Reply With Quote
Old Feb 16th, 2005, 2:52 PM   #6
gencor45
Newbie
 
Join Date: Feb 2005
Posts: 20
Rep Power: 0 gencor45 is on a distinguished road
thank you

thank you bobo, its exactly what I need.
gencor45 is offline   Reply With Quote
Old Feb 16th, 2005, 2:56 PM   #7
gencor45
Newbie
 
Join Date: Feb 2005
Posts: 20
Rep Power: 0 gencor45 is on a distinguished road
one more question

If i wanted to give the user a hint and display the random number I already created in a label randomly, how would I do that?

For example, say the random number I created is 4567. I want to display in a hint label 6574. How would I do that?
gencor45 is offline   Reply With Quote
Old Feb 17th, 2005, 9:55 AM   #8
gencor45
Newbie
 
Join Date: Feb 2005
Posts: 20
Rep Power: 0 gencor45 is on a distinguished road
nevermind bobo, i figured this question out. Thank you again for all of your help.
gencor45 is offline   Reply With Quote
Old Feb 17th, 2005, 12:55 PM   #9
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Mind posting the finished code? I'm curious.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:31 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC