Charles Han
Charles Han

Reputation: 799

Why is this while loop getting stuck?

I just wrote a simple c# code for calculating the factorial of a number but the the program is getting stuck on the last name. Could someone why it's getting stuck?

Thanks,

Omin

using System;

//1. Write a program which finds the factorial of a number entered by the user.

namespace Beginner1{

class ProblemOne
{
    static void Main (string[] args)
    {
        bool play = true;
        while ( play ) {
            Console.Write ("Type in the number you would like to find Factorial of: ");
            int num = Convert.ToInt16( Console.ReadLine ());
            int sum = 1;
            for (int i = 2; i <= num; i++) {
                sum = sum * i;
            }
            Console.WriteLine ("The Factorial of {0} is {1}", num, sum);
            Console.Write ( "Would you like to play again? (Y or N): " );
            string ans = Console.ReadLine();
            do {
                if( ans == "Y" || ans == "y" )  {
                    play = true;
                    //break;
                }
                else if( ans == "N" || ans == "n" ) {
                    play = false;
                    //break;
                }
                else
                {
                    Console.Write( "You have entered an invalid answer. Please choose from either Y or N: ");
                    ans = Console.ReadLine();
                }
            } while ( (ans != "Y") || (ans != "y") || (ans != "N") || (ans != "n") );
        }
    }
}

} `

Upvotes: 0

Views: 767

Answers (3)

Matthew Alltop
Matthew Alltop

Reputation: 545

I would do something like this:

bool isValid = false;

        do
        {

            if( ans == "Y" || ans == "y" )  {
                play = true;
                isValid = true;
                //break;
            }
            else if( ans == "N" || ans == "n" ) {
                play = false;
                isValid = true;
                //break;
            }
            else
            {
                Console.Write( "You have entered an invalid answer. Please choose from either Y or N: ");
                ans = Console.ReadLine();
                isValid = false;
            }
        }while(isValid == false);

Relevant fiddle:https://dotnetfiddle.net/bxHY27

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500555

Look at your while condition:

while ( (ans != "Y") || (ans != "y") || (ans != "N") || (ans != "n") )

In order to break out, all of those sub-conditions have to be false (so that the overall value is false).

The only way for the first condition to be true is if ans is "Y"... which means it definitely won't be equal to "y"...

In other words, your loop is equal to:

while (!(ans == "Y" && ans == "y" && ans == "N" && ans == "n"))

It would have to be a pretty special string to be equal to all four values.

You actually want:

while (ans != "Y" && ans != "y" && ans != "N" || ans != "n")

In other words, keep going while the value is not equal to any of the desired values. (An alternative would be to keep a set of "good answers"...

Upvotes: 8

urlreader
urlreader

Reputation: 6605

        do {
            string ans = Console.ReadLine();
            if( ans == "Y" || ans == "y" )  {
                play = true;
                //break;
            }
            else if( ans == "N" || ans == "n" ) {
                play = false;
                //break;
            }
            else
            {
                Console.Write( "You have entered an invalid answer. Please choose from either Y or N: ");
                //ans = Console.ReadLine();
            }
        } while ( (ans != "Y") && (ans != "y") && (ans != "N") && (ans != "n") );

Upvotes: 0

Related Questions