Thread: help debugging my program

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    1

    Question help debugging my program

    my program terminated with the error "smashing stack detected".
    please help out a beginner thnx!

    below is the output:
    Code:
    ======================================
    Welcome to QWE Airline
    ======================================
    Welcome sir, your name? kenyi
    ###### SYSTEM MESSAGE ######
    + Select flight class:
    (1) First Class
    (2) Economy Class
    (3) Exit the program 
    ############################
    => 1
    *** stack smashing detected ***: ./a.out terminated
    Aborted (core dumped)
    and here is the source:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int getClass(void);
    int getDest(void);
    int getDest(void);
    void msg(const char *msg);
    float getTotal(int class, int dest, int numOfSeat);
    int validAns(int choice, const char *menu[]);
    
    const char INVALID_MENU[] = "Please enter a bracketed number displayed in the menu";
    const char QUIT[] = "Exit the program";
    const char BACK[] = "Back to previous";
    const char *classMenu[3] = {"First Class", "Economy Class", QUIT};
    const char *destMenu[6] = {"Mongolia", "England", "Arab Saudi", "Taiwan", BACK, QUIT};
    const float destPrice[4] = {544.50, 1001.99, 643.4, 320.99};
    const char *confirmMenu[3] = {"Yes", BACK, QUIT};
    
    int main(){
    	char clientName[21], strtemp[50];
    	int dest, class, numOfSeat, quit = 0, confirm, valid = 1;
    	float total;
    
    	printf("======================================\n"
    	       "Welcome to QWE Airline\n"
    	       "======================================\n");
    
    	printf("Welcome sir, your name? ");
    	scanf(" %[^\n]", clientName);
    
    	do{
    		class = getClass();
    
    		if( !validAns(class, classMenu) ){
    			msg(INVALID_MENU); 
    			continue;
    		}
    
    		if( strcmp(classMenu[class-1], QUIT) == 0){
    			quit = 1;
    			break;
    		}
    
    		sprintf(strtemp, "Sir, you\'ve selected %s.", classMenu[class-1]);
    		msg(strtemp);
    		do{
    			dest = getDest();
    			if( !validAns(dest, destMenu) ){
    				msg(INVALID_MENU);
    				continue;
    			}
    
    			if( strcmp(destMenu[dest-1], QUIT) == 0 ){
    				quit = 1;
    				break;
    			}
    
    			if( strcmp(destMenu[dest-1], BACK) == 0 ){
    				break;
    			}
    
    			msg("How many seats wanted?");
    			printf("=> ");
    			scanf("%d", &numOfSeat);
    
    			do{
    				valid = 1;
    				confirm = getConfirm();
    
    				if( !validAns(confirm, confirmMenu) ){
    					msg(INVALID_MENU);
    					valid = 0;
    				}
    			} while(!valid);
    
    			if( strcmp(confirmMenu[confirm-1], QUIT) == 0 ){
    				quit = 1;
    				break;
    			}
    
    			if( strcmp(confirmMenu[confirm-1], BACK) == 0 ){
    				continue;
    			}
    
    		} while(1);
    
    		total = getTotal(class, dest, numOfSeat);
    		printf("Information...\n");
    	} while( quit == 0 );
    
    	printf("GoodBye\n");
    
    	return 0;
    
    }
    
    int getClass(void){
    	char temp[20];
    	int class;
    
    	sprintf(temp,
    	        "Select flight class:\n"
    		"(1) %s\n"
    		"(2) %s\n"
    		"(3) %s",
    		classMenu[0],
    		classMenu[1],
    		classMenu[2]
    		);
    	msg(temp);
    
    	printf("=> ");
    	scanf("%d", &class);
    
    	return class;
    }
    
    int getDest(void){
    	return 0;
    }
    
    int getConfirm(void){
    	return 0;
    }
    
    void msg(const char *msg){
    	printf("###### SYSTEM MESSAGE ######\n"
    	       "+ %s \n"
    	       "############################\n",
    	       msg);
    }
    
    float getTotal(int class, int dest, int numOfSeat){
    	return 0.0;
    }
    
    int validAns(int ans, const char *menu[]){
    	return (menu[ans-1] != NULL);
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    check this link to see if you can pinpoint your problem
    http://www.de-brauwer.be/wiki/wikka....kka=StackSmash

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    char temp[20];
    int class;
    sprintf(temp,
    "Select flight class:\n"

    Try counting how many characters you're going to write here.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM