I have done this countless times before and for some reason now I cannot seem to scanf a string in C. Here is my code for the specific function:
int putIn(NODE **head,NODE **current,NODE **prev) {
char tempName[40];
printf("Enter party name: ");
scanf("%s",tempName);
printf("Good?");
current=head;
int match=0;
printf("Hi");
while (*current !=NULL) {
printf("No");
if (strcmp((*current)->name,tempName)==0) {
printf("Name exists already");
match=1;
*current=(*current)->next;
}
}
printf("HI");
The function does not even get to the printf("Good"); statement. thanks for your help!
Edit: Here is all code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define NODE struct node
struct node {
char name[20];
int number;
NODE *next;
};
int counter;
int putIn(NODE **head,NODE **current,NODE **prev);
int delete(NODE **head,NODE **current,NODE **prev);
int show(NODE **head,NODE **current,NODE **prev);
NODE *head=NULL;
NODE *current=NULL;
NODE *prev=NULL;
char tempName[40];
int main() {
int whileLoop=0;
while(whileLoop==0) {
printf("Enter A Command: (1)Insert Party (2)Delete Party (3)Show List (4)Quit: ");
int selection;
scanf("%d",&selection);
switch(selection) {
case 1:
putIn(&head,¤t,&prev);
break;
case 2:
delete(&head,¤t,&prev);
break;
case 3:
show(&head,¤t,&prev);
break;
case 4:
whileLoop=1;
break;
default:
putIn(&head,¤t,&prev);
}
}
return 0;
}
int putIn(NODE **head,NODE **current,NODE **prev) {
//char tempName[40];
printf("Enter party name: ");
scanf("%s",tempName);
printf("Good?");
current=head;
int match=0;
printf("Hi");
while (*current !=NULL) {
printf("No");
if (strcmp((*current)->name,tempName)==0) {
printf("Name exists already");
match=1;
*current=(*current)->next;
}
}
printf("HI");
current=(NODE **)malloc(sizeof(NODE *));
if (*head==NULL) {
head=current;
}
(*current)->next=NULL;
strcpy((*current)->name,tempName);
printf("enter party size: ");
scanf("%d",&(*current)->number);
prev=head;
int i;
for (i=0;i<counter-1;i++){
*prev=(*prev)->next;
}
if (counter!=0) {
(*prev)->next=*current;
}
printf("%d",(*head)->number);
counter+=1;
return 0;
}
int delete(NODE **head,NODE **current,NODE **prev) {
int openTable;
printf("Enter open table size: ");
scanf("%d",&openTable);
current=head;
int match=0;
int grow=0;
while (*current!=NULL) {
if ((*current)->number<=openTable) {
match=1;
printf("A table of %d has been removed",(*current)->number);
prev=head;
int i;
for (i=0;i<grow-1;i++) {
*prev=(*prev)->next;
}
(*prev)->next=(*current)->next;
free(*current);
counter-=1;
} else {
grow+=1;
*current=(*current)->next;
}
}
return 0;
}
int show(NODE **head,NODE **current,NODE **prev) {
if (head==NULL) {
printf("\nNo data entered");
}
else {
printf("\nHere is the waiting list: \n");
}
current=head;
while (*current !=NULL) {
printf("Name: %s Size: %d\n",(*current)->name,(*current)->number);
*current=(*current)->next;
}
return 0;
}