Vote count:
0
Hello I am having problem with switch 4 in the main. I narrow down the problem to if ((strcmp(name, temp->name)) == 0) in deleteByName.
For some reason it wont get past , if ((strcmp(name, temp->name)) == 0), and goes back to the switch 4 and ask the question printf("\nEnter the full name to delete , ending with a period ("".""):\n"); over and over and over again non stop really fast.
struct node {
char name[50];
int id;
struct node *next;
} *head;
void readInData();
void insert(char name[50], int id);
void deleteById(int id);
void deleteByName(char name[50]);
main(){
readInData();
int i, id;
char name[50];
while (1){
printf("Select an Option from the Table:\n\n");
printf("#1. Insert\n");
printf("#2. Display\n");
printf("#3. Delete by ID\n");
printf("#4. Delete by Name\n");
printf("#5. Exit\n\n");
scanf("%d", &i);
switch (i){
case 1:{
printf("\nEnter your full name and your ID separated by a comma:\n");
scanf("%[^,] , %i", name, &id);
insert(name, id);
}
break;
case 2:{
struct node *temp;
temp = head;
while (temp->next != NULL) {
printf("%s, %d\n", temp->name, temp->id);
temp = temp->next;
}
if (temp->next == NULL){
printf("%s, %d\n", temp->name, temp->id);
}
}
break;
case 3:{
printf("Enter the ID to delete: \n");
scanf("%i", &id);
deleteById(id);
}
break;
case 4:{
printf("\nEnter the full name to delete , ending with a period ("".""):\n");
scanf("%[^.]", &name);
deleteByName(name);
}
break;
case 5:{
return(0);
}
break;
default:{
printf("Please enter a number from 1-5 \n");
}
break;
}
}
system("pause");
}
void deleteByName(char name[50]){
struct node *temp, *prev;
temp = (struct node *) malloc(sizeof(struct node));
temp = head;
while (temp != NULL)
{
if ((strcmp(name, temp->name)) == 0)
{
if (temp == head)
{
head = temp->next;
free(temp);
return 1;
}
else
{
prev->next = temp->next;
free(temp);
return 1;
}
}
else
{
prev = temp;
temp = temp->next;
}
}
return 0;
}
asked 31 secs ago
Switch case 4 keeps repeating
Aucun commentaire:
Enregistrer un commentaire