Thread: Switch Menu help

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    33

    Switch Menu help

    I'm using a switch for an ATM machine program. Every time I select an option for my switch it just reprints the menu. Did I mess something up within the cases? I've also tried putting case '1': and so on which did not work either.

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        int balance, pin, tempPin, choice, choice2, choice3=0;
        char ans;
    
    
        printf("Please enter a pin:\n");
        printf("The beginning number must not start with 0\n");
        scanf("%d", &tempPin);
    
    
        if(tempPin < 1000){
            printf("Invalid Pin");
            return 0;
            }
    
    
        printf("Would you like a receipt?\n");
        printf("y or Y -> Yes\n");
        printf("n or N-> No\n");
        scanf("%c\n", &ans);
    
    
    
    
        do{
        system("cls\n");
        printf("Choose from the following:\n");
        printf("1- Fast Cash\n");
        printf("2- Withdraw\n");
        printf("3- Deposit\n");
        printf("4- Check Balance\n");
        printf("5- Get Card Back\n");
    
    
    
    
    	scanf("%c\n", &choice);
        switch (choice){
        case 1:
                printf("Fast Cash: Select from the following\n");
                printf("1-- $20.00      2-- $40.00\n");
                printf("3-- $80.00      4-- $100.00\n");
                scanf("%c", &choice2);
                    switch(choice2){
                    case '1':
                            balance-=20;
                    case '2':
                            balance-=40;
                    case '3':
                            balance-=80;
                    case '4':
                            balance-=100;
                        }
                printf("1- Another Transaction  2- Get Card Back\n");
                scanf("%d\n", &choice3);
                if(choice3 == 2){
                    printf("Please take your card.\n");
                    break;
                }
        case 2:
                system("cls\n");
                int amtWithdraw;
                printf("Enter an amount (enter 0 to cancel):");
                scanf("%d", &amtWithdraw);
                balance-=amtWithdraw;
                printf("1- Another Transaction  2- Get Card Back\n");
                scanf("%d\n", &choice3);
                if(choice3 == 2){
                    printf("Please take your card.\n");
                    break;
                }
        case 3:
                system("cls\n");
                int amtDeposit;
                printf("Enter amount to deposit:");
                scanf("%d", amtDeposit);
                balance += amtDeposit;
                printf("1- Another Transaction  2- Get Card Back\n");
                scanf("%d\n", &choice3);
                if(choice3 == 2){
                    printf("Please take your card.\n");
                    break;
                }
        case 4:
                system("cls\n");
                printf("Your Balance is: $%d\n", balance);
                printf("1- Another Transaction  2- Get Card Back\n");
                scanf("%d\n", &choice3);
                if(choice3 == 2){
                    printf("Please take your card.\n");
                    break;
                }
    
    
        case 5:
                printf("Please take your card.\n");
                break;
            }
        }
        while(choice3 != 2);
    	return (0);
    
    
    
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Be sure you are compiling with full warnings.

    Code:
    /*
    main.c||In function 'main':|
    main.c|30|warning: implicit declaration of function 'system'|
    main.c|41|warning: format '%c' expects type 'char *', but argument 2 has type 'int *'|
    main.c|47|warning: format '%c' expects type 'char *', but argument 2 has type 'int *'|
    main.c|80|warning: format '%d' expects type 'int *', but argument 2 has type 'int'|
    main.c|6|warning: unused variable 'pin'|
    main.c|80|warning: 'amtDeposit' may be used uninitialized in this function|
    ||=== Build finished: 0 errors, 6 warnings ===|
    */
    You should be compiling and testing as you go, building your program up gradually. There are quite a few mistakes scattered across the code that should have been caught earlier on.

    Try increasing your warning level and start by fixing those, and see how far you get.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    33
    Quote Originally Posted by Matticus View Post
    Be sure you are compiling with full warnings.

    Code:
    /*
    main.c||In function 'main':|
    main.c|30|warning: implicit declaration of function 'system'|
    main.c|41|warning: format '%c' expects type 'char *', but argument 2 has type 'int *'|
    main.c|47|warning: format '%c' expects type 'char *', but argument 2 has type 'int *'|
    main.c|80|warning: format '%d' expects type 'int *', but argument 2 has type 'int'|
    main.c|6|warning: unused variable 'pin'|
    main.c|80|warning: 'amtDeposit' may be used uninitialized in this function|
    ||=== Build finished: 0 errors, 6 warnings ===|
    */
    You should be compiling and testing as you go, building your program up gradually. There are quite a few mistakes scattered across the code that should have been caught earlier on.

    Try increasing your warning level and start by fixing those, and see how far you get.
    I'm not sure how to turn on full warnings. I'm using Code Blocks

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Try going to "Settings -> Compiler and Debugger", and look in the "Compiler Flags" area for options to increase your warnings.

    For instance, if you're using the GCC compiler with Code Blocks, enable "all compiler warnings" [-Wall] and "extra compiler warnings" [-Wextra].

    Note that, in Code Blocks, you won't see any warnings in the log if you do "Build and Run" - you have to just do "Build" (or "compile current file") to be able to see the warnings.

  5. #5
    Registered User
    Join Date
    Dec 2014
    Posts
    33
    Quote Originally Posted by Matticus View Post
    Try going to "Settings -> Compiler and Debugger", and look in the "Compiler Flags" area for options to increase your warnings.

    For instance, if you're using the GCC compiler with Code Blocks, enable "all compiler warnings" [-Wall] and "extra compiler warnings" [-Wextra].

    Note that, in Code Blocks, you won't see any warnings in the log if you do "Build and Run" - you have to just do "Build" (or "compile current file") to be able to see the warnings.
    thanks for this. Never knew that

    so I deleted things out to try and make it more simple while i figured out the switch. It just runs through and prints out everything without giving the user to enter an input from the menu

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    int main(){
    
    
        int balance=0;
        int tempPin;
        int choice=0;
        int choice2=0;
        int amtDeposit, amtWithdraw;
        char ans;
    
    
        printf("Please enter a pin:\n");
        printf("The beginning number must not start with 0\n");
        scanf("%d", &tempPin);
    
    
        if(tempPin < 1000){
            printf("Invalid Pin");
            return 0;
            }
    
    
        printf("Would you like a receipt?\n");
        printf("y or Y -> Yes\n");
        printf("n or N-> No\n");
        scanf("%c\n", &ans);
    
    
        printf("Choose from the following:\n");
        printf("1- Fast Cash\n");
        printf("2- Withdraw\n");
        printf("3- Deposit\n");
        printf("4- Check Balance\n");
        printf("5- Get Card Back\n");
    
    
        scanf("%d", &choice);
        switch(choice){
        case 1:
                printf("Fast Cash: Select from the following\n");
                printf("1-- $20.00      2-- $40.00\n");
                printf("3-- $80.00      4-- $100.00\n");
                scanf("%d", &choice2);
                    switch(choice2){
                    case 1:
                            balance-=20;
                            break;
                    case 2:
                            balance-=40;
                            break;
                    case 3:
                            balance-=80;
                            break;
                    case 4:
                            balance-=100;
                            break;
                        }
                    break;
    
    
        case 2:
                printf("Enter an amount (enter 0 to cancel):");
                scanf("%d", &amtWithdraw);
                balance-=amtWithdraw;
                    break;
    
    
        case 3:
                printf("Enter amount to deposit:");
                scanf("%d", &amtDeposit);
                balance += amtDeposit;
                    break;
    
    
        case 4:
                printf("Your Balance is: $%d\n", balance);
                    break;
    
    
        case 5:
                printf("Please take your card.\n");
                    break;
    
    
        default:
                printf("Invalid option\n");
                    break;
            }
    
    
    	return (0);
    }

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    On line 29, you don't need the newline ("\n") in the format string of "scanf()".

    If you get rid of that, you'll encounter another problem. When you get there, read this: FAQ > How do I avoid a "dangling" newline when reading single character user input? - Cprogramming.com

  7. #7
    Registered User
    Join Date
    Dec 2014
    Posts
    33
    I'm trying to figure out how to go back to the beginning of the menu after a case is selected. so when it prompts to do another transaction or get card out. When selecting 1 to do another transaction it just stays there without reprinting the prompt. just a little weird behavior but it does end up working.

    Also the 4 digit pin, not sure how to allow a user to enter 0000 or start the pin with a 0 and after 3 tries of entering a pin (if invalid) will kick the user out


    Code:
    do{
        printf("Choose from the following:\n");
        printf("1- Fast Cash\n");
        printf("2- Withdraw\n");
        printf("3- Deposit\n");
        printf("4- Check Balance\n");
        printf("5- Get Card Back\n");
    
    
        scanf(" %d", &choice);
        switch(choice){
        case 1:
                printf("Fast Cash: Select from the following\n");
                printf("1-- $20.00      2-- $40.00\n");
                printf("3-- $80.00      4-- $100.00\n");
                scanf("%d", &choice2);
                    switch(choice2){
                    case 1:
                            balance-=20;
                            break;
                    case 2:
                            balance-=40;
                            break;
                    case 3:
                            balance-=80;
                            break;
                    case 4:
                            balance-=100;
                            break;
                        }
                printf("1- Another Transaction  2- Get Card Back\n");
                scanf("%d\n", &choice3);
                if(choice3 == 1)
                    break;
                else
                    printf("Please take your card.\n");
    
    
        case 2:
                printf("Enter an amount (enter 0 to cancel):");
                scanf("%d", &amtWithdraw);
                if(amtWithdraw==0)
                    break;
                balance-=amtWithdraw;
                printf("Your balance is now $%d\n", balance);
                printf("1- Another Transaction  2- Get Card Back\n");
                scanf("%d\n", &choice3);
                if(choice3 == 1)
                    break;
                else{
                    printf("Please take your card.\n");
                    break;
                }
    
    
        case 3:
                printf("Enter amount to deposit:");
                scanf("%d", &amtDeposit);
                balance += amtDeposit;
                printf("Your balance is now $%d\n", balance);
                printf("1- Another Transaction  2- Get Card Back\n");
                scanf("%d\n", &choice3);
                if(choice3 == 1)
                    break;
                else{
                    printf("Please take your card.\n");
                    break;
                }
    
    
        case 4:
                printf("Your Balance is: $%d\n", balance);
                printf("1- Another Transaction  2- Get Card Back\n");
                scanf("%d\n", &choice3);
                if(choice3 == 1)
                    break;
                else{
                    printf("Please take your card.\n");
                    break;
                }
    
    
        case 5:
                printf("Please take your card.\n");
                    break;
    
    
        default:
                printf("Invalid option\n");
                    break;
            }
        }
        while(choice3!=2);
    	return (0);

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    In cases like this, you should post a complete and compilable program, rather than a snippet, just to make sure there are no problems with the parts that weren't included.

    Quote Originally Posted by tlxxxsracer View Post
    I'm trying to figure out how to go back to the beginning of the menu after a case is selected. so when it prompts to do another transaction or get card out. When selecting 1 to do another transaction it just stays there without reprinting the prompt. just a little weird behavior but it does end up working.
    You should once again heed my advice from before:

    Quote Originally Posted by Matticus View Post
    ...you don't need the newline ("\n") in the format string of "scanf()".


    Quote Originally Posted by tlxxxsracer View Post
    Also the 4 digit pin, not sure how to allow a user to enter 0000 or start the pin with a 0 and after 3 tries of entering a pin (if invalid) will kick the user out
    Interesting thing about programming - just because data consists of numbers, does not mean it should necessarily be treated as a number. Some data (e.g. pin numbers, zip codes, phone numbers) can be represented with strings. This is especially true when the data does not require mathematical manipulation - and even more so when an input of "0000" is intended to mean "0000" and not 0.

  9. #9
    Registered User
    Join Date
    Dec 2014
    Posts
    33
    So I have sorted out the issue with the cases going back to the main menu and properly breaking when user wants to exit the program.
    I'm still puzzled and confused on the whole strings and using for the 4 digit pin and only accepting 3 tries.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Please describe the exact behavior you want for reading the pin number.

    Some things to consider:

    • What happens if the user enters less than or more than 4 digits?
    • Can a valid input start with one or more zeros?
    • What is considered "invalid"?

  11. #11
    Registered User
    Join Date
    Dec 2014
    Posts
    33
    Quote Originally Posted by Matticus View Post
    Please describe the exact behavior you want for reading the pin number.

    Some things to consider:

    • What happens if the user enters less than or more than 4 digits?
    • Can a valid input start with one or more zeros?
    • What is considered "invalid"?
    A pin can start with 0 or multiples.
    Pin must be 4 digits. To be a valid pin, 4 digit must be entered.
    After 3 tries the program boots the user out to start over.

    Currently I just have it that a user enters a number between 1000-9999, otherwise it states "invalid pin". This was more of a place holder for my program so I can come back to this

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    A pin can start with 0 or multiples.
    As I said earlier, if you want to keep leading zeros intact when the user enters a value, you should read the input as a string.
    FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com

    You can then verify that the input is valid.

    Pin must be 4 digits.
    Does the input contain exactly four characters? (Look into the "strlen()" [string length] function.)

    To be a valid pin, 4 digit must be entered.
    Does the input contain only numbers? (Look into the "isdigit()" function.)

    After 3 tries the program boots the user out to start over.
    Multiple tries implies a loop.
    Exactly 3 tries maximum implies a counter of some kind.

    Sit down with a pencil and paper, and work out each individual step you need to perform in order to complete this portion. This is how you will develop the logic, which can then be translated into code.

  13. #13
    Registered User
    Join Date
    Dec 2014
    Posts
    33
    Here's what I have so far..


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    int main(){
    int count=0;
    char tempPin[4];
    
    do{
        printf("Please enter a pin:\n");
        scanf(" %c", &tempPin);
        if (strlen(tempPin) != 4)
            printf("Invalid PIN\n");
            count++;
        if (count == 3){
            printf("Invalid PIN, please contact your bank.\n");
            return 0;
            }
        }while(count<=3);
    }

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Does it work? Or does it not work, and if so, what is the problem?

    Code:
    scanf(" %c", &tempPin);
    This reads a single character, not a string. See the link in my previous post for a good way to read a string from the user.

  15. #15
    Registered User
    Join Date
    Dec 2014
    Posts
    33
    Quote Originally Posted by Matticus View Post
    Does it work? Or does it not work, and if so, what is the problem?

    Code:
    scanf(" %c", &tempPin);
    This reads a single character, not a string. See the link in my previous post for a good way to read a string from the user.
    Thanks for the help. I was able to figure it out along with the rest of the code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch menu loop help
    By kebo in forum C Programming
    Replies: 0
    Last Post: 12-01-2015, 09:33 PM
  2. Not taker switch menu
    By Spartica in forum C Programming
    Replies: 4
    Last Post: 10-14-2013, 03:17 AM
  3. Switch statement menu
    By zaihed13 in forum C Programming
    Replies: 2
    Last Post: 09-20-2013, 08:37 PM
  4. Menu Switch Problem
    By software tester in forum Game Programming
    Replies: 2
    Last Post: 02-02-2011, 09:20 AM
  5. menu to switch between OS's
    By DavidP in forum Tech Board
    Replies: 3
    Last Post: 12-22-2002, 02:24 AM