C# Input String not in correct format [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I can't figure out why the code is in the wrong format. I'm getting the the error on the line of TotalTicketprice = decimal.Parse(overallTicketCostlabel.Text); and everything that's associated with it. Anyone know a solution for this?
private void calculateButton_Click(object sender, EventArgs e)
{
int Tickets; //Quantity of Tickets
decimal Price; //price per ticket
decimal DiscountPercent = 0.1m; // Discount given
decimal TotalTicketprice; //Ticket price before discount given
decimal Discount;
Tickets = int.Parse(ticketQuantityTextBox.Text);
Price = decimal.Parse(priceTicketTextBox.Text);
TotalTicketprice = decimal.Parse(overallTicketCostlabel.Text);
Discount = decimal.Parse(initialDiscountLabel.Text);
//Ticket Price
overallTicketCostlabel.Text = (Price * Tickets).ToString("c");
//Displays Discount
initialDiscountLabel.Text = (TotalTicketprice * DiscountPercent).ToString("c");
//Displays Discounted Total Cost
discountedCostLabel.Text = (TotalTicketprice - Discount).ToString("c");

Just a guess, going from your naming convention, should this be;
TotalTicketprice = decimal.Parse(overallTicketCostTextBox.Text);
instead of:
TotalTicketprice = decimal.Parse(overallTicketCostlabel.Text);
You may also want to include some validation to ensure that these are numeric prior to parsing them.

overallTicketCostlabel.Text = (Price * Tickets).ToString("c");
this formats as a Currency. This means that it will probably have a currency symbol in the text. Like $ 100 for example.

Related

C# cant figure out addition and percentage calculation [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 6 months ago.
Improve this question
i have to make a simple ticket system where a person inputs name, number of adults and children and gets back the amount of money they have to pay (children get a 75% discount) and i cant seem to figure out how to do the calculation in code, and since there is only one teacher for about 500 students getting an answer from them isnt much of an option. Thanks in advance for any kind of help!
Are you looking for sth like this?
double cost = 10;
Console.WriteLine("Number of Adults");
var adults = Console.ReadLine();
var numberOfAdults = 0;
if (adults != null) numberOfAdults = int.Parse(adults);
Console.WriteLine("Number of Children");
var children = Console.ReadLine();
var numberOfChildren = 0;
if (children != null) numberOfChildren = int.Parse(children);
double price = numberOfAdults * cost + numberOfChildren * cost * 0.25;
Console.WriteLine($"Price: ${price}.");
Console.ReadLine();

cannot convert datatype from float to string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
I want to change the value of total price's text box when unit price and quantity is entered.Unit price and total price's data type is float.
This code show error tht cannot convert int to float
how i can solve this issue?
Txtbox_quantity_textchanged
{
TxttotalPrice.text= (a*b).toString();
}
I can't see where is the problem, since you're unclear.
the following code compiles and runsL
float floatNumber = 60;
int intNumber = 34;
string result = (intNumber*floatNumber).ToString();
If you're having other trouble try casting:
float floatNumber = 60;
int intNumber = 34;
float result = (float) intNumber*floatNumber;
string newResult = result.ToString();

Operator `*' cannot be applied to operands of type `System.Collections.Generic.List<double>' and `double' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
I'm trying to do a simple math equation, however, when I try to use "*" it throws the error
error CS0019: Operator *' cannot be applied to operands of typeSystem.Collections.Generic.List' and `double'
Code example:
public static List<string> items = new List<string> ();
public static List<double> itemsprice = new List<double>();
public static List<double> qu = new List<double>();
int i = 0;
double price = 0;
while(i != items.Count){
price = itemsprice[1];
ticksales = qu * pricepoint / 2; // Error on this line
income = income + ticksales * price;
}
That is occuring because qu is a List of type double and you are trying to multiply it. It seems like what you want to do is qu[i] * pricepoint/2;
Also you should increment i at the end of your loop or it will run forever.
qu is a List. What do you think list * pricepoint / 2 equals? (we don't know what pricepoint is BTW)
au is a List, you have to provide the index of the item you want to access. And also at the end you have to increment the index.
ticksales = qu[i] * (pricepoint / 2)
and i++; at the end.

sum digits from a very long number [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Assume I let user input number : 1298743257884834...(long as user need)
Let program tell how many digit are they ?
then give result by SUM them = ?
By using LINQ
string input = // get input from console, textbox, ...
int inputLength = input.Length; // get number of digits, although you don't need it here
int sum = input.Sum(c => int.Parse(c.ToString())); // summarize
Hint: use the BigInteger structure found in System.Numerics.
First of all you can know the number of digits in this way:
string digits = TextBox1.Text;
long digitsnumber = digits.Length;
Then to sum each number, you need to loop in your string like an array of char, in this way, and cast the car value to an integer with the GetNumericValue method of System.Char:
int sum = 0;
foreach (var c in digits)
{
if (Char.IsNumber(c))
{
sum += (int)Char.GetNumericValue(c);
}
}
Sum will be your result.

c# text box "between" validation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to have a text box validate if the entry is a number between 1 and 100.
Example:
if (textBox.Text is equal to numbers between 1 and 100)
{
do this;
}
else
{
do this;
}
This is form validation for a trackbar used for jpeg compression and can only have numeric values between 1 and 100. How do I do this?
String text = TextBox.Text;
try{
long value = long.parse(text.trim());
if(value > 0 && value < 101){
//do something here
}
else{
//Do something else
}
}
catch(Exception e){
Messagebox.Show("Please check you input and try again");
}
First you need to convert input from textbox from string to integer
string textBoxvalue = textBox.Text;
int textBoxIntValue = int.TryParse(textBoxvalue)
then you need to check values for condition you need
if(textBoxIntValue > 0 && textBoxIntValue <= 100)
{
//do THIS
}

Categories

Resources