user2916941
user2916941

Reputation: 25

C# Console Program Loop Issue

I have tried to create a C# program that gives the user 3 options:

  1. Create name (gets the user to enter their first name and surname and display it as J.Blogg)

  2. Factorial of a number (outputs as for example 5x4x3x2x1 =120 which is the factorial of 5

  3. Quit

I have the program work fine but when I try picking option 1 (create name) and then option 2 it goes to option 1 instead and then it doesn't lets me Quit (option 3). I'm new to programming so it could be simple but I can't see where i'm going wrong, Any help would be very greatful. I want to keep the same layout, I think my problem could be the loops but any help and improvement would be great.

    static void Main(string[] args)
    {

        //The value returned from the topmenu method is stored in a variable called useroption
        int useroption;
        useroption = topmenu();

        // excute while loop untill option is not 1-3
        do
        {


            if (useroption == 1)
            {
                Console.Clear();
                Createname();
                //break;
            }

            if (useroption == 2)
            {
                Console.Clear();
                factorial();
               // break;
            }

            if (useroption == 3)
            {
                Console.Clear();
                Console.WriteLine("Thank you for using my program, Good bye !!!");
               // break;
            }

            //topmenu();
             } 
        while (useroption != 3);




        Console.ReadKey();


    }

    //This method present the user with an menu which the user has a choice of 3 options
    static int topmenu()
    {
        int option;
        string option_str;

        Console.Clear();
        Console.WriteLine("********************************************************************************");
        Console.WriteLine("********************************************************************************");
        Console.WriteLine("*********      OPTION 1 : Enter your name                              *********");
        Console.WriteLine("*********      OPTION 2 : Enter the number you want to factorise       *********");
        Console.WriteLine("*********      OPTION 3 : Quit                                         *********");
        Console.WriteLine("********************************************************************************");
        Console.WriteLine("********************************************************************************");
        option_str = Console.ReadLine();

        option = Convert.ToInt32(option_str);
        Console.Clear();

        if (option < 0 || option > 3)
        {
            Console.WriteLine("You have enter an invald option,");
            Console.WriteLine("Please chose a option between 1-3 (Please press any key to return to main menu)");
            Console.ReadLine();
            Console.Clear();
            topmenu();
        }
        else
        {
            Console.WriteLine("You have chosen option: " + option + " (Please press any key continue)");
        }
        Console.ReadKey();
        return option;




    }
    //this method asks user to enter their name (1st name then surname) and presents it back to the user as their intial(1st name) and surname
    static void Createname()
    {
        string firstname, surname, firstname_str, surname_str, userfullname;

        Console.Clear();
        Console.WriteLine("Please enter your first name ");
        firstname_str = Console.ReadLine();
        firstname = Convert.ToString(firstname_str);
        Console.Clear();
        Console.WriteLine("Please enter your surname name ");
        surname_str = Console.ReadLine();
        surname = Convert.ToString(surname_str);
        Console.Clear();
        userfullname = firstname + surname;

        Console.WriteLine("You have entered your name as " + firstname[0] + "." + surname);
        Console.WriteLine("(Please press any key to return to main menu)");
        Console.ReadKey();
        topmenu();

    }

    //this method asks the user to enter a number and returns the factorial of that number
    static double factorial()
    {


        string number_str;
        double factorial = 1;



        Console.WriteLine("Please enter number");
        number_str = Console.ReadLine();

        int num = Convert.ToInt32(number_str);


        // If statement is used so when the user inputs 0, INVALID is outputed

        if (num <= 0)
        {
            Console.WriteLine("You have enter an invald option");
            Console.WriteLine("Please enter number");
            number_str = Console.ReadLine();
            Console.Clear();


            num = Convert.ToInt32(number_str);
            //Console.Clear();
            //topmenu();
            //number_str = Console.ReadLine();
        }

        if (num >= 0)
        {

            while (num != 0) 
            {
                for (int i = num; i >= 1; i--)
                {
                    factorial = factorial * i;
                    Console.Write(i + " * ");

                }


                    Console.WriteLine("= "+factorial+ " which is factorial of " + number_str.ToString() );
                    Console.WriteLine("(please any key to return to main menu)");
                    Console.ReadKey();
                    Console.Clear();
                    topmenu();

            }

        }


        return factorial;




    }
}

}

Upvotes: 0

Views: 325

Answers (3)

PaulShovan
PaulShovan

Reputation: 2134

Just put these line inside do...while

int useroption;
useroption = topmenu();

rearrange as following...

int useroption;
        // excute while loop untill option is not 1-3
        do
        {

            useroption = topmenu();

and your program will work fine

The full code is here : http://pastebin.com/fCh0ttUY

Upvotes: 3

BenCr
BenCr

Reputation: 6042

The issue is that although you display topmenu again you never re-assign the value of useroption.

As gypsyCoder said, moving the display of your menu inside the do{}while() block will fix your issue because it will cause the useroption to be re-assigned each time round the loop.

Upvotes: 0

Robin Dijkhof
Robin Dijkhof

Reputation: 19278

First of al, set useroption to 0 after executing some code. Otherwise it will keep executing it. Second, ReadKey() right before your while statement. Otherwise you won't be able to read the input.

Upvotes: 0

Related Questions