Dmon
Dmon

Reputation: 218

Infinite While looping in c#

I'm new to c# and am having a problem with a while loop that won't break. It's just a simple console calculator and I want the switch statement to break out of the loop if a correct entry is made and to keep looping otherwise. Problem is it keeps looping even when a correct letter is chosen eg A for add, Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace basic_calc
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("this is a basic calculator");
            double num1 = Getvalue("Enter the first number: ");
            double num2 = Getvalue("Enter the second number: ");

            double output = 0;
            while (true)
            {
                Console.WriteLine(" Please select an operation (A)dd, (S)ubtract, (M)ultiply, (D)ivide");
                char operation = Console.ReadKey().KeyChar;
                operation = Char.ToUpper(operation);
                switch (operation)
                {
                    case 'A':
                        output = Add(num1, num2);
                        break;
                    case 'S':
                        output = Subtract(num1, num2);
                        break;
                    case 'M':
                        output = Multiply(num1, num2);
                        break;
                    case 'D':
                        output = Divide(num1, num2);
                        break;
                    default:
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("INVALID INPUT!!!!!!!!!!!!!!!");
                        Console.ResetColor();
                        continue;
                }    
            }


            Console.WriteLine("\nresult: "+ output);

            Console.ReadKey();
        }
        private static double Getvalue(string input)
        {
            //the value to be returned
            double value;
            while (true)
            {
                Console.Write(input);
                string number = Console.ReadLine();
                if (double.TryParse(number, out value))
                {
                    return value;

                }
                else
                    Console.WriteLine("please enter a number for this calculator");
            }
        }
        private static double Add(double n1, double n2){return n1 + n2;}
        private static double Subtract(double n1, double n2) { return n1 - n2; }
        private static double Multiply(double n1, double n2) { return n1 * n2; }
        private static double Divide(double n1, double n2) { return n1 / n2; }
    }
}

Upvotes: 3

Views: 901

Answers (3)

Rohit
Rohit

Reputation: 10296

You have to break out of switch statement.Because of While(true) you end up in a loop.So after your switch statement ad a break.

switch (operation)
{
case 'A':
    output = Add(num1, num2);
    break;
case 'S':
    output = Subtract(num1, num2);
    break;
case 'M':
    output = Multiply(num1, num2);
    break;
case 'D':
    output = Divide(num1, num2);
    break;
default:
    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine("INVALID INPUT!!!!!!!!!!!!!!!");
    Console.ResetColor();
    continue;
}
  break; 

Upvotes: 2

Obl Tobl
Obl Tobl

Reputation: 5684

Your break just jumps out of your switch-Statement. Try it with a bool-variable you set in your cases:

var noOutput = true;
while (noOutput)
            {
                Console.WriteLine(" Please select an operation (A)dd, (S)ubtract, (M)ultiply, (D)ivide");
                char operation = Console.ReadKey().KeyChar;
                operation = Char.ToUpper(operation);
                switch (operation)
                {
                    case 'A':
                        output = Add(num1, num2);
                        noOutput = false;
                        break;
                    ...
                }    
            }

Upvotes: 3

dario
dario

Reputation: 5269

Just add a break after the switch:

switch (operation)
{
    case 'A':
        output = Add(num1, num2);
        break;
    case 'S':
        output = Subtract(num1, num2);
        break;
    case 'M':
        output = Multiply(num1, num2);
        break;
    case 'D':
        output = Divide(num1, num2);
        break;
    default:
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("INVALID INPUT!!!!!!!!!!!!!!!");
        Console.ResetColor();
        continue;
}
break; // This will exit the loop when a match is found.

Since you are using continue if a match is not found, the break after the switch will exit the loop if a match is found.

Upvotes: 6

Related Questions