Thread: Really close to solve an exercise. I have an easy question of switch case

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    101

    Post Really close to solve an exercise. I have an easy question of switch case

    Hi,

    I am now with structures. After a hard day, I have a program that is able to get a menu with players data: name, age, height. The program works perfectly but I need an improvement. To get the menu, I use switch case but with 0,1,2,3. I must use A,B,C and I have in mind using 0 to exit the program. This is a very easy question: if I put A, B, C, 0 I get an error that those are undefinied variables. How can I make to put A,B,C in each case instead of 1,2,3? This is only a section of my code, the int main ()

    Code:
    int main()
    {    
        struct players data[10];
        int n, choice;
    
    
        printf("Insert the number of registrations of players: ");
        scanf("%d", &n);
        read_players(data, n);
        do {
            printf("\nMenu :\n");
            printf("Press 1 to print every name.\n");
            printf("Press 2 to print every age.\n");
            printf("Press 3 to print every height.\n");
            printf("Press 0 to exit\n");
            printf("\nInsert an option (1-3), 0 to exit : ");
            scanf("%d", &choice);
            switch (choice)
            {
                case 1:
                    print_names(data, n);
                    break;
                case 2:
                    print_ages(data,n);
                    break;
                case 3:
                    print_heights(data,n);
                    break;
            }
        }
        while (choice != 0);
    
    
        return 0;
    }
    This is only int main () if you need all the code (which is ok) I can put it. I need to substitute case 1, case 2, case 3 with case A, case B, case C (0 to exit).

    Thank you in advance.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    if I put A, B, C, 0 I get an error that those are undefinied variables.
    If you put A, B, C, or 0 where and how exactly? Do you know how to use character constants?

    Jim

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    101
    Jim, you know that I suck with chars and strings

    This is my idea:

    Code:
    int main()
    {    
        struct players data[10];
        int n, choice;
    
    
        printf("Insert the number of registrations of players: ");
        scanf("%d", &n);
        read_players(data, n);
        do {
            printf("\nMenu :\n");
            printf("Press A to print every name.\n");
            printf("Press B to print every age.\n");
            printf("Press C to print every height.\n");
            printf("Press 0 to exit\n");
            printf("\nInsert an option (1-3), 0 to exit : ");
            scanf("%d", &choice);
            switch (choice)
            {
                case A:
                    print_names(data, n);
                    break;
                case B:
                    print_ages(data,n);
                    break;
                case C:
                    print_heights(data,n);
                    break;
            }
        }
        while (choice != 0);
    
    
        return 0;
    }
    I know I need to declare A,B,C. I'm not quite sure if I know enough of character constants, is something like... const char... and that's all I know.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    So you then you don't realize that in "case A:" the compiler thinks you're trying to use a variable instead of a character constant like 'A'?

    What book are you using that doesn't explain the difference between a variable name, a character constant, and a string constant?

    Jim

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    101
    Yes, I know it, if I change 1 to A, 2 to B, 3 to C I know the compiler thinks I am trying to use a variable. So, I guess I have to declare this variable. Is the first time I use letters instead of numbers in switch case.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Did you even read my last post?

    You should use a character constant instead of a variable.

    Please answer the last question in my last post.


    Jim

  7. #7
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by jimblumberg View Post
    Did you even read my last post?

    You should use a character constant instead of a variable.

    Please answer the last question in my last post.


    Jim
    I'm not following a book, I am making a course. This is my last attempt:

    Code:
    int main()
    {    
        struct players data[10];
        int n;
        char choice;
    
    
        printf("Insert the number of registrations of players: ");
        scanf("%d", &n);
        read_players(data, n);
        do {
            printf("\nMenu :\n");
            printf("Press 'A' to print every name.\n");
            printf("Press 'B' to print every age.\n");
            printf("Press 'C' to print every height.\n");
            printf("Press '0' to exit\n");
            printf("\nInsert an option (A-C), 0 to exit : ");
            scanf("%c", &choice);
            switch (choice)
            {
                case 'A':
                    print_names(data, n);
                    break;
                case 'B':
                    print_ages(data,n);
                    break;
                case 'C':
                    print_heights(data,n);
                    break;
            }
        }
        while (choice!=0);
    
    
        return 0;
    }
    I'm not convinced with the output:

    Menu :
    Press A to print every name
    Press B to print every age
    Press C to print every height
    Press 0 to exit

    Insert an option (A-C), 0 to exit :
    Menu :
    Press A to print every name
    Press B to print every age
    Press C to print every height
    Press 0 to exit

    Moreover, the program doesn't exit with 0...

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I'm not following a book, I am making a course.
    What does "making a course" mean?

    I'm not convinced with the output:
    What does that mean?
    Try having scanf() skip the leading whitespace by placing a space before your specifier:
    Code:
    scanf(" %c", &choice); // Note the space.
    Moreover, the program doesn't exit with 0...
    You can't have it both ways, you either need to use character constants or numeric constants, you must match the type of the variable you used in your switch().

    Jim

  9. #9
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by jimblumberg View Post
    What does "making a course" mean?
    I am an unemployed engineer, so I am taking a course to gain some programming skills. I paid money for taking a 5-hour class a week and get lots of exercises to solve each week, not much funy but stressful. I decided to take C because seems to be useful to understand other languages as well.

    I finally solved this exercise. This is the part of the code we are talking about:

    Code:
    int main()
    {    
        struct players data[10];
        int n;
        char choice;
    
    
    
    
        printf("Insert the number of registrations of players: ");
        scanf("%d", &n);
        read_players(data, n);
        do {
            printf("\nMenu :\n");
            printf("Press 'A' to print every name.\n");
            printf("Press 'B' to print every age.\n");
            printf("Press 'C' to print every height.\n");
            printf("Press 'E' to exit\n");
            printf("\nInsert an option (A-C), E to exit : ");
            scanf(" %c", &choice);
            switch (choice)
            {
                case 'A':
                    print_names(data, n);
                    break;
                case 'B':
                    print_ages(data,n);
                    break;
                case 'C':
                    print_heights(data,n);
                    break;
            }
        }
        while (choice!='E');
    
    
    
    
        return 0;
    }
    Thank you for let me know I can't merge characters and numeric constants. My program now exits with E. I added a space in scanf but I would like to understand why this happened and what that space makes to prevent that the menu appears twice.

    Jorge

  10. #10
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    I'm gonna take a gamble..

    The
    Code:
    scanf("%d", &n);
    read an integer from the input buffer (standard input, STDIN), but left the '\n' (newline, return) character that you entered, is still in the buffer.

    Thus when you subsequently call
    Code:
    scanf("%c", &choice);
    You should get a '\n' char into the "choice" variable.

    What you've done by adding the extra space into
    Code:
    scanf(" %c", &choice);
    is to correct for that extra newline char.

  11. #11
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Remember, everything you write into the a buffered input stream, remains in the buffer, until it is read back out.*

    * This is a non-technical discussion, but at least holds for this case.

  12. #12
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Another option is to make "choice" an "int" variable, and simply use another "scanf(%d, &choice)". IIRC, the %d specifier is defined to skip any leading whitespace in the input buffer. However, for %c, no character are skipped, and the next unread character is simply returned immediately.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by MacNilly View Post
    Another option is to make "choice" an "int" variable, and simply use another "scanf(%d, &choice)". IIRC, the %d specifier is defined to skip any leading whitespace in the input buffer. However, for %c, no character are skipped, and the next unread character is simply returned immediately.
    I don't think I would do that. The cases are characters, so making the conversion a %d would not work and be confusing. The %d conversion does not play nice with letters. For proof:
    Code:
    #include <stdio.h>
    int main(void)
    {
        int d;
        int rv;
    
        rv = sscanf("ABCAB", "%d", &d);
        if (rv != 1)
        {
            puts("conversion failed");
        }
        return 0;
    }
    
    conversion failed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone help me with this question (switch case)?
    By Rosh Ho in forum C Programming
    Replies: 1
    Last Post: 07-16-2016, 07:35 AM
  2. question about switch case
    By san12345 in forum C Programming
    Replies: 9
    Last Post: 02-26-2016, 02:04 PM
  3. Easy if-else OR switch-case
    By audinue in forum Tech Board
    Replies: 5
    Last Post: 03-22-2009, 02:33 PM
  4. switch case question
    By dantegl36 in forum C Programming
    Replies: 2
    Last Post: 12-04-2006, 11:12 AM
  5. Switch Case Question
    By dnysveen in forum C++ Programming
    Replies: 12
    Last Post: 06-06-2006, 05:34 AM

Tags for this Thread