C# C# Basics Perfect Wrap Up

bot .net
bot .net
9,771 Points

Arithmetic Calculator challenge problem, I cant exit the program for some reason.

Hi, This is my code:

using System;

namespace TreeHouse.Calculator
{
    class Calculator
    {
        static void Main()
        {
            var totalResult = 0.0;

            while(true) 
            {
                try
                {
                    Console.WriteLine("Please Enter a number: ");
                    string entry = Console.ReadLine();
                   var number = double.Parse(entry);
                    if(entry.ToLower() == "quit") {
                        break;

                    }


                    Console.WriteLine("Please enter an operation [+ - / * ]: ");
                    var entry2 = Console.ReadLine();
                    if(entry2.ToLower() == "quit") {
                        break;
                    }



                    Console.WriteLine("Please enter another number: ");
                    string entry3 = Console.ReadLine();
                    var number2 = double.Parse(entry3);
                    if(entry3.ToLower() == "quit") {

                        break;
                    }

                    if(entry2 == "+")
                    {
                        totalResult = number + number2;    
                    }

                    else if(entry2 == "-")
                    {
                        totalResult = number - number2;

                    }

                    else if(entry2 == "*")
                    {
                        totalResult = number * number2;


                    }
                    else if(entry2 == "/")
                    {
                        totalResult = number / number2;


                    }


                    Console.WriteLine("the result is: {0}", totalResult);

                } 

                catch(FormatException) {
                    Console.WriteLine("Your input is invalid, Please try again!..");
                    continue;




                }



            }



        }



    }




}

when i try to exit the program by typing 'quit', i get the messege from my exception, I checked alot of times and i can't find the problem why my 'if' statment are not working..

another problem i have is when i type a string or a number in a operator prompt it pass and in result the "final result" become 0.. any ideas how to fix this?

Steven Parker Brendan Whiting

:D?

bot .net
bot .net
9,771 Points

****** UPDATE ******

I somehow tried to get all this code outside the 'try' check

using System;

namespace TreeHouse.Calculator
{
    class Calculator
    {
        static void Main()
        {
            var totalResult = 0.0;
            Console.WriteLine("what is your name? : ");
            string name = Console.ReadLine();
            Console.WriteLine("Welcome to the program {0}!", name);


            while(true) 
            {
                 Console.WriteLine("Please Enter a number: ");
                 string entry = Console.ReadLine();
                 if(entry.ToLower() == "quit") {
                     Console.WriteLine("Goodbye, {0}", name);
                        break;

                    }

                 Console.WriteLine("Please enter an operation [+ - / * ^ ]: ");
                 var entry2 = Console.ReadLine();
                 if(entry2.ToLower() == "quit") {
                      Console.WriteLine("Goodbye, {0}", name);
                        break;
                    }

                 Console.WriteLine("Please enter another number: ");
                 string entry3 = Console.ReadLine();
                 if(entry3.ToLower() == "quit") {
                      Console.WriteLine("Goodbye, {0}", name);
                        break;
                    }


                try
                {
                    var number = double.Parse(entry);
                    var number2 = double.Parse(entry3);


                    if(entry2 == "+")
                    {
                        totalResult = number + number2;    
                    }

                    else if(entry2 == "-")
                    {
                        totalResult = number - number2;

                    }

                    else if(entry2 == "*")
                    {
                        totalResult = number * number2;


                    }
                    else if(entry2 == "/")
                    {
                        totalResult = number / number2;


                    }

                    else {
                        totalResult = Math.Pow(number, number2);

                    }


                    Console.WriteLine("the result is: {0}", totalResult);

                } 

                catch(FormatException) {
                    Console.WriteLine("Your input is invalid, Please try again!..");
                    continue;




                }



            }



        }



    }




}

It's working now but i dont fully understand why i had to get this code outside of the "TRY" check.

and i still didnt fixed the other problem i mentioned before.

1 Answer

Erik Gabor
Erik Gabor
3,404 Points

The reason are the following codes

                   Console.WriteLine("Please Enter a number: ");
                    string entry = Console.ReadLine();
                   var number = double.Parse(entry);
                    if(entry.ToLower() == "quit") {
                        break;

                    }

//....

                    Console.WriteLine("Please enter another number: ");
                    string entry3 = Console.ReadLine();
                    var number2 = double.Parse(entry3);
                    if(entry3.ToLower() == "quit") {

                        break;
                    }

These codes are contained in a try catch block. So when you enter "quit" you already caused a format exception and in the catch block you have continue which sends to the beginning of the while loop. The solution would have been to call the parse after checking "quit". You did this by restructuring the code in the second example.