difficulty creating a function

This is a discussion on difficulty creating a function within the C Programming forums, part of the General Programming Boards category; My first time trying to create a function. Having no luck. Can anyone help. My code is just simple I ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    4

    Unhappy difficulty creating a function

    My first time trying to create a function. Having no luck. Can anyone help. My code is just simple I have to allow a function to be used for the data entry of the member data. here is what it is like so far.
    include "stdafx.h"

    Code:
    // Project 3.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    /* -----------------------------------------------------------------------------------
    * Name: sample.c
    * Function:
    * Developed By: 
    * Date:
    * History: Modified By: Reason: Date:
    --------------------------------------------------------------------------------------*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    // ------------------- New types declared - Global types.-------------------
    struct addr
    {
      char first_name[30];
      char surname[40];
      char address1[40];
      char county[50];
      char country[30];
     int  sex;   // 1 for female, 0 for male
     int  active_member;
     int  member_num;
     };
    
    // ----------------- Function prototypes ----------------------
    int main(void);
    
    // ------------------------------------------------------------
    int main(void)
    {
     struct addr member;   // Declare variable to use.
     int getmemberID();
     int i;
     i = 1;  // First member.
     
     for(i=1; i>=1; i++)  //For loop assigning unique id to each member 
     {
    	getmemberID(int i) //Function call
     }
     printf("\n\n");
     return 0;
    }
    getmemberID(void) //Function to print out member information
    {
    	
    	
    	int i;
    	printf("Enter first name for member %d: ", i);
    	scanf("%s", member.first_name);
    	printf("Enter surname for member %d: ", i);
    	scanf("%s", member.surname);
    	printf("Enter the first line of address for member %d: ", i);
    	scanf("%s", member.address1);
      
    	printf("Enter county for member %d: ", i);
    	scanf("%s", member.county);
       
    	printf("Enter country for member %d: ", i);
    	scanf("%s", member.country);
       
    	printf("Is member %d active (1=Y/0=N): ", i);
    	scanf("%d", &member.active_member);
      
    	printf("Is member %d male or female (1=Y/0=N): ", i);
    	scanf("%d", &member.sex);
    	member.member_num = i;
    	
    	printf("\n\n");
    	
    }

  2. #2
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Assuming that this is C rather than C++, there are two things you need to consider. If you want a function, you must place it directly above the function calling it, unless you use prototypes. You are also missing the data type that the function is to return. Here's a simple example to add x+2:

    Code:
    int AddTwo(int x); // function prototype
    
    int Example; // a variable
    
    int AddTwo(int x) // function will return an int with x as a parameter
    {
    	return(x+2); // return the value
    }
    
    int main()
    {
    	 // call the function with 4 as the parameter
    	Example = AddTwo(4); // Example should be 6
    }
    
    /* the function could be placed here, but you'll need a prototype first
    int AddTwo(int x)
    {
    	return(x+2);
    }
    */
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    It's always good practice to use function prototypes and it's very bad practice to not specify return type.
    And main does not need a function prototype, just so you know. You shouldn't call main anyway.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    I don't see any indication that you know about pointers yet. Therefore, I am going to try to
    correct on the fly your code. Watch my comments and corrections.

    Code:
    /* C files should end with .c extension */
    // Project 3.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    /* -----------------------------------------------------------------------------------
    * Name: sample.c
    * Function:
    * Developed By: 
    * Date:
    * History: Modified By: Reason: Date:
    --------------------------------------------------------------------------------------*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    /* set MAX to as many as you need */
    #define MAX 2
    
    // ------------------- New types declared - Global types.-------------------
    struct addr
    {
      char first_name[30];
      char surname[40];
      char address1[40];
      char county[50];
      char country[30];
     int  sex;   // 1 for female, 0 for male
     int  active_member;
     int  member_num;
     };
    
    /* there's not need to prototype the main function */
    // ----------------- Function prototypes ----------------------
    int main(void);
    struct addr getmemberID( struct addr patron ); /* function prototype */
    
    // ------------------------------------------------------------
    int main(void)
    {
     struct addr member[MAX];
     int i = 0; 
    
    for ( i = 0; i < MAX; i++ )
    {
         member[i] = getmemberID( member[i] );
         
         printf( "Data of member &#37;d\n", member[i].member_num + 1 );
         printf( "firstname: %s\n", member[i].first_name );
         printf( "surname: %s\n", member[i].surname );
         printf( "address: %s\n", member[i].address1 );
         printf( "county: %s\n", member[i].county );
         printf( "country: %s\n', member[i].country );
         if( member[i].sex )
             printf( "sex: female\n" );
         else
            printf( "sex: male\n" );
         
         if( member[i].active )
             printf( "active member" );
         else
            printf( "inactive member" );
    }
    
    return 0;
    }
    
    
    
    
    /* let's work in that function */
    struct addr getmemberID( struct addr patron ) //Function to print out member information
    {
    		
            /* int i;  eliminating this */
    	printf("Enter first name: " );
            fflush( stdout );
    	scanf("%29[^\n]s", patron.first_name);
    	printf("Enter surname: ");
            fflush( stdout );
    	scanf("%39[^\n]s", patron.surname);
    	printf("Enter the first line of address for member: ");
            fflush( stdout );
    	scanf("%39[^\n]s", patron.address1);
      
    	printf("Enter county for member: ");
            fflush( stdout );
    	scanf("%49[^\n]s", patron.county);
       
    	printf("Enter country for member: ");
    	scanf("%29[^\n]s", patron.country);
       
    	printf("active member?: (1=Y/0=N)" );
            fflush( stdout );
    	scanf("%d", &patron.active_member);
      
    	printf("male or female?: (1=F/0=M): ");
            fflush( stdout );
    	scanf("%d", &patron.sex);
    	patron.member_num = i;
    	
    	return patron;
    }
    There's not data validation and the problem of doing it without pointers is that the structures are been copied back and forth.
    It is possible that I misspelled some or I did not notice something. It is hard to see when you use colors in this editor.
    Last edited by Aia; 02-24-2008 at 01:01 PM. Reason: edit width in the scanf to accommodate for the terminator and handle spaces
    When the eagles are silent, the parrots begin to jabber. ~Winston Churchill

  5. #5
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Also, for the male/female notations, you might want to use defines to make it easier to work with. I do this in my game with things like left and right, and normal, frame advance, paused, debug normal, and debug frame advance game modes. It becomes apparent when you compare something like "direction = 0;" to "direction = left;". It's much easier to understand with this. The same applies with gender as well. It's easier to understand "gender = male;" than it is "gender = 1;", wouldn't you think?
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    4

    Talking Thanks for helping me with my functions

    Thanks so much for all your help (time & effort). It is really appreciated! You have fixed all my problems.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > you might want to use defines to make it easier to work with.
    I'd recommend against defines, go with enum's instead.
    Code:
    enum sex {
        SEX_FEMALE=0,
        SEX_MALE=1
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21