0

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;




    }
}

}

1
  • 1
    Please try to make sample smaller (check sscce.org for guidance). For future posts try to avoid "thank you notes"/"new here" and other text not directly related to your problem (especially taking half of the post). Commented Oct 24, 2013 at 23:33

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

9 Comments

Once it loops back, if I try option 2, it makes me press enter more than once before going ahead with the factorial and after if I use option 3 (QUIT) It keeps on displaying the factorial but keeps increasing each time I press enter instead of Quiting..
Yes. I am also observing this problem. I am trying to provide you a solution
There were some silly mistakes in your program. I have solved those.
gypsyCoder I don't think you're not helping him in the long run by just fixing issues for him. Provide some context about what was wrong and why and how to fix it.
Ok @BenCr. I'll obey the rule
|
0

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.

2 Comments

Thank you for your help!! It appears to behaving a bit better but still not working right, did as you said but once it loops back to the main menu it requires the user to press enter extra few times not quite sure why tho.
and then when I try Option3 (QUIT) it keeps display the factorial result and increases it each time I try..
0

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.

2 Comments

Thank you.. still not working correct once I have moved and addes the following lines in the Do loop... useroption = 0; useroption = topmenu(); Console.ReadKey();
You're gonna have to be more descriptive. "Still not working" isn't enough information to help solve your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.