Thread: Switch case statement does not execute

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    5

    Switch case statement does not execute

    Im going through a C tutorial and im going to create a Database as part of the course. Im just in the very beginning of the project so its a very simple program so far.

    I was trying to test it out but there seems to various problems with it and im at loss what it could be.

    Code:
    #include<stdio.h>
    
    main()
    {
        /*Variabler för att lagra information om skiva*/
        char title[200];
        char artist[100];
        int tracks;
        int price;
        int albsing;
    
    
        /*Variabler för menyknappar*/
        int menukey;
        int storemenukey;
        int readmenukey;
    
    
        /*tillfällig variabel. används i switch case*/
        char aob;
    
    
    
    
    
    
        /*Huvudmeny med 2 val: Antingen lagra ny skiva eller hämta information om en redan lagrad skiva*/
        printf("MAIN MENU\nSelect operation: 1 to store, 2 to read\n");
        scanf("%d",&menukey);
    
    
    
    
        switch (menukey)
        {
    
    
        /*Meny för att lagra ny information om skiva. Lämnar default som break tillsvidare*/
        case 1:
            printf("Select operation: 1.Title  2.Artist  3.Tracklist  4.Price  5.Album/Single\n");
            scanf("%d",storemenukey);
    
    
            switch (storemenukey)
            {
            case 1:
                printf("Enter album title:\n");
                scanf("%s",title);
                break;
            case 2:
                printf("Enter artist name:\n");
                scanf("%s",artist);
                break;
            case 3:
                printf("Enter number of tracks on the record:\n");
                scanf("%d",&tracks);
                break;
            case 4:
                printf("Enter price:\n");
                scanf("%d",&price);
                break;
            case 5:
                /*Sätter albsing till 1 om album och 0 om singel*/
                printf("Is this record A - an album or B - a single. Choose A or B:\n");
                scanf("%c",&aob);
                if (strcmp(aob,"A") == 0)
                    albsing = 1;
                else if (strcmp(aob,"B") == 0)
                    albsing = 0;
                else
                break;
            default:
                break;
            }
        break;
    
    
    
    
        /*Meny för att hämta information om skiva*/
        case 2:
            printf("Select operation: 1.Title  2.Artist  3.Tracklist  4.Price  5.Album/Single\n");
            scanf("%d",readmenukey);
    
    
            switch (readmenukey)
            {
            case 1:
                printf("%s\n",title);
                break;
            case 2:
                printf("%s\n",artist);
                break;
            case 3:
                printf("%d\n",tracks);
                break;
            case 4:
                printf("%d\n",price);
                break;
            case 5:
                if (aob == 0)
                    printf("Single\n");
                else
                    printf("Album\n");
    
    
                break;
            default:
                break;
            }
            break;
        default:
            break;
        }
    }
    The first switch case statement takes me to the menu of choice, 1 or 2. However, chosing menu option one and trying to input a title or artist name, the program crashes.

    Next, going into menu option 2 and then trying to print out, for example, title nothing happends. The process just ends.

    Again, im sure this simple prototype is full of errors so i figured id turn to you guys for help.

    Thanks in advance!

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code:
    scanf("%d",storemenukey);
    You know this is wrong

    Also, you use strcmp, without having the string.h included. However, since you check only characters, you could do the comparison like this
    Code:
    if(myChar == 'A')
    EDIT: You do not use strcmp as it should be used. aob is a char, and strcmp allows as arguments char *
    Last edited by std10093; 02-11-2013 at 04:14 PM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch-case statement not working properly
    By Shinzu911 in forum C Programming
    Replies: 11
    Last Post: 04-25-2012, 10:41 AM
  2. HELP! How do I execute a function in a switch statement??
    By kkmoslehpour in forum C Programming
    Replies: 34
    Last Post: 10-13-2011, 08:40 PM
  3. can you place a if/else statement in a switch case
    By swansea in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2010, 08:59 PM
  4. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  5. switch case statement
    By stanlvw in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 05:06 AM