Thread: Help with completing this code

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    1

    Help with completing this code

    Hi, I'm new to programming.. started a basic evening course and currently working on strutures.

    The examples the course gave us are incomplete so I'm not sure why the following code isn't working..

    so wondering if someone can check this out.

    I think basically I've written the string of arrays (I think that is the correct term) wrong in the address function. When I run it in Visual Studios Express 2008 I get an exception error message pop up rather than a failed message in debug. the code is as follows.. thanks for reading

    Code:
    void get_address ( struct FRIEND add[], int num )
    {
    	int line = 0;
    
    	for ( line = 0; line < num; line++ )
    	{
    		printf ( "Enter your friends Address\n:" );
    
    		printf ( "Enter line %d of address:", line + 1 );
    		gets ( &add[num].person.address );
    	}
    }
    Full code
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    
    struct CONTACT
    {
    	char name[30];
    	char address[4][30];
    	char postcode [8]; 
    };
    
    
    struct DOB
    {
    	int day;
    	int month;
    	int year;
    };
    
    struct FRIEND
    {
    	struct CONTACT person;
    	struct DOB date_birth;
    };
    
    void get_mate ( struct FRIEND *mate);
    void get_address ( struct FRIEND add[], int num );
    void get_birth ( struct FRIEND *mate );
    void main()
    {
    	struct FRIEND mate;
    	struct FRIEND add[4];
    
    	get_mate ( &mate );
    	get_address ( add, 4 );
    	get_birth ( &mate );
    
    
    }
    void get_mate ( struct FRIEND *mate)
    {
    	printf ( "Enter your friends details:" );
    
    	printf ( "\nEnter Name: " );
    	gets ( &mate->person.name );
    }
    void get_address ( struct FRIEND add[], int num )
    {
    	int line = 0;
    
    	for ( line = 0; line < num; line++ )
    	{
    		printf ( "Enter your friends Address\n:" );
    
    		printf ( "Enter line %d of address:", line + 1 );
    		gets ( &add[num].person.address );
    	}
    }
    void get_birth ( struct FRIEND *mate )
    {
    	printf ( "\nEnter Date of Birth:" );
    	
    	printf ( "Year: " );
    	scanf ( "%d", &mate->date_birth.year );
    	fflush ( stdin );
    
    	printf ( "Month (1-12): " );
    	scanf ( "%d", &mate->date_birth.month );
    	fflush ( stdin );
    
    	printf ( "Day: " );
    	scanf ( "%d", &mate->date_birth.day );
    	fflush ( stdin );
    }
    Last edited by Eskimoe; 06-10-2010 at 11:49 AM. Reason: further expressing the issue

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    A few things:

    1) NEVER use fflush(stdin). If your instructor taught you that he is an idiot.

    2) NEVER use gets(). If your instructor taught you that, again, he is an idiot.

    Here is why fflush(stdin) is bad: Cprogramming.com FAQ > Why fflush(stdin) is wrong

    Here is why gets() is bad: Cprogramming.com FAQ > Why gets() is bad / Buffer Overflows

    3) main should be of type int main(void) and should have "return 0;" at the end of it. Again, should I mention the instructor?

    Here is why: Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])

    4) Now for the actual loop. Inside the loop you are iterating around "line" but you are never using that as an index when you are reading the respective friend. Instead you are using the loop's limit "num" as an index. Replace:
    add[num].person.address with add[line].person.address
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    I ran this in my own compiler and you need to let the person enter the friends details.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM