![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Posts: 20
Rep Power: 0
![]() |
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! ![]() |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Feb 2005
Posts: 16
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2005
Posts: 20
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Feb 2005
Posts: 16
Rep Power: 0
![]() |
Yes. Is it a windows app?
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Feb 2005
Posts: 16
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Feb 2005
Posts: 20
Rep Power: 0
![]() |
thank you
thank you bobo, its exactly what I need.
![]() |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Feb 2005
Posts: 20
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Feb 2005
Posts: 20
Rep Power: 0
![]() |
nevermind bobo, i figured this question out. Thank you again for all of your help.
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|