problem returning variables

This is a discussion on problem returning variables within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int get_employees(int); main() { /*Variables*/ int employees; get_employees(employees); /*call function*/ printf("%i", employees); return 0; } int get_employees(int ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    15

    problem returning variables

    Code:
    #include <stdio.h>
    
    int get_employees(int);
    
    main()
    {
    /*Variables*/
    int employees;
    
    get_employees(employees);	/*call function*/
    printf("%i", employees);
    
    return 0;
    }
    
    int get_employees(int employees)	/* the function*/
    {	
    	int x=0;
    	for (x = 1; x <=1; ++x)
    	{
    			printf("Please enter # of employees(1-10): ");
    			scanf("%i",&employees); 
    			if (employees > 10)
    				{	printf ("Invalid Entry\n");
    					--x;
    				}
    			if (employees < 1)
    				{	printf ("Invalid Entry\n");
    					--x;
    				}
    		
    	}
    	return employees;
    }
    Why does this print a random number instead of the one scanned in by the user???

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    Because you never assign the return of get_employees to anything?
    Variables have garbage data by default until initialized or assigned.
    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.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    15
    How would I assign that??? Sorry for my incompetence Im new at this.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    Code:
    employees = get_employees(employees);
    I'd say you need to further look up how variables work, though. You are passing a variable to a function that does not need it.
    And then that function returns the data.

    Oh yes.
    There are two other problems.
    First one.
    Second one.
    Last edited by Elysia; 02-22-2009 at 01:04 PM.
    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.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    15
    Thanks

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you do not need to pass a parameter to getEmployee function
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem returning array from function(among others)
    By Calef13 in forum C++ Programming
    Replies: 30
    Last Post: 10-30-2006, 03:26 PM
  2. problem with extern variables
    By crash88 in forum C Programming
    Replies: 11
    Last Post: 05-05-2006, 01:44 PM
  3. Problem with global variables
    By DominicTrix in forum C++ Programming
    Replies: 6
    Last Post: 09-08-2004, 01:26 PM
  4. small reference problem
    By DavidP in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2004, 07:29 PM
  5. Replies: 4
    Last Post: 10-17-2002, 10:09 PM

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