Thread: problem returning variables

  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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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 02: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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you do not need to pass a parameter to getEmployee function
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

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, 04: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