3

For example when my program asks for a number and I enter a letter the program exits. So how can I make it do nothing when a letter Is entered? i have seen this with 'int' or 'strings' but not with 'doubles' it is really stumping me and i don't quite understand how even those were fixed

 static void Main(string[] args)
    {
        Console.WriteLine("type 'Exit' to leave this program.");
        Console.ReadLine();
        Console.Clear(); 
    Valuechoice: Console.WriteLine("Please choose a number.");
        double value1 = Convert.ToDouble(Console.ReadLine());     
        Console.WriteLine("Please choose a second Number.");
        double value2 = Convert.ToDouble(Console.ReadLine());
        Console.WriteLine("What would you like to do to these numbeers?");
        Console.WriteLine("Multiply");
        Console.WriteLine("Divide");
        Console.WriteLine("Add");
        Console.WriteLine("subtract");
        string operation = Console.ReadLine();
        Console.WriteLine();
        Console.Clear();
        if (operation == "Multiply")
        {
            double total1;
            total1 = (value1 * value2);
            Console.WriteLine(total1);
            Console.ReadLine();
            Console.Clear();
            goto Valuechoice;
        }
        if (operation == "Divide")
        {
            double total2;
            total2 = (value1 / value2);
            Console.WriteLine(total2);
            Console.ReadLine();
            Console.Clear();
            goto Valuechoice;
        }
        if (operation == "Add")
        {
            double total3;
            total3 = (value1 + value2);
            Console.WriteLine(total3);
            Console.ReadLine();
            Console.Clear();
            goto Valuechoice;

        }
        if (operation == "Subtract")
        {
            double total4;
            total4 = (value1 - value2);
            Console.WriteLine(total4);
            Console.ReadLine();
            Console.Clear();
            goto Valuechoice;

        }
        if (Console.ReadLine() == "Exit")
        {
            Environment.Exit(0);
        }
Mark Cidade
  • 98,437
  • 31
  • 224
  • 236

3 Answers3

4

Convert.ToDouble will throw a FormatException if the input is not a valid double. Use double.TryParse instead.

For example, you could do something like this to keep asking the user for a valid number:

double value1;

while(!double.TryParse(Console.ReadLine(), out value1)) 
{
    Console.WriteLine("Please choose a valid number");
}

Also there's really no reason to use goto here. I'd recommending restructuring your program to use a loop instead.

Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
  • thanks it works like a charm. could you maybe explain how to use a loop? I am just learning how to program. – TheMightyQuin Mar 03 '13 at 01:31
  • @TheMightyQuin: [MSDN's documentation on the while loop](http://msdn.microsoft.com/en-us/library/2aeyhxcd(v=vs.71).aspx) is pretty good--Basically it just repeats the code inside of the block until the condition inside of the parenthesis is false. – Andrew Whitaker Mar 03 '13 at 13:24
1

Use TryParse - something like this:

double value1;
while (!double.TryParse(Console.ReadLine(), out value1) {
  Console.WriteLine("Please enter a valid number");
}
MiMo
  • 11,793
  • 1
  • 33
  • 48
0

You can't convert a letter to a double, which is a number. It's like saying turn "e" into the decimal 34.72. It's impossible.

Try something like this:

bool valid = true; // locally declared variable, this is important!

string value = Console.ReadLine();
valid = validateInput(value);

if (valid == true) {
double value1 = Convert.ToDouble(value);
// Proceed with calculations, etc
}

private bool validateInput(string value) {
bool valid = true;
foreach (char c in value)
    {
        if (char.IsLetter(c))
            valid = false;
        Console.WriteLine("Input is invalid!");
    }
return valid;
}
barnacle.m
  • 2,070
  • 3
  • 38
  • 82