Cant C whats wrong


Results 1 to 4 of 4

Thread: Cant C whats wrong

  1. #1
    Join Date
    Nov 2005
    Posts
    4

    Cant C whats wrong

    Why does this crash? It gets up to asking for the name in the getstruct func, then just crashes, compiles fine but crashes afterwards. Also is there a better way to get an integer instead of having to take 48 away from what I get as a character to get its numeric value? (Without using scanf).

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct tnode {
    	char *name;
    	int age;
    	char *postcode;
    } person;
    
    void getstruct(void);
    
    int main()
    {
    	int i, j;
    	i = 0;
    	printf("Hello, please number of people: ");
    	i = getchar();
    	i = i - 48;
    	for(; j<i; j++)
    	{
    		getstruct();
    	}
    
    	return 0;
    }
    
    void getstruct(void)
    {
    	FILE *fp;
    	person rec;
    	int i;
    	i = 0;
    	printf("\nEnter name: ");
    	while((rec.name[i++] = getchar()) != '\n'){}
    	rec.name[--i] = '\0';
    	printf("\nEnter age: ");
    	while((rec.age = (int)getchar()) != '\n'){}
    	i = 0;
    	printf("\nEnter postcode: ");
    	while((rec.postcode[i++] = getchar()) != '\n'){}
    	rec.name[--i] = '\0';
    	printf("\nThank you.");
    	fp = fopen("data.txt", "w");
    	fprintf(fp, "%s %d %s\n", rec.name, rec.age, rec.postcode);
    	fclose(fp);
    	printf("\nInformation has been written to file!");
    }
    Any easier ways to do this? I tried using pointers and malloc and stuff but that got too complicated and I gave up and made the getstruct function take and return void.

  2. #2
    Join Date
    Aug 2002
    Location
    Delaware
    Posts
    4,310
    i = getchar();

    the input buffer still has data left in it.
    irc.freenode.net #justlinux The Not So Official JL IRC Channel.
    Debian Apt-Get

  3. #3
    Join Date
    Nov 2005
    Posts
    4
    Ok, Ive cleared the buffer using a function called, suprisingly, clearbuf but it still doesnt work. Same thing happens but at a different point.

    My code is now.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct tnode {
    	char *name;
    	int age;
    	char *postcode;
    } person;
    
    void getstruct(void);
    void clearbuf(void);
    
    int main()
    {
    	int i, j;
    	i = 0;
    	printf("Hello, please number of people: ");
    	i = getchar();
    	i = i - 48;
    	printf("%d", i);
    	for(; j<i; j++)
    	{
    		getstruct();
    	}
    
    	return 0;
    }
    
    void getstruct(void)
    {
    	FILE *fp;
    	person rec;
    	int i;
    	i = 0;
    	clearbuf();
    	printf("\nEnter name: ");
    	while((rec.name[i] = getchar() != '\n') && rec.name[i++] != EOF);
    	rec.name[--i] = '\0';
    	clearbuf();
    	printf("\nEnter age: ");
    	while((rec.age = (int)getchar()) != '\n' && rec.age != EOF);
    	i = 0;
    	printf("\nEnter postcode: ");
    	while((rec.postcode[i++] = getchar()) != '\n'){}
    	rec.name[--i] = '\0';
    	printf("\nThank you.");
    	fp = fopen("data.txt", "w");
    	fprintf(fp, "%s %d %s\n", rec.name, rec.age, rec.postcode);
    	fclose(fp);
    	printf("\nInformation has been written to file!");
    }
    
    void clearbuf(void)
    {
    	int i;
    	while ((i = getchar()) != '\n' && i != EOF);
    }
    According to debugger the problem lies in this line:
    Code:
    while((rec.name[i] = getchar() != '\n') && rec.name[i++] != EOF);
    But why?

  4. #4
    Join Date
    Apr 2001
    Location
    Norway
    Posts
    344
    rec.name is a pointer to a character (or character array), but you did not allocate any memory for this array so it crashes.

    If you tried changing the definition of name to, for example, char name[20] it would take any name up to 19 characters. Better is to use a temporary character buffer, and then allocate the size the user input and copy to name.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •