Reputation: 23
I'm doing an exercise about the linked-list where I just started to learn it. It requires me to input the room number and the event of the hotel but when I try to print the output of both the room number and event it stop the program after I have choose the option of list all rooms from the menu. How can I fix this codes?
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
void insertRoom(int number, char name);
void listRoom(int number, char name);
struct Number {
int number;
char name;
struct Number *ptrnext;
};
struct Number *headptr, *newptr, *currentptr;
int main()
{
char ch, name;
int choice=TRUE, number;
headptr=NULL;
while(choice==TRUE) { //menu
printf("\n\ne - Enter room number and event");
printf("\nl - List all rooms");
printf("\nx - Exit\n");
printf("\nEnter choice: ");
scanf(" %c",&ch);
switch(ch) {
case 'e':insertRoom(number,name);break;
case 'l':listRoom(number,name);break;
case 'x': choice=FALSE; break;
default: printf("\nEnter only one from the above");
}
}
return 0;
}
void insertRoom(int number,char name) {
newptr=(struct Number *)malloc(sizeof(struct Number));
printf("\nEnter a number: ");
scanf("%d",&newptr->number);
printf("\nEnter name: ");
scanf(" %[^\n]s",&newptr->name);
if (headptr==NULL) {
headptr=newptr;
newptr->ptrnext=NULL;
}
else {
newptr->ptrnext=headptr;
headptr=newptr;
}
}
void listRoom(int number,char name) {
if (headptr==NULL) {
printf("\nEmpty list");
return;
}
currentptr=headptr;
do {
printf("\n\nRoom number \tEvent");
printf("\n%d \t%s",currentptr->number,currentptr->name);
printf("\n");
currentptr=currentptr->ptrnext;
} while(currentptr !=NULL);
}
Here is the output of the problem: output
Upvotes: 0
Views: 60
Reputation: 23
Just give the size for the name of the event when declaring the variable name to solve the problem
char name[30];
Upvotes: 1