Thread: I need to build a menu with a loop + scanf (without else if) and i got stuck

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    2

    I need to build a menu with a loop + scanf (without else if) and i got stuck

    Hi guys i need your help.
    I am a C beginner and i have a few problems with my project in C for my school.


    I need to build a menu: Input 1 2 3 4 5 - for other inputs print "No!"


    1 - Printf logout - Done


    2 - Doing nothing just take you back to main menu - Done


    3 - Scanf a question "Are you sure you would like to place your order"
    The user need to input "Y" or "y" and right after that the program will quit (for other input at the scanf the program will be back to main menu) - I putted the inputs "Y" 'y" in the cases but it's wrong - I don't know how to work with scanf in a loop.


    4 - I need to know how much times the program backed to main menu and write the number of "people" that used it - like if the i pressed nine times on "1" - that means that i am ten person who is using the program - i need to use "i++" some how but i don't know how to..


    5- Quit the program - my problem here is that the Default printf "No!" combined with the option i don't know how to remove the printf here


    The code-
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    
    int main(void)
    {
        char ch;
    
    
        do {
            printf("\Enter number 1-5 : ");
            ch = getche();
            printf("\n");
            
    
    
            switch (ch){
    
    
            case '1':
                printf("logout\n");
                break;
    
    
            case '2':
                break;
    
    
            case '3':
                printf("Are you sure you would like to place your order ???\n");
                printf("Please enter one char: ");
                scanf("%d");
                break;
    
    
            case '4':
                printf("Your position in queue is %d\n");
                break;
    
    
            case '5':
                printf("");
    
    
            case 'y':
                printf("");
            
    
            case 'Y':
                printf("");
            
    
                /*    scanf("%c", &canceling);
    
    
    
    
        switch (canceling)
        {
        case 'Y':
        case 'y':
            printf("“Canceled");
            break;
    
    
    
    
            default:
            printf("Wrong input\n");
        }*/
                
    
            default:
                printf("No!\n"); 
                break;
        
            }
        } while (ch != '5');
        printf("");
    
    
        return 0;
    }
    Last edited by hitnoob; 12-01-2016 at 06:46 AM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by hitnoob View Post
    3 - Scanf a question "Are you sure you would like to place your order"
    The user need to input "Y" or "y" and right after that the program will quit (for other input at the scanf the program will be back to main menu) - I putted the inputs "Y" 'y" in the cases but it's wrong - I don't know how to work with scanf in a loop.
    Make sure your compiler warnings are maximized, and heed them.

    Code:
    main.c||In function 'main':|
    main.c|32|warning: too few arguments for format|
    You're missing something in that "scanf()" call. Also, "%d" is for integer, not character.

    When reading single characters in this fashion, here is something you need to be aware of: FAQ > How do I avoid a "dangling" newline when reading single character user input? - Cprogramming.com

    Quote Originally Posted by hitnoob View Post
    4 - I need to know how much times the program backed to main menu and write the number of "people" that used it - like if the i pressed nine times on "1" - that means that i am ten person who is using the program - i need to use "i++" some how but i don't know how to..
    You need to figure out where to increment that counter. Just visualize the flow of code, and see where it makes the most sense to include this increment.

    Also, I would suggest using a more descriptive variable name for this purpose, rather than "i". Perhaps "runCount" or some-such.

    Quote Originally Posted by hitnoob View Post
    5- Quit the program - my problem here is that the Default printf "No!" combined with the option i don't know how to remove the printf here
    Notice how some of your switches cases are missing a "break" statement.

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    2
    thank you for your time

    i tried another option with the scanf problem and now after i write Y/y The program is crashing can you help me with that?

    Code:
    
    #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    
    {
        char ch;
    
            case '3':
                printf("Are you sure you would like to place your order ???\n");
                printf("Please enter one char: ");
    
    
        do {
            printf("\Enter number 1-5 : ");
            ch = getche();
            printf("\n");
            
       
            switch (ch)
    {
    
    do
                {
                    scanf("%d");
                }
                    while (ch != 'Y');
                while (ch != 'y');
                break;
                
    default:
                printf("No!\n");
                break;
    
    
            }
        } while (ch != '5');
        
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You have the same problem with the "scanf()". Look at line 25 in the code you just posted, and compare that to line 53 in the code from the first post.

    You have other problems, which appear to be the result of adding code without thought. You should be developing the logic "by hand" on paper, and only then converting the logic into code.

    Also, ensure your code is properly formatted and indented.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stuck on scanf ?????
    By zmhnak in forum C Programming
    Replies: 4
    Last Post: 03-23-2013, 07:37 AM
  2. stuck with scanf %s
    By pipskie in forum C Programming
    Replies: 6
    Last Post: 01-21-2013, 09:02 AM
  3. How to build a flexible Game Menu..
    By ThLstN in forum Game Programming
    Replies: 1
    Last Post: 12-13-2008, 10:53 AM
  4. Help.. newbie for loop..stuck with the loop..
    By jochen in forum C Programming
    Replies: 15
    Last Post: 10-01-2007, 12:31 AM
  5. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM

Tags for this Thread