Thread: My Switch/case ends early, any ideas why? thank you

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    13

    My Switch/case ends early, any ideas why? thank you

    well guys and gals, my problem is this, im pretty new to programming but i was pretty confident that this code would work, because the individual parts worked separately just fine... everything was going great, then when it got to the line with append(ap) it started the while loop all over.
    Code:
    #include <stdio.h>
    
    void append(char an);
    
    int main(){
    	int choice1;
    	int choice2;
    	char ap;
    	printf("_________________________________________________________________________________________________\n\n\n\n");
    	
    	printf("Would you like to check inventory or update inventory?");
    	while(1){
    	printf("\n\n[1] check\n[2] update\n");
    	scanf("%d", &choice1);
    	switch (choice1){
    		case 1:
    			printf("\n this part hasn't been made yet");
    			break;
    		case 2:
    			printf("\nWould you like to add a new item or change an existing item?");
    			printf("\n\n[1] add\n[2] change\n");
    			scanf("%d", &choice2);
    			switch (choice2){
    				case 1:
    					printf("\nEnter the item you would like to add.");
    					ap=getchar();/*it breaks here, goes back to original switch(choice1), and then carries out append(ap for whatever you type even though it's no longer on this part of the program, any ideas?)*/
    					append(ap);
    					break;
    				case 2:
    					printf("\nnot yet made");
    					break;
    				default:
    					printf("\ninvalid entry, please re-enter:");
    					break;
    				}
    			break;
    		default:
    			printf("\ninvalid entry, please re-enter:");
    			break;
    		}
    	}
    	return 0;
    	}
    
    void append(char an){
    	FILE *f;
    	f=fopen("temp.txt", "a");
    	fprintf(f, "%c", an);
    	fclose(f);
    	}
    anyway i tried for a really long time to figure it out, but to no avail. so if anyone out there know what i did wrong, your help would be greatly appreciated. thankyou!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You have an extraneous break; statement at the exit of the inner switch.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    13

    Unhappy

    nope still doesnt work. no matter what i do to the code it cuts out what ever's after ap=getchar(); and moves on to what ever is next so i guess that's the problem, but i cant see the problem in those two or three line. 8 (

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    When you have scanf("%d", &choice2), you're asking the user to enter a number; but what he does is type in a number and then hit enter. scanf() reads the number, but leaves the newline (from the user hitting enter) hanging around, waiting to be read. So when you have your getchar() call, it sees the newline and reads it. You perceive it as skipping the call, but it doesn't. It just doesn't need to wait around for you to type something in because you already did.

    C input can be very messy. Probably the quickest way to get around your problem is to add a getchar() after your scanf(). There is no need to assign its return value to anything because you're just eating a newline.

    This is fragile and assumes the user input will be in a specific format, but that's the road you go down when you're using scanf().

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    13
    that makes sense, thank you so much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Project ideas.
    By Wraithan in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 04-25-2009, 03:29 PM
  2. Ideas, how do you get them? xD
    By Akkernight in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-22-2009, 03:53 AM
  3. cool ideas for a game
    By Shadow12345 in forum Game Programming
    Replies: 7
    Last Post: 05-18-2004, 08:37 PM
  4. idea's idea's idea's
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-29-2002, 12:30 AM
  5. Small app ideas
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 02-15-2002, 08:57 PM

Tags for this Thread