segmentation fault using scanf
Posted
by
agarrow
on Stack Overflow
See other posts from Stack Overflow
or by agarrow
Published on 2012-11-19T04:57:10Z
Indexed on
2012/11/19
4:59 UTC
Read the original article
Hit count: 151
c
|segmentation-fault
noob question here: I'm trying to write a simple menu interface, but I keep getting a segmentation fault error and I can't figure out why.
#include <stdlib.h>
#include <stdio.h>
int flush(); int add(char *name, char *password, char *type); int delete(char *name);
int edit(char *name, char *password, char *type, char *newName, char *newPassword, char *newType);
int verify(char *name, char *password);
int menu(){
int input;
char *name, *password, *type, *newName, *newPassword, *newType;
printf("MAIN MENU \n ============\n");
printf("1. ADD\n");
printf("2. DELETE\n");
printf("3. EDIT\n");
printf("4. VERIFY\n");
printf("5. Exit\n");
printf("Selection:");
scanf("%d", &input);
flush();
switch (input){
case 1:
printf("%s\n", "Enter Name:");
scanf("%s", name);
flush();
printf("%s\n", "enter password" );
scanf("%s", password);
flush();
printf("%s\n","enter type" );
scanf("%s",type);
add(name, password, type);
menu();
break;
case 2:
printf("Enter Name:" );
scanf("%s",name);
flush();
delete(name);
menu();
break;
case 3:
printf("Enter Name:\n");
scanf("%s",name);
flush();
printf("Enter Password\n");
scanf("%s", password);
flush();
printf("enter type:\n");
scanf("%s", type);
flush();
printf("enter your new username:\n");
scanf("%s",newName);
flush();
printf("enter your new password\n");
scanf("%s", newPassword);
flush();
printf("enter your new type\n");
scanf("%s",newType);
flush();
edit(name, password, type, newName, newPassword, newType);
menu();
break;
case 4:
printf("Enter Name\n");
scanf("%s",name);
flush();
printf("Enter Password\n");
scanf("%s",password);
flush();
verify(name, password);
menu();
break;
case 5:
return 0;
default:
printf("invalid input, please select from the following:\n");
menu();
}
return 0;
}
int flush(){
int ch;
while ((ch = getchar()) != EOF && ch != '\n') ;
return 0;
}
I get the segmentation fault after entering two fields, in any menu option
© Stack Overflow or respective owner