xtheking
xtheking

Reputation: 564

c# - program not exiting

just started using C#, and this may be just a simple fix but I can't seem to see it. Once the program executes the first time (after adding two numbers), the user is prompted to input yes or no as an exit condition. When I type in 'no', the program will loop again.

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

namespace BasicCalculator
{
    class Program
    {
        static void Main(string[] args)
        {
            bool again = false;
            while (!again)
            {
                Console.WriteLine("C# Basic Calcuator");
                Console.WriteLine("Please enter two numbers to be added: ");
                Console.WriteLine("Number 1: ");

                int result;
                int a = Convert.ToInt32(Console.ReadLine());


                Console.WriteLine("Number 2: ");
                int b = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("Result: ");
                result = a + b;

                //print result
                Console.WriteLine(result);

                //potential exit condition
                Console.WriteLine("Would you like to calculate again?");
                if(Console.ReadLine() == "no")
                {
                    again = false;
                }
                else
                {
                    again = true;
                }

            }



        }
    }
}

Any help is greatly appreciated!

Upvotes: 2

Views: 325

Answers (5)

Neel
Neel

Reputation: 11731

You can achieve what you want with use of While with making minor changes as suggested by differnet people here but You can even Use do while instead of while which will go in loop first time and then will check for the condition everytime:-

As MSDN says :-

The do statement executes a statement or a block of statements repeatedly until a specified expression evaluates to false. The body of the loop must be enclosed in braces, {}, unless it consists of a single statement. In that case, the braces are optional.

bool again = false;
               do {
                Console.WriteLine("C# Basic Calcuator");
                Console.WriteLine("Please enter two numbers to be added: ");
                Console.WriteLine("Number 1: ");

                int result;
                int a = Convert.ToInt32(Console.ReadLine());


                Console.WriteLine("Number 2: ");
                int b = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("Result: ");
                result = a + b;

                //print result
                Console.WriteLine(result);

                //potential exit condition
                Console.WriteLine("Would you like to calculate again?");
                if(Console.ReadLine() == "no")
                {
                    again = false;
                }
                else
                {
                    again = true;
                }

            }  while (again);

Upvotes: 3

Steve
Steve

Reputation: 216288

You could write

again = (Console.ReadLine() == "no" ? true : false);

But your choice of variable naming is really ambiguous, a better name could be

bool stopLoop = false;
while (!stopLoop)
{
    .....
    stopLoop = (Console.ReadLine() == "no" ? true : false);
}

As pointed in the comment below, you shouldn't try to convert to an integer what your user types without a better check for errors. Using Convert.ToInt32 on a non numeric value throws an exception. A better code could be

  int result;
  if(!Int32.TryParse(Console.ReadLine(), out result))
  {
       Console.WriteLine("Please enter a valid number");
       continue;
  }

Upvotes: 2

daryal
daryal

Reputation: 14919

change

while (!again)

with

while (again)

when user types no, again is set to false; so in the while loop again is negated which results in continuation of while loop.

by the way, you need to change

bool again = false;

to

bool again = true;

Upvotes: 3

ttaaoossuu
ttaaoossuu

Reputation: 7884

Accidental programming powers activate!

if(Console.ReadLine() == "no")
    again = true;
else
    again = false;

And also you might wanna want to change the name of the variable to something like stop_flag so that it would make sense.

Upvotes: 0

Melbz
Melbz

Reputation: 489

Change the top 2 lines to the following:

bool again = true;
while (again)

Upvotes: 10

Related Questions