Here is a simple calculator, to calculate how much candy you can get when dividing the kilogram price with the money you have allocated for candy.
In example: "Candy costs 5$ a kilo. I have 3.50$ allocated for candy. I would use this program to calculate the amount of candy I get."
It works fine until using decimals. I want to convert the string to Double so I can use decimals with the calculator. 4$ kilo price and 8$ money of course results to 2 kilos of candy. But if the kilo price was 4.80$ and I had 9.30$ money, I would only get an error and cause the command prompt to crash.
static void Main(string[] args)
{
//The variables
int int1;
int int2;
string text1;
string text2;
//The actual calculation
System.Console.Write("Please input the kilogram price of candy: ");
text1 = System.Console.ReadLine();
int1 = int.Parse(text1);
System.Console.Write("Please input the money allocated for candy ");
text2 = System.Console.ReadLine();
int2 = int.Parse(text2);
System.Console.WriteLine("With the amount of money you input you would get " + (int2 / int1) + " kilos of candy.");
}
}
}
Double.TryParse(text1, out myDoubleVariable);instead of using integers. Integers are, by definition, whole numbers.intnotdouble? ps.int1is a terrible variable name :p4.80in anint"? Maybe you need to educate yourself about the various numeric variable types C# offers.