when i run the program the movie i add doesnt show when i display

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>


struct record
{
char movie[30];
record *next;
};

bool isEmpty(record *head);
char menu();
void insertAsFirstElement(record *&head, record *&last, char movie);
void insert(record *&head, record *&last, char movie);
void remove(record *&head, record *&last);
void showList(record *current);

bool isEmpty(record *head)
{
if(head == NULL)
return true;
else
return false;
}

char menu()
{
int choice;
system("cls");
printf("Welcome to the Movie Registry Menu.\n\n\n");
printf("*******************************************\n");
printf("* 1. Add a movie title *\n");
printf("* 2. Remove a movie title *\n");
printf("* 3. Show the movie record *\n");
printf("* 4. Exit *\n");
printf("*******************************************\n");

printf("Enter your choice from the menu : ");
scanf("%d", &choice);

return choice;
}



void insertAsFirstElement(record *&head, record *&last, char movie)
{
record *temp = new record;
temp->movie[30] = movie;
temp->next = NULL;
head = temp;
last = temp;


}

void insert(record *&head, record *&last, char movie)
{
if(isEmpty(head))
insertAsFirstElement(head, last, movie);
else
{
record *temp = new record;
temp->movie[30] = movie;
temp->next = NULL;
last->next = temp;
last = temp;
}

}

void remove(record *&head, record *&last)
{
if(isEmpty(head))
printf("The list is already empty. \n");
else if (head == last)
{
delete head;
head == NULL;
last == NULL;
}
else
{
record *temp = head;
head = head->next;
delete temp;
}

}

void showList(record *current)
{
if(isEmpty(current))
printf("The record is empty. \n");
else
{
printf("The record contains: \n");
while(current != NULL)
{
printf("%s\n", current->movie);
current = current->next;

}

}
system("\npause");
}

int main(void)
{
record *head = NULL;
record *last = NULL;
int choice;
char movie[30];

do{

choice = menu();

switch(choice)
{
case 1 : system("cls");
printf("Please enter the movie title: ");
scanf("%s",&movie);
insert (head, last, movie[30]);
break;

case 2 : remove (head, last);
break;

case 3 : showList(head);
break;
default: printf("System exit\n");
}

}while(choice != '4');
system("\npause");
return 0;
}
Please use [code] tags.

Compiler warnings help an awful lot. You have already made some changes to your code to quiet the most important ones...

Lines 80 and 81 have no effect. (You are using the equality comparison operator [==] instead of the assignment operator [=].)

Line 127 complains because you are making an easy newbie mistake. It should read:

scanf("%s",movie);

However, you should be using fgets() to avoid problems with movie names having spaces in their title.

Unfortunately, your code has other issues that will interfere with that, so just make it read:

scanf(" %29[^\n]",movie);

That means 'skip past all leading whitespace, then read everything (including any additional whitespace) up to (but not including) the next newline, up to a maximum of 29 characters.' (Your array can only hold 30 characters max, and the last one must be a null character.)


Okay, that past, there are some other problems. You changed all your char* movie to char movie. Change it back. You'll have to properly deal with the errors on lines 51 and 66. Use the strcpy() function to copy the movie name. (You'll have to #include <string.h>.)

Relatedly, you'll also have to fix line 129. Currently it is passing the thirty-first character of your thirty-character array as argument. Don't do that. Just pass the array like normal:

insert (head, last, movie);


Finally, choice can never be equal to '4'. Think about this one.


Hope this helps.
still not any better , not entirely sure how to put in the strcpy in this situation

Topic archived. No new replies allowed.