The following section of code does not read the character in from the command line. Instead the execution skips this line and executes the action in the last else clause of the program. Does anyone have any ideas as to why scanf is failing here?
char c ;
printf("Would you like to alter anyone's details? n -for name, a - address, i - age, q - quit\n");
scanf("%c",&c); //this line is skipped
int j;
char* oldName;
char* newName;
char* newAdd;
struct person temp;
int newAge = 0;
printf("The char c is %c",c);
if(c == 'n')
{
printf("Enter the name of the person to change\n");
scanf("%s", oldName);
for(j = 0; j < 5; j++){
temp = people[j];
if(strcmp(temp.name,oldName) == 0)
break;
}
printf("Enter the new name to change o\n");
scanf("%s",newName);
temp.name = newName;
}
else if(c == 'a'){
printf("Enter the name of the person to change\n");
scanf("%s", oldName);
printf("Enter the new address to change to\n");
scanf("%s",newAdd);
for(j = 0; j < 5; j++){
temp = people[j];
if(strcmp(temp.name,oldName) == 0)
temp.address = newAdd;
}
}
else if(c=='i'){
printf("Enter the name of the person to change\n");
scanf("%s", oldName);
printf("Enter the new age to change to\n");
scanf("%i",&newAge);
for(j = 0; j < 5; j++){
temp = people[j];
if(strcmp(temp.name,oldName) == 0)
temp.age = newAge;
}
}
else if(c == 'q')
printf("Closing system\n");
else
printf("See ya later!"); //executes this line before exiting
return 0;
}
char c ;
printf("Would you like to alter anyone's details? n -for name, a - address, i - age, q - quit\n");
scanf("%c",&c); //this line is skipped
int j;
char* oldName;
char* newName;
char* newAdd;
struct person temp;
int newAge = 0;
printf("The char c is %c",c);
if(c == 'n')
{
printf("Enter the name of the person to change\n");
scanf("%s", oldName);
for(j = 0; j < 5; j++){
temp = people[j];
if(strcmp(temp.name,oldName) == 0)
break;
}
printf("Enter the new name to change o\n");
scanf("%s",newName);
temp.name = newName;
}
else if(c == 'a'){
printf("Enter the name of the person to change\n");
scanf("%s", oldName);
printf("Enter the new address to change to\n");
scanf("%s",newAdd);
for(j = 0; j < 5; j++){
temp = people[j];
if(strcmp(temp.name,oldName) == 0)
temp.address = newAdd;
}
}
else if(c=='i'){
printf("Enter the name of the person to change\n");
scanf("%s", oldName);
printf("Enter the new age to change to\n");
scanf("%i",&newAge);
for(j = 0; j < 5; j++){
temp = people[j];
if(strcmp(temp.name,oldName) == 0)
temp.age = newAge;
}
}
else if(c == 'q')
printf("Closing system\n");
else
printf("See ya later!"); //executes this line before exiting
return 0;
}