Overclock.net banner

1 - 18 of 18 Posts

·
Registered
Joined
·
13 Posts
Discussion Starter #1
I have this program that I have written in C that when I compile and run the program it run thru asking for the data to be entered then when I am done entering the information in it displays on the screen. When I hit any key to exit I get an error.

Can some one help me on figuring out what is causing my program not to exit correctly.

Code:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct guest {
       int age;
       char lastname[30];
};

struct guest* guestlist_allocate(int number);
void guestlist_deallocate(struct guest* p, int number);
void guestlist_readall(struct guest* p, int number);
void guestlist_display(struct guest* p, int number);

int main()
{
    int n = 3;
    struct guest* guestlist;

    printf("How many guests do you need to enter? ");
    scanf("%d", &n);

    guestlist_allocate;

    while(n <= 0)
    {
         printf("Error you entered a negative number!");
         break;
    }

    printf("Reading data for %d guests\
", n);
    guestlist_readall(guestlist, n);

    printf("Displaying data for %d guests\
", n);
    guestlist_display(guestlist, n);

    guestlist_deallocate;

    system("PAUSE");
    return 0;
}

struct guest* guestlist_allocate(int number)
{
       char lastname;
       int age = 0;
       int n;
       char buffer[30];

       struct guest *guestlist;
       guestlist = malloc (sizeof(struct guest) * n); 
       strcpy(lastname,buffer);

}

void guestlist_deallocate(struct guest* p, int number)
{
     int i;
     int n;
     struct guest *guestlist;

     for (i = 0; i < n; i++)
         free(p[i].lastname);
     free(guestlist);
}

void guestlist_readall(struct guest* p, int number)
{
     char lastname;
     int age=0;
     int i;

     for( i = 0; i < number; i++)
     {
          printf("\Guest #%d:\
 ", i);
          printf("\\Enter lastname: ");
          scanf("%29s", p[i].lastname);
          printf("\\Enter age: ");
          scanf("%ld", &p[i].age);
     }
}

void guestlist_display(struct guest* p, int number)
{
     int i;
     for(i = 0; i < number; i++)
           printf("\Guest #%d: lastname = %s\age = %ld\
", i, p[i].lastname, p[i].age);
}
 

·
Premium Member
Joined
·
13,477 Posts
You need to actually call guestlist_allocate() with the number of records you want.
 

·
Registered
Joined
·
13 Posts
Discussion Starter #3
Now when I do as you stated above and call the function as soon as I enter how many guest the program errors.
 

·
Premium Member
Joined
·
13,477 Posts
Well, what you need to do is read the errors.
 

·
Registered
Joined
·
821 Posts
This should help you a little:

Code:
Code:
struct guest* guestlist_allocate(int number)
{
       char lastname;
       int age = 0;
       [B]//int n;[/B]
       char buffer[30];

       struct guest *guestlist;
       guestlist = (guest*)malloc (sizeof(struct guest) * [B]number[/B]); 
[B]       // Heh???
       //strcpy(lastname,buffer);[/B]
       return guestlist;
}
 

·
Registered
Joined
·
13 Posts
Discussion Starter #6
The reason I had the strcpy in the code is because I have to set the lastname field of each element to an empty string.

I made the suggested changes but still have the same result.

If I had errors to read I wouldn't be having as many issues. I do not get errors upon compilation the problem comes in when I am exiting out of the program after entering the information.

Below is what my code looks like now.

Code:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct guest {
       int age;
       char lastname[30];
};

struct guest* guestlist_allocate(int number);
void guestlist_deallocate(struct guest* p, int number);
void guestlist_readall(struct guest* p, int number);
void guestlist_display(struct guest* p, int number);

int main()
{
    int n = 0;
    struct guest* guestlist;

    printf("How many guests do you need to enter? ");
    scanf("%d", &n);

    guestlist_allocate(n);

    while(n <= 0)
    {
         printf("Error you entered a negative number!");
         break;
    }

    printf("Reading data for %d guests\
", n);
    guestlist_readall(guestlist, n);

    printf("Displaying data for %d guests\
", n);
    guestlist_display(guestlist, n);

    guestlist_deallocate(guestlist, n);

    system("PAUSE");
    return 0;
}

struct guest* guestlist_allocate(int number)
{
       char lastname;
       int age = 0;
       char buffer[30];

       struct guest *guestlist;
       guestlist = malloc (sizeof(struct guest) * number); 
       return guestlist;
}

void guestlist_deallocate(struct guest* p, int number)
{
     int i;
     int n;
     struct guest *guestlist;

     for (i = 0; i < n; i++)
         free(p[i].lastname);
     free(guestlist);
}

void guestlist_readall(struct guest* p, int number)
{
     char lastname;
     int age=0;
     int i;

     for( i = 0; i < number; i++)
     {
          printf("\Guest #%d:\
 ", i);
          printf("\\Enter lastname: ");
          scanf("%29s", p[i].lastname);
          printf("\\Enter age: ");
          scanf("%ld", &p[i].age);
     }
}

void guestlist_display(struct guest* p, int number)
{
     int i;
     for(i = 0; i < number; i++)
           printf("\Guest #%d: lastname = %s\age = %ld\
", i, p[i].lastname, p[i].age);
}
 

·
Registered
Joined
·
821 Posts
You never set guestlist to the value returned from guestlist_allocate(). And guestlist_deallocate()'s loop is testing 'n' rather than 'number'. And either remove the call to free the lastname or make lastname a pointer and allocate its memory.
 

·
Premium Member
Joined
·
13,477 Posts
And remember to run the program in the debugger.
 

·
Registered
Joined
·
13 Posts
Discussion Starter #9
Okay I made some changes as was suggested and then continued on with making changes and I have narrowed my issues down to the following section of my code. When I run the program through the debugger I get an error "an access violation (segmentation fault) raised in your program". I need to use something similar to what I have done but apparently I have something not right in the code. Any help would be apprecaited.

Code:
Code:
void guestlist_readall(struct guest* p, int number)
{
    char lastname;
    int age=0;
    int i;
    char buffer[30];
    int name_length;

    for( i = 0; i < number; i++)
    {
          printf("\Guest #%d:\
 ", i);
          printf("\\Enter lastname: ");
          scanf("%29s", buffer);
          name_length = strlen(buffer);
          guest* p[i].lastname = malloc(sizeof(char)* (name_length+1));
          strcpy(guest* p[i].lastname, buffer);

          printf("\\Enter age: ");
          scanf("%ld", &p[i].age);
    }
}
 

·
Registered
Joined
·
13 Posts
Discussion Starter #10
Below is my most current version of the program I am working on. Now when I run this through the debugger it is not giving me anything but after the program runs through it errors. Almost like I am back at square one again. I have written this code from scratch so any help getting the problems fixed would be a great help.

Code:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct guest {
       int age;
       char lastname[30];
};

struct guest* guestlist_allocate(int number);
void guestlist_deallocate(struct guest* p, int number);
void guestlist_readall(struct guest* p, int number);
void guestlist_display(struct guest* p, int number);

int main()
{
    int n = 0;
    struct guest* guestlist;
    int number;
    struct guest* p;

    printf("How many guests do you need to enter? ");
    scanf("%d", &n);

    guestlist_allocate(guestlist);

    while(n <= 0)
    {
         printf("Error you entered an invalid number!");
         break;
    }

    printf("Reading data for %d guests\
", n);
    guestlist_readall(guestlist, n);

    printf("Displaying data for %d guests\
", n);
    guestlist_display(guestlist, n);

    guestlist_deallocate(guestlist, p);

    system("PAUSE");
    return 0;
}

struct guest* guestlist_allocate(int number)
{
    char lastname;
    int age = 0;
    char buffer[30];

    struct guest *guestlist;
    guestlist = malloc (sizeof(struct guest) * number); 
    return guestlist;
}

void guestlist_deallocate(struct guest* p, int number)
{
    int i;
    struct guest *guestlist;

    for (i = 0; i < number; i++)
         free(p[i].lastname);
         free(guestlist);
}

void guestlist_readall(struct guest* p, int number)
{
    char lastname;
    int age=0;
    int i;
    char buffer[30];
    int name_length;

    for( i = 0; i < number; i++)
    {
          printf("\Guest #%d:\
 ", i);
          printf("\\Enter lastname: ");
          scanf("%29s", buffer);
          name_length = strlen(buffer);
          strcpy(p[i].lastname, buffer);

          printf("\\Enter age: ");
          scanf("%ld", &p[i].age);
    }
}

void guestlist_display(struct guest* p, int number)
{
    int i;
    for(i = 0; i < number; i++)
           printf("\Guest #%d: lastname = %s\age = %ld\
", i, p[i].lastname, p[i].age);
}
 

·
Premium Member
Joined
·
13,477 Posts
What compiler and what OS are you using? The program should have crashed much earlier than at exit.

Compiling this with gcc shows exactly where and what the problems are:

Code:

Code:
test.c: In function 'main':
test.c:25: warning: passing argument 1 of 'guestlist_allocate' makes integer from pointer without a cast
test.c:10: note: expected 'int' but argument is of type 'struct guest *'
test.c:39: warning: passing argument 2 of 'guestlist_deallocate' makes integer from pointer without a cast
test.c:11: note: expected 'int' but argument is of type 'struct guest *'
Looks like you have *'s where they don't belong, or missing them when you need them. It's very common, and trips up even experienced C programmers.
 

·
Registered
Joined
·
821 Posts
This doesn't crash:

Code:

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct guest {
    int age;
    char lastname[30];
};

struct guest* guestlist_allocate(int number);
void guestlist_deallocate(struct guest* p, int number);
void guestlist_readall(struct guest* p, int number);
void guestlist_display(struct guest* p, int number);

int main()
{
    int n = 0;
    struct guest* guestlist;
    int number;
    struct guest* p;

    printf("How many guests do you need to enter? ");
    scanf("%d", &n);

    [B]guestlist = guestlist_allocate(n);
[/B]
    while(n <= 0)
    {
        printf("Error you entered an invalid number!");
        break;
    }

    printf("Reading data for %d guests\
", n);
    guestlist_readall(guestlist, n);

    printf("Displaying data for %d guests\
", n);
    guestlist_display(guestlist, n);

    guestlist_deallocate(guestlist, n);

    system("PAUSE");    
    return 0;
}

struct guest* guestlist_allocate(int number)
{
    char lastname;
    int age = 0;
    char buffer[30];

    struct guest *guestlist;
    guestlist = (guest *)malloc (sizeof(struct guest) * number); 
    return guestlist;
}

void guestlist_deallocate(struct guest* p, int number)
{
    int i;
    struct guest *guestlist;

    for (i = 0; i < number; i++)
[B]        //free(p[i].lastname)
        ;

    free(p);[/B]
}

void guestlist_readall(struct guest* p, int number)
{
    char lastname;
    int age=0;
    int i;
    char buffer[30];
    int name_length;

    for( i = 0; i < number; i++)
    {
        printf("\Guest #%d:\
 ", i);
        printf("\\Enter lastname: ");
        scanf("%29s", buffer);
        name_length = strlen(buffer);
        strcpy(p[i].lastname, buffer);

        printf("\\Enter age: ");
        scanf("%ld", &p[i].age);
    }
}

void guestlist_display(struct guest* p, int number)
{
    int i;
    for(i = 0; i < number; i++)
        printf("\Guest #%d: lastname = %s\age = %ld\
", i, p[i].lastname, p[i].age);
}
 

·
Registered
Joined
·
13 Posts
Discussion Starter #14
I am wondering if anyone could help me change this program for another assignment that I have. I need to take the above program and change it according to the following instructions. I need some direction on how to go about doing this. I am not wanting some one to just give me the answer.

 struct guest* guest_create( char* lastname, int age, );
o allocates a new struct guest data structure which address you will return
o Set the lastname field to a newly allocated copy of the string pointed to by
the lastname parameter
o Set the age field to the value in the age parameter
o Set the next pointer to NULL
 void guestlist_deallocate (struct guest* p);
o Deallocates all the elements of the struct guest linked list pointed to by p
 struct guest * guestlist_readall (int number);
o This function will read the name and age of number guests from the user
o For each guest, create a new struct guest data structure, assign appropriate
value to its fields (the age value and a copy of the lastname typed by the
user), and link it to the end of a linked list of all struct guest read so far.
o You will return a pointer to the first struct guest of the linked list
o Use the guest_create function to help you write this one
 void guestlist_display(struct guest* p);
o Display information about each guest (name and age) on the screen
 

·
Registered
Joined
·
13 Posts
Discussion Starter #15
Below is what I have come up with but can not get to compile. Any help on pointing me in the right direction would be a great help

Code:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct guest{
       int age;
       char lastname;
       struct node *next;
};

struct guest* guest_create(char* lastname, int age);
void guestlist_deallocate(struct guest* p);
struct guest * guestlist_readall(int number);
void guestlist_display(struct guest* p);

int main()
{
    int n = 0;
    struct guest* guestlist;
    int number;
    struct guest* p;

    printf("How many guests do you need to enter? ");
    scanf("%d", &n);

    struct guest *guest_create = NULL;

    while(n <= 0)
    {
         printf("Error you entered a negative number!");
         break;
    }

    printf("Reading data for %d guests\
", n);
    guestlist_readall(number);

    printf("Displaying data for %d guests\
", n);
    guestlist_display(p);

    guestlist_deallocate(p);

    system("PAUSE");
    return 0;
}

struct guest* guest_create(char* lastname, int age)
{
    struct guest *tmp;

    if(lastname == NULL)
    {
         lastname = (struct guest*)malloc(sizeof(struct guest));
         if(lastname == NULL)
         {
              printf("Error! memory is not available\
");
              exit(0);
         }
         lastname-> age = age;
         lastname-> next = lastname;
    }
    else
    {
        tmp = lastname;

        while (tmp-> next != lastname)
            tmp = tmp -> next;
        tmp -> next = (struct guest *)malloc(sizeof(struct guest));
        if(tmp -> next == NULL)
        {
               printf("Error! memory is not available\
");
               exit(0);
        }
        tmp = tmp->next;
        tmp -> age = age;
        tmp -> next = lastname;
    }
    return lastname;
}

void guestlist_deallocate(struct guest* p)
{
    struct guest *current, *tmp;

    current = lastname->next;
    lastname->next = NULL;
    while(current != NULL)
    {
         tmp = current -> next;
         free(current);
         current = tmp;
    }
}

struct guest * guestlist_readall(int number)
{
    struct guest {
    int age;
    struct node *next;
};

    char lastname;
    int age=0;
    int i;
    char buffer[30];
    int name_length;

    for( i = 0; i < number; i++)
    {
          printf("\Guest #%d:\
 ", i);
          printf("\\Enter lastname: ");
          scanf("%29s", buffer);
          name_length = strlen(buffer);
          strcpy(lastname, buffer);

          printf("\\Enter age: ");
          scanf("%ld", &age);
    }
}

void guestlist_display(struct guest* p)
{
    struct guest *current
    current = lastname;

    if(current != NULL)
    {
         do
         {
               printf("%d\",current->age);
               current = current->lastname;
         }while(current != lastname);
         printf("\
");
    }
    else
         printf("The list is empty\
");

}
 

·
Premium Member
Joined
·
13,477 Posts
I caught it: zero certainly isn't a negative number. But it seemed irrelevant to the problem.
 

·
Registered
Joined
·
821 Posts
Too many errors to fix for you.

What is a node?

Code:
Code:
struct guest{
       int age;
       char lastname;
       struct node *next;
};
Hmmmm, what do you think is wrong with this line?

Code:
Code:
lastname = (struct guest*)malloc(sizeof(struct guest));
I suggest you recompile your code after every change or additional line of code. Writing large chunks of code without intermediate compiles only leads to this kind of mess.
 
1 - 18 of 18 Posts
Top