Thread: Help please.

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    7

    Help please.

    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", &current->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;
    }
    Last edited by Salem; 12-27-2016 at 10:43 AM. Reason: fixed code tags

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Edit3: In C you can NOT declare variables anywhere in the code; you must do it only in the beginning of a block.
    Code:
    int choice;
    Code:
    if (head == NULL);
    return;
    You have a ";" after the ")" this is likely wrong.

    Edit: If you indent your code and post it correctly in code tags you are likely to get more help.
    Edit2: Decide if you are doing C or C++ programming! You should NOT include a C++ header in C code.

    Tim S.
    Last edited by stahta01; 12-26-2016 at 07:33 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    7
    I am using my textbook, and I was to copy the program and just add a loop in it to make it read the users input backward. SoI have to use the program as they have it setup. I can, however, show the indents didn't think it would have been that big of a deal not too since I'm sure I'm just missing something so simple. This is the error I am getting.
    Error 2 error C4700: uninitialized local variable 'prev' used



    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", &current->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;
    }
    Last edited by Thermal; 12-27-2016 at 03:30 AM.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Make a choice C++ or C; if C, use a C Compiler instead of a C++ Compiler!

    Edit1:
    Error 2 error C4700: uninitialized local variable 'prev' used
    Why do you NOT init it to NULL?
    Note, this will likely change it to a run-time error instead.

    Tim S.
    Last edited by stahta01; 12-27-2016 at 05:30 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    If that code is from a text book - chuck it out and get a modern C language text book - preferably one that teaches C99/11. Any author thinking the C++ headers belong in a C program does not deserve to be writing books. Also

    Code:
    system("pause");
    Is a OS System command and should not be used to keep the console open. Use

    Code:
    getchar();
    instead. Do not use getch() for this reason, as the conio,h header is outdated and not standard.
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Tags for this Thread