Thread: Help with program.

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    17

    Help with program.

    So I was learning about linked list and I created a method to delete the list, I am getting run-time error(unusual flow of control) with the goto statement in main function which I'm using as a looped confirmation prompt, here:

    Code:
    #include <stdio.h>#include <stdlib.h>
    struct Node{
        int data;
        struct Node *next;
    }*head;
    struct Node* push(struct Node *current_head, int new_data)
    {
        struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
        new_node->data = new_data;
        new_node->next = current_head;
        return new_node;
    }
    void displayList(struct Node* current_head)
    {
      while(current_head != NULL){
          printf("%d\n", current_head->data);
          current_head = current_head->next;
      }
    }
    struct Node* deleteList(struct Node* current_head){
        struct Node *next;
        while(current_head != NULL){
            next = current_head->next;
            free(current_head);
            current_head = next;
        }
        printf("The list is deleted.");
        return current_head;
    }
    int main(){
        int num;
        puts("Enter the elements:(-1 to end)");
        while(num != -1){
            scanf("%d", &num);
            if(num != -1)
                head = push(head, num);
            else
                break;
        }
        puts("Displaying the List:");
        displayList(head);
        puts("Do you want to empty list before exiting?");
        askAgain: {
            puts("Y or N?");
            char choice;
            scanf("%c", choice);
            if(choice == 'Y' || choice == 'N' || choice == 'n' || choice == 'y')
            {
              if(choice == 'Y' || choice == 'y'){
                  head = deleteList(head);
                }
              }
              else
              {
                printf("Invalid Input. Try again:");
                goto askAgain;
              }
            }
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Change the use of goto to a loop control structure. You should not be using goto for this.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 03-15-2016, 06:35 AM
  2. Replies: 2
    Last Post: 09-09-2014, 02:36 PM
  3. Replies: 2
    Last Post: 12-11-2012, 12:25 AM
  4. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  5. Replies: 5
    Last Post: 08-16-2007, 11:43 PM

Tags for this Thread