Thread: Creating A Menu

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    9

    Creating A Menu

    The menu I created for this project isn't seamless in the slightest.
    Below are the assignment guidelines, current code, then questions.
    Sample Output of teacher's program last.


    A. int main()
    This is your main program. main() should perform the following tasks:


    1. Print a welcome message.
    2. Print a menu of options, including one to quit the program.
    3. Prompt the user to choose a valid option.
    4. Get the choice from the user.
    5. Repeat this task until the user chooses a valid option.
    6. If the user chooses the option to quit, the program ends.
    7. If the user chooses a valid option, the function corresponding to that function
    is executed.
    8. Once the function returns, the menu is redisplayed and the user is again
    prompted to choose an option. Steps 2 through 7 are repeated until the
    program ends.


    Some specific coding requirements/suggestions:


    1. You must appropriately use a do/while loop in the main() function.
    2. You must appropriately use a switch statement in the main() function.


    Code:
    int main( )
    {
    	int a;	
    	printf("Hello and welcome to my project!\n");
    	do
    	{
    		printf("Please choose an option.\n\n1 - Sum of even integers\n2 - Sum of dollar amounts");
    		printf("\n3 - Factorial of a number\n\nQ - Quit\n\nEnter your choice: ");
    		scanf("%d",&a);
    		switch (a)
    		{
    			case 1:
    				sumIntegers ();
    				printf("=============================================================================\n");
    				break;
    			case 2:
    				sumDoubles ();
    				printf("=============================================================================\n");
    				break;
    			case 3:
    				calcFactorial ();
    				printf("=============================================================================\n");
    				break;
                            default:
    				printf("\nYour input was not recognized.\n\n");
    printf("=============================================================================\n");
    				break;
    		}
    	}while (a != 'Q');
    }

    Question) With each option corresponding to a number (1,2,3), how do I get Quit (Q) recognized?


    ***Sample Output***
    Welcome To Assignment #2


    Please choose from the following options:


    1 - Sum of even integers
    2 - Sum of dollar amounts
    3 - Factorial of a number
    Q - Quit


    Enter your choice (1, 2, 3, Q): 0
    *** You entered an invalid choice!!! ***


    Please choose from the following options:


    1 - Sum of even integers
    2 - Sum of dollar amounts
    3 - Factorial of a number


    Q - Quit


    Enter your choice (1, 2, 3, Q): 4
    *** You entered an invalid choice!!! ***


    Please choose from the following options:


    1 - Sum of even integers
    2 - Sum of dollar amounts
    3 - Factorial of a number


    Q - Quit


    Enter your choice (1, 2, 3, Q): q
    *** You entered an invalid choice!!! ***


    Please choose from the following options:


    1 - Sum of even integers
    2 - Sum of dollar amounts
    3 - Factorial of a number


    Q - Quit


    Enter your choice (1, 2, 3, Q): 1
    I want to sum up some even integers.


    Enter an integer: -44
    *** Negative numbers are invalid! ***


    Enter an integer: 6
    The even numbers up to 6 sum to 12.


    Thank you for playing!
    Press any key to continue . . .

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    So, what question are you asking here?

    This is not a site where you can just post a partially completed homework exercise in the hope that someone will tidy it up.

    More generally, read the site's homework policy, here and forum guidelines (which, really, you should have read before posting) here, taking particular note of point 4.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Question) With each option corresponding to a number (1,2,3), how do I get Quit (Q) recognized?
    you cannot get Q as an answer as long as you read an integer with scanf.
    Solution make a a char.
    you have to change the scanf() call to
    Code:
    scanf(" %c",&a);
    Did you notice the leading space in the format string ? It helps to skip the remaining '\n' when scanning for chars
    also you have to change the case statements to e.g
    Code:
    			case '1':
    				sumIntegers ();
    Kurt

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Another day, another cross-post
    Creating A C Menu - C And C++ | Dream.In.Code
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a menu C
    By DeanWinchester in forum C Programming
    Replies: 2
    Last Post: 01-05-2012, 04:07 PM
  2. Creating a C menu using Up Down
    By RockLee in forum C Programming
    Replies: 7
    Last Post: 03-15-2011, 06:02 PM
  3. creating menu
    By spikestar in forum C Programming
    Replies: 3
    Last Post: 09-19-2009, 11:16 PM
  4. Creating a window through menu
    By Homunculus in forum Windows Programming
    Replies: 17
    Last Post: 02-20-2006, 06:56 PM
  5. creating a menu using C
    By whitey82 in forum C Programming
    Replies: 11
    Last Post: 12-01-2005, 12:43 PM