2 Replies - 1602 Views - Last Post: 18 November 2012 - 01:27 PM Rate Topic: -----

#1 bigbaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-November 12

Menu loop issue C#

Posted 18 November 2012 - 12:43 PM

Hi, I hope I am now posting this correctly.

I have created a BMI program in C# which is menu driven; the menu needs to be displayed repeatedly until the option to quit is selected. I have the program working and it is calculating both metric and imperial and it also exits when the number 3 is pressed which is great. However I cannot get the menu to display repeatedly.

Any guidance to help me resolve this would be greatly appreciated. "No errors showing on VSE 2010".


Sample Dialogue

Main Menu
1. Metric
2. Imperial
3. Quit

Enter choice : 1
Enter Height in meters 1.8
Enter Weight in Kg 60

Your BMI is 18.5

This indicates that you are of Normal weight. // the program works as it should to this point.

Main Menu // this is where the problem is, getting this "Main Menu" or block of code to repeat unless option 3 is selected.
1. Metric
2. Imperial
3. Quit





 {   // this is my attempt at the loop but i cant get it to work

                    int menuchoice = 0;

                    while (menuchoice != 3) // the loop stays in the loop until 3 is pressed?

                    switch (menuchoice)
                        {   
                             
                        
                        }            
                break;

                }


        }



    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Menu loop issue C#

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: Menu loop issue C#

Posted 18 November 2012 - 01:07 PM

Well after you have successfully printed the result, you just need to display the menu at the end of the loop and collect their choice before you go back to the top of the loop.

1) Present the menu
2) They enter their choice
3) You enter the while loop if they chose a valid value and not 3
   3a) Make the calculations
   3b) You show them their results
   3c) You present the menu again
   3d) Collect their choice and go to step 3 above
4) Exit the program



See how we do that above? You have a place where you show the menu before entering the loop, then another place where you show the menu at the end of the loop before going back to the top.

Because you have two places you call the menu, I always suggest you put the menu itself in its own function and have it return the number of the choice they made. That way you can avoid repeating the menu in two places. It is easier to maintain.

:)
Was This Post Helpful? 2
  • +
  • -

#3 bigbaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-November 12

Re: Menu loop issue C#

Posted 18 November 2012 - 01:27 PM

Thank you Martyr2 for your feedback. I will certainly have a bash at this now. Hopefully I will get it right this time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1