I keep getting errors in my programming now it is giving me the unintinitlized local variable prev used. can anyone help me with this real fast, I have to go to bed soon.
Code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h> /* has the malloc prototype */
#include <string.h> /* has the strcpy prototype */
#define TSIZE 45 /* size of array to hold title */
struct film
{
char title[TSIZE];
int rating;
struct film * next; /* points to next struct in list */
};
void reverse(struct film* head)
{
if (head == NULL);
return;
reverse(head->next);
printf("Movie: %s Rating: %d\n", head->title, head->rating);
}
char * s_gets(char * st, int n);
int main(void)
{
struct film * head = NULL;
struct film * prev, *current;
char input[TSIZE];
/* Gather and store information */
puts("Enter first movie title:");
while (s_gets(input, TSIZE) != NULL && input[0] != '\0');
{
current = (struct film *) malloc(sizeof(struct film));
if (head == NULL) /* first structure */
head = current;
else /* subsequent structures */
prev ->next = current;
current->next = NULL;
strcpy_s(current->title, input);
puts("Enter your rating <0-10>:");
scanf_s("%d", ¤t->rating);
while (getchar() != '\n')
continue;
puts("Enter next movie title (empty line to stop):");
prev = current;
}
/* Show list of movies */
if (head == NULL)
printf("No data entered. ");
else
printf("\What order do you want to print your list? 1. Input order 2. Reverse input order :");
int choice;
scanf_s("%d", &choice);//reading choice from user
printf("Here is the movie list:\n");
if (choice == 1){
current = head;
while (current != NULL)
{
printf("Movie: %s Rating: %d\n",
current->title, current->rating); //it oprintd normally top to bottom
current = current->next;
}
}
else{
printf("\nPrinting in reverse order\n");
reverse(head);
}
/* Program done, so free allocated memory */
current = head;
while (current != NULL)
{
struct film * tmp = current->next;
free(current);
current = tmp;
}
printf("Bye!\n");
system("PAUSE");
return 0;
}