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);
}