I need to create a linked list of customer data. One functions needs to insert a new customer data at the location indicated by the parameter "index". If index is 0 then it should be inserted as the first data in the linked list. I think I coded that function right, here it is:
Code:
int insertAtIndex(char * lastname, char * firstname, int id, float balance, int index)
{
int counter = 0;
struct Contact * newContact, *temp;
newContact = (struct Contact *) malloc(sizeof(struct Contact));
strcpy(newContact->last_name,lastname);
strcpy(newContact->first_name,firstname);
newContact->id = id;
newContact->balance = balance;
if(head == NULL)
{
head=newContact;
newContact->next = NULL;
}
else
{
temp = head;
while(counter != index)
{
temp=temp->next;
}
newContact->next=NULL;
temp->next=newContact;
}
return 0;
}
The second function just returns the number of entries in the linked list. Here is that function:
Code:
int size()
{
struct Contact *conductor;
conductor = head;
int total = 0;
while(conductor !=NULL)
{
total++;
conductor = conductor->next;
}
return total;
}
The third function prints all entries in the linked list it looks like this:
Code:
void displayLinkedList( )
{
struct Contact *conductor;
conductor = head;
while ( conductor != NULL )
{
printf("%s\n", conductor->last_name);
printf("%s\n", conductor->first_name);
printf("%d\n", conductor->id);
printf("%f\n", conductor->balance);
printf("\n");
conductor = conductor->next;
}
}
Now in my main function I have a switch case to act as a menu. I'm not sure if its my function causing the problems, or a logic error in my do-while switch loop. When I run my program I can always input my first choice fine. But when I try to input my second choice it just automatically goes to the default case in the switch. Here is that-
Code:
do
{
printf("What action would you like to perform?\n");
choice = getchar();
getchar(); //to flush '\n'
choice = toupper(choice);
switch (choice)
{
case 'A':
printf("Please enter a customer to add: \n");
printf("Please enter the last name: \n");
i = 0;
do
{
inputChar = getchar();
if (inputChar != '\n' && inputChar != '\r')
{
*(last_name + i) = inputChar;
}
i++;
} while (i < 50 && inputChar != '\n' && inputChar != '\r');
*(last_name + i) = '\0';
printf("Please enter the first name: \n");
i = 0;
do
{
inputChar = getchar();
if (inputChar != '\n' && inputChar != '\r')
{
*(first_name + i) = inputChar;
}
i++;
} while (i<50 && inputChar != '\n' && inputChar != '\r');
*(first_name + i) = '\0';
printf("Please enter the id(number): \n");
scanf("%d", &id);
printf("Please enter the balance: \n");
scanf("%f", &balance);
printf("Please enter the index to insert: \n");
scanf("%d",&index);
bool_index = insertAtIndex(last_name, first_name, id, balance, index);
if(bool_index < 0)
printf("The Customer could not be added\n");
else
printf("The Customer is added at %d\n", index);
break;
case 'D':
printf("The size of the linked list is %d\n", size());
break;
case 'P':
if(size() == 0)
printf("The list is empty\n");
else
displayLinkedList( );
break;
/*case 'R':
printf("Please enter an id(number) to remove: \n");
scanf("%d", &id);
bool_remove = removeCustomer(id);
if(bool_remove < 0)
printf("Customer with the id was not found");
else
printf("Customer was removed");
break;
case 'S': */
default:
printf("Unknown action\n");
break;
}
} while (choice != 'Q');
Any help would be appreciated.