test code

This is a discussion on test code within the C Programming forums, part of the General Programming Boards category; i have this create_new function that seems to be right to me but i dont know how to test it. ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    34

    test code

    i have this create_new function that seems to be right to me but i dont know how to test it. How would i call the function?

    Code:
    personal create_new()
    {
    	personal temp;
      
      printf("\nFist Name: ");
      fgets(temp.F_name, sizeof temp.F_name, stdin);
      
      printf("\nLast Name: ");
      fgets(temp.L_name, sizeof temp.L_name, stdin);
     
      printf("\nSalary: ");
      scanf("%d", &temp.salary);
    
      printf("\nMonth of birth: ");
      scanf("%d", &temp.birth.Birth_month);
    
      printf("\nDay of Birth: ");
      scanf("%d", &temp.birth.Birth_day);
      
      printf("\nYear of Birth: ");
      scanf("%d", &temp.birth.Birth_year);
    
      printf("\nAddress information...");
      printf("\nStreet name: ");
      fgets(temp.stuff.street, sizeof temp.stuff.street, stdin);
    
      printf("\nCity: ");
      fgets(temp.stuff.city, sizeof temp.stuff.city, stdin);
    
      printf("\nState: ");
      fgets(temp.stuff.state, sizeof temp.stuff.state, stdin);
    
      printf("\nZip Code: ");
      fgets(temp.stuff.zip, sizeof temp.stuff.zip, stdin);
      
      return (temp);
    
    }

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Why don't you ask the person you got it from?

    That function is not defined properly either. It is missing a type, and a parameter list (or if no parameters, then void)

    ~/

  3. #3
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I hope this works for ya:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /// STRUCTURES
    struct Sbirth {
    	double Birth_year;
    	double Birth_month;
    	double Birth_day;
    };
    
    struct Sstuff {
    	char street[50];
    	char city[50];
    	char state[15];
    	char zip[10];
    };
    
    struct personal {
    	char F_name[50];
    	char L_name[50];
    	double salary;
    	struct Sbirth birth;
    	struct Sstuff stuff;
    };
    
    /// PROTOTYPE FUNCTIONS
    struct personal create_new();
    
    /// MAIN
    int main() {
    	struct personal p = create_new();
    	printf("Hello %s", p.F_name);
    	return 0;
    }
    
    /// CREATE NEW PERSON
    struct personal create_new() {
    	struct personal temp;
    	
    	printf("\nFist Name: ");
    	fgets(temp.F_name, sizeof temp.F_name, stdin);
    	
    	printf("\nLast Name: ");
    	fgets(temp.L_name, sizeof temp.L_name, stdin);
    	
    	printf("\nSalary: ");
    	scanf("%d", &temp.salary);
    	
    	printf("\nMonth of birth: ");
    	scanf("%d", &temp.birth.Birth_month);
    	
    	printf("\nDay of Birth: ");
    	scanf("%d", &temp.birth.Birth_day);
    	
    	printf("\nYear of Birth: ");
    	scanf("%d", &temp.birth.Birth_year);
    	
    	printf("\nAddress information...");
    	printf("\nStreet name: ");
    	fgets(temp.stuff.street, sizeof temp.stuff.street, stdin);
    	
    	printf("\nCity: ");
    	fgets(temp.stuff.city, sizeof temp.stuff.city, stdin);
    	
    	printf("\nState: ");
    	fgets(temp.stuff.state, sizeof temp.stuff.state, stdin);
    	
    	printf("\nZip Code: ");
    	fgets(temp.stuff.zip, sizeof temp.stuff.zip, stdin);
    	
    	return (temp);
    }

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Kleid-0, you seem to be doing this a lot. Please do not post a whole bunch of source code that doesn't even answer the OP's question. No one learns anything when you just give them source code.

    Once you've got a working function, you can call it in the following way:
    Code:
    variable1 = function1(parameter1, parameter2);
    Everything is optional except for the function name and parentheses. If your function returns something, you can assign that variable to something by using the function on the right side of an assignment statement. To pass parameters into your function, you just place the code parameters, in order, delimited by commas, in between the parentheses.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. please test my code!!!
    By MK27 in forum Linux Programming
    Replies: 3
    Last Post: 07-09-2008, 11:41 PM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  3. Code to test if a tree is balanced
    By rahuls in forum C Programming
    Replies: 1
    Last Post: 03-20-2003, 02:41 PM
  4. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM

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