How do I add data validation onto my current code? [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm 100% new to programming, this is what I want:
(Name) No numbers
(Card Number) Limit to 16 digits and no letters
(Expiry Date) Numbers like this - "02/17" and no letters
(Security Code) Limit to 3 numbers and no letters.
My code:
string message =
"Name: " + nameTextBox.Text +
"\nCard Number: " + cardNumberTextBox.Text +
"\nExpiry Date: " + expiryDateTextBox.Text +
"\nSecurity Code: " + securityCodeTextBox.Text +
"\nOrder: Pizza " + pizzaType + ", " + pizzaSize;
if (TotalToppingQuantities() > 0)
{
for (int toppingIndex = 0; toppingIndex < toppingQuantities.Length; toppingIndex++)
{
if (toppingQuantities[toppingIndex] > 0)
{
message += ", " + toppingQuantities[toppingIndex] + " x " +
toppingNames[toppingIndex];
}
}
}
message +=
"\nPickup Spot: " + pickupSpot +
"\nDelivery Time: 30 minutes";
MessageBox.Show(message);

For your problem, regex is good solution.
And this should work for you
using System.Text.RegularExpressions;
//====================================
if (Regex.Match(nameTextBox.Text, "\\d").Success)
{
MessageBox.Show("(Name) must contain No numbers");
return ;
}
if (!Regex.Match(cardNumberTextBox.Text, "^\\d{16}$").Success)
{
MessageBox.Show("(Card Number) must be Limited to 16 digits and no letters");
return ;
}
if (!Regex.Match(expiryDateTextBox.Text, "^\\d{2}/\\d{2}$").Success)
{
MessageBox.Show("(Expiry Date) must be Numbers like this - 02/17 and no letters");
return ;
}
if (!Regex.Match(securityCodeTextBox.Text, "^\\d{3}$").Success)
{
MessageBox.Show("(Security Code) must be Limited to 3 numbers and no letters.");
return ;
}

Related

Is their a way to decrease a number? 10/7/20 - 3/5/21 *update* [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
**What I tried to do is decrease 1000 to 900 but instead it would give 999 not 900 an then it would put the 900 above the the text.
Cone_head Casted The cone of time & Cone of shield
4
you regain 5 slots
900
you lost 999 of your health
expericance: 2.7
Here's the code:
_Player_Health = Player_Health = 1000;
damage = 100;
if (Chance > 99)
{
System.Console.WriteLine(Player_Health - damage);
System.Console.WriteLine(Player_Health);
Player_Health--;
System.Console.WriteLine("you lost "+ Player_Health + " of your health");
string a = Convert.ToString(Player_Health);
} else
{
System.Console.WriteLine("Miss!, next player turn");
c = Convert.ToInt16(Player_Health);
}
static void Main(string[] args)
{
int Player_Health = 1000;
Conehead_Wizzard wizzard = new Conehead_Wizzard("Cone_Head ", "The Cone of time &" + " Cone of shield", Player_Health);
}
player_Health - b prints out this
I think you are missing too much code to make people understand what you want. it is not even possible to compile what you showed. Do you want to do something like that:
damage = 100;
if (Chance > 99)
{
System.Console.WriteLine("Health before taking damage: " + Player_Health);
Player_Health -= damage;
System.Console.WriteLine("you lost "+ damage + " of your health");
System.Console.WriteLine("You have " + Player_Health + " health left");
string a = Convert.ToString(Player_Health);
} else
{
System.Console.WriteLine("Miss!, next player turn");
c = Convert.ToInt16(Player_Health);
}

Calling class in a class? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm new.
my problem:
class Battle
{
public void Fight(string charOne, string charTwo)
{
void CurrentHealth()
{
Console.WriteLine(charOne.name + "'s Health: " +
charOne.currentHealth + "/" + charOne.maxHealth +
" | " + charTwo.name + "'s Health: " + charTwo.currentHealth +
"/" + charTwo.maxHealth);
}
}
}
the ".name" and ".currentHealth" are not working.
charOne would be a class instance, and charTwo would be another class instance.
[UPDATE BELOW]
Below was the code that was originally working in the main program class, and I was trying to move it in to a class, where I could pass in some parameters to the class, so the Hero could fight other monsters in the future. This single time monster was "mosquito".
I just want to be able to replace a "mosquito" with a "wolf" class and maybe "hero2" instead of "hero". Hope this makes more sense.
void CurrentHealth()
{
Console.WriteLine(hero.name + "'s Health: " + hero.currentHealth + "/" + hero.maxHealth + " | " + mosquito.name + "'s Health: " + mosquito.currentHealth + "/" + mosquito.maxHealth);
}
void Attack(int dmg)
{
Console.WriteLine(hero.name + " Dealt " + dmg + " damage to " + mosquito.name + ".");
}
while (mosquito.currentHealth > 0 && hero.currentHealth > 0)
{
Console.Clear();
int damage = hero.AttackAction();
mosquito.currentHealth = mosquito.currentHealth - damage;
int newHealth = mosquito.currentHealth;
Attack(damage);
CurrentHealth();
Console.WriteLine();
Console.WriteLine("Press any key to continue.");
Console.Read();
}
if (mosquito.currentHealth <= 0)
{
Console.WriteLine("Right on! You are Victorious!!!");
}
else if (hero.currentHealth <= 0)
{
Console.WriteLine("Oh no.. you are dead...");
}
else
{
Console.WriteLine("You reached the end.");
}
public void Fight(MyCharacterClass charOne, MyCharacterClass charTwo)
Try it like this, of course change your own class with MyCharacterClass

Is their a way to keep individual variables to themselves without adding them with each other c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Is their a way to add each variable by colum instead of individually adding each one?
Like if I wanted to multiply a row the code won't just take every variable and multiply it. I want the code to keep them in the same place and print it to the screen in the same format.
Console.WriteLine(x + " " + y + " " + z);
Console.WriteLine(a + " " + b + " " + c);
Console.WriteLine(ab + " " + bb + " " + cb);
Something like this?
Console.WriteLine("{0,10}{1,10}{2,10}", x, y, z);
See here.
static void Main(string[] args)
{
int x = 1;
int y = 2;
int z = 3;
ConsoleWriteColumn(x,y,z);
Console.WriteLine("Multiplied by 2:");
ConsoleWriteColumn((x*2),(y*2),(z*2));
Console.ReadKey();
}
static void ConsoleWriteColumn(int value1, int value2, int value3)
{
Console.WriteLine($"{value1,5}{value2,5}{value3,5}");
}
Console.WriteLine($"{x}\t{y}\t{z}");
Console.WriteLine($"{a}\t{b}\t{c}");
Console.WriteLine($"{ab}\t{bb}\t{cb}");
Note: This format is supported as from C# 6 and up.

My c# program for square roots only works when the discriminant is 0 or less than 0, but not when it's greater than 0 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
private void btnCompute_Click(object sender, EventArgs e)
{
double coefficientA;
double coefficientB;
double coefficientC;
double root1;
double root2;
double discriminant;
coefficientA = double.Parse(txtCoeA.Text);
coefficientB = double.Parse(txtCoeB.Text);
coefficientC = double.Parse(txtCoeC.Text);
discriminant =
(coefficientB * coefficientB) -
(4 * coefficientA * coefficientC);
txtOutput.Text = "Discriminant = " + discriminant.ToString("N2");
//-b/2a
root1 = (-coefficientB + discriminant) / (2 * coefficientA);
root2 = (-coefficientB - discriminant) / (2 * coefficientA);
if (discriminant < 0)
{
txtOutput.Text += "\r\nEquation has no real roots!";
}
else if (discriminant == 0)
{
txtOutput.Text +=
"\r\nEquation has one root: " + root2.ToString("N2");
}
else if (discriminant > 0)
{
txtOutput.Text = "Equation has two real roots: " +
"\r\nroot1: " +
(-coefficientB + Math.Sqrt(discriminant) /
(2 * coefficientA)) +
"\r\nroot2: " + (coefficientB - Math.Sqrt(discriminant) /
(2 * coefficientA));
}
}
Try this:
txtOutput.Text = "Equation has two real roots: " +
"\r\nroot1: " +
(-coefficientB + Math.Sqrt(discriminant)) / (2 * coefficientA) +
"\r\nroot2: " +
(-coefficientB - Math.Sqrt(discriminant)) / (2 * coefficientA);
You were bracketing the formula incorrectly.

Int and string together [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Its my code :
if (numavg <=3 ){
Console.WriteLine("Congratz, we can start! but some note before :");
Console.WriteLine("When you put the last number, type 'c' in the next line for calculate the avg");
Console.WriteLine("So lets start! type the first number.");
avg1 = int.Parse(Console.ReadLine());
Console.WriteLine("Ok, so you choose " + avg1 + ",who is the next number?");
avg2 = int.Parse(Console.ReadLine());
Console.WriteLine("Wonderful! put the next number");
avg3 = int.Parse(Console.ReadLine());
if (avg3 = "c"){
Console.WriteLine("Ok, Lets calculate!");
average = (avg1 + avg2)/numavg;
Console.WriteLine("The average is " + average + ".");
}
else {
Console.WriteLine("Perfect! please type the next number,or 'c' for avg to the last 3 numbers.");
avg4 = int.Parse(Console.ReadLine());
if (avg4 = "c" ){
Console.WriteLine("Ok, Lets calculate!");
average1 = (avg1 + avg2 + avg3)/numavg;
Console.WriteLine("The average is " + average1 + ".");
}
}
The problem is with type "c" (for calculate the avg).
I dont understand how I can keep avg3 and avg4 as string and int together.
help?
and more pure question -
If I want the program end and restart automaticlly, which code I need type?
(if the restart isn't possible, how can I do the first thing with the auto program closing?)
Thank u guys!
NOTE :
The error is " Cannot implicitly convert type 'string' to 'int'".
do you mean if(avg3 == "c") instead of if (avg3 = "c")
(avg3 = "c") you are assigning "c" to avg3 which is of type int which explains " Cannot implicitly convert type 'string' to 'int'".
String avg3Str = Console.ReadLine();
if (avg3Str.equals("c")){
Console.WriteLine("Ok, Lets calculate!");
average = (avg1 + avg2)/numavg;
Console.WriteLine("The average is " + average + ".");
}
else {
avg3 = int.Parse(avg3Str);
Console.WriteLine("Perfect! please type the next number,or 'c' for avg to the last 3 numbers.");
String avg4Str = Console.ReadLine();
if (avg4Str.equals("c") ){
Console.WriteLine("Ok, Lets calculate!");
average1 = (avg1 + avg2 + avg3)/numavg;
Console.WriteLine("The average is " + average1 + ".");
}
else
{
avg4 = int.Parse(avg4Str);
Console.WriteLine("Ok, Lets calculate!");
average1 = (avg1 + avg2 + avg3 + avg4)/numavg;
Console.WriteLine("The average is " + average1 + ".");
}
}

Categories

Resources