Thread: Program Crashing when storing a value of 0

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    12

    Program Crashing when storing a value of 0

    Hey guys,
    I'm working on a project for my programming class, and for the part I'm working on, I ask the user if they want to repeat the sequence again or return to the menu. If the user enters 1 it will repeat the sequence, if 0, it will return to the menu. However, for some reason it will not read 0 when I type it in. If I type 1, everything works fine, it just doesn't seem to like the 0. I have the sort of code for a different function and everything works fine, I have no idea what's going on here, and it's really bugging me, maybe you can help.

    This is the error:
    Type 1 to return to the search or 0 to return to the drill database.
    0
    Program received signal: “EXC_BAD_ACCESS”.
    sharedlibrary apply-load-rules all
    warning: Unable to read symbols for "" (file not found).
    warning: Unable to read symbols from "" (not yet mapped into memory).

    And this is the code for the function:
    Code:
    int drill_search(void)
    {
    	int choice, i, j, wtf;
    	char drill[SIZE][SIZE];
    	char garbage[2];
    	
    	printf("Enter the critera for your search:\n");
    	printf("1. Title of drill\n");
    	printf("2. Muscles worked\n");
    	printf("3. Strokes used\n");
    	scanf("%d", &choice);
    	
    	do {
    		switch (choice) {
    			case 1: 
    				printf("Enter the name of the drill:\n");
    				fgets(garbage, 2, stdin);
    				fgets(drill[0],50,stdin);
    				if (drill[0][strlen(drill[0])-1]=='\n')
    					drill[0][strlen(drill[0])-1]='\0';
    				
    				drill_info(drill, choice);
    				
    				/* resets the array */
    				for (i=0; i< SIZE; i++) {	
    					garbage[i]='\0';
    					for (j=0; j<SIZE; j++)
    						drill[i][j]='\0';
    				}				
    				break;
    			case 2:
    				printf("2");
    				break;
    			case 3:
    				printf("3");
    				break;
    			default:
    				return(1);
    				choice = 1;
    				break;
    		}
    		
    		printf("Type 1 to return to the search or 0 to return to the drill database.\n");
    		scanf("%d", &wtf);
    		
    		if (wtf == 0)
    			return 1;
    		
    	} while (wtf == 1);
    
    	return(0);
    }
    The bold is what I'm having trouble with.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for (i=0; i< SIZE; i++) {	
        garbage[i]='\0';
    Unless SIZE just happens to be 2, you're doin' it wrong.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    12
    WOWWWWWWWWWWWww

    I had that separated at first but I thought it would save time by just throwing it in there, didn't realize it would mess up everything! Thank you so much!

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by mdekom12 View Post
    The bold is what I'm having trouble with.
    Maybe, maybe not. The bold area could just be a symptom of something else. You are returning a value and all sorts of neat stuff happen to the stack when you exit a function, it's possible you've trashed said stack by some other operation.

    [edit]Yep, quzah beat me to it.[/edit]
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program crashing
    By bijan311 in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2010, 09:55 AM
  2. Program crashing at fgets() - any ideas why?
    By avi2886 in forum C Programming
    Replies: 4
    Last Post: 03-06-2009, 09:24 PM
  3. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  4. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM