trouble with functions

This is a discussion on trouble with functions within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int ask(void); int receive(int); float calculate(int, int); int main() { int ne; int na; ...

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    trouble with functions

    Code:
    #include <iostream>
    using namespace std;
    
    int ask(void);
    int receive(int);
    float calculate(int, int);
    
    int main()
    {
    	int ne;
    	int na;
    	float calc;
    
    	ne = ask();
    
    	na = receive(ne);
    
    	calc = calculate(ne, na);
    
    	ask();
    	receive();
    
    	cout << "The average number of days absent is  " << calc << endl;
    
    	return 0;
    
    
    }
    
    int ask(void)
    
    {	
    	int number_of_employees;
    
    	cout << "Enter the number of employees in the company." << endl;
    	cin >> number_of_employees;
    	
    	if(number_of_employees < 1)
    	{
    		cout << "Invalid input" << endl;
    		cin >> number_of_employees;
    	}
    
    	return 0;
    	
    
    }
    
    int receive(int number_of_employees)
    
    {
    	int sum = 0;
    	int days_absent;
    
    	cout << "Enter the number of absences for each employee" << endl;
    	
    	for (int i= 0; i < number_of_employees; i++)
    	{
    		cin >> days_absent;
    
    		if(days_absent < 0)
    		
    			cout << "Invalid input" << endl;
    		
    		else
    			sum += days_absent;
    		
    	}
    
    	return sum;
    
    }
    
    float calculate(int number_of_employees, int number_of_absences)
    
    {
    	return (float) number_of_absences/number_of_employees;
    }
    The receive function is giving me problems. I'm trying to get it to accept the number of employees as it's parameter.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Why does ask() always return 0 ?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Originally posted by Hammer
    Why does ask() always return 0 ?
    It returns 0 because...uh...

    Maybe I should quit programming and become a farmer...


  4. #4
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    Originally posted by volk

    Maybe I should quit programming and become a farmer...
    I'm very new to c++ as well...this is my first semester, and my advice is not to give up. Just put some time into it, just like anyother class. But if this is something you want to do for life, you should, preferably, put more time into it.

    Try making usefull programs that you could use in your math courses, or something else like simple text games. There are MANY posibilities even with the amount of knowledge you have so far. Later it just get easier and the code becomes shorter...
    couple weeks into my C++ class I made Nine Men's Morris game, without using arrays, everything worked but it was close to 1500 lines! later When I learned about arrays, I shortened it to about 700 lines!!!!

    But it also could be that programming is not for you, and it is better to know that sooner than later,

    cheers,

    axon

    P.S.
    If you do decide to stick with it I recommend this book: "Accelerated C++" by Moo and Koenig

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    return number_of_employes; in your ask function

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    ask fxn should return number_of_employees as previously mentioned.

    Code:
        ask();
    receive();
    why do this in main? it is a fxn that returns a value, but it is not being stored into anything. the answer is being thrown away. also, what is the purpose of it being there? it is unnecessary.

  7. #7
    Registered User sikamikaniko's Avatar
    Join Date
    Mar 2003
    Posts
    28

    hm...

    ask() should return 0 loke this:
    <code>

    if(number_of<1)
    {cout<<"Invalid input"; return 0;}
    return num_of;
    </code>

    Don't go farmering, pick something easier: beer/coffee and TV like me

  8. #8
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    I tried fixing my code by doing this, but it's requesting the same input even after it was received.

    Code:
    #include <iostream>
    using namespace std;
    
    int ask(void);
    int receive(int);
    float calculate(int, int);
    
    int main()
    {
    	int ne;
    	int na;
    	float calc;
    
    	ne = ask();
    
    	na = receive(ne);
    
    	calc = calculate(ne, na);
    
    	ask();
    	receive(ne);
    
    	cout << "The average number of days absent is  " << calc << endl;
    
    	return 0;
    
    
    }
    
    int ask(void)
    
    {	
    	int number_of_employees;
    
    	cout << "Enter the number of employees in the company." << endl;
    	cin >> number_of_employees;
    	
    	if(number_of_employees < 1)
    	{
    		cout << "Invalid input" << endl;
    		cin >> number_of_employees;
    	}
    
    	return number_of_employees;
    	
    
    }
    
    int receive(int number_of_employees)
    
    {
    	int sum = 0;
    	int days_absent;
    
    	cout << "Enter the number of absences for each employee" << endl;
    	
    	for (int i= 0; i < number_of_employees; i++)
    	{
    		cin >> days_absent;
    
    		if(days_absent < 0)
    		
    			cout << "Invalid input" << endl;
    		
    		else
    			sum += days_absent;
    		
    	}
    
    	return sum;
    
    }
    
    float calculate(int number_of_employees, int number_of_absences)
    
    {
    	return (float) number_of_absences/number_of_employees;
    }

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    here, try this:

    Code:
    #include <iostream>
    using namespace std;
    
    int ask(void);
    int receive(int);
    float calculate(int, int);
    
    int main()
    {
    	int ne;
    	int na;
    	float calc;
    	
    	ne = ask();
    	na = receive(ne);
    	calc = calculate(ne, na);
    	
    	//do not need to call functions again here.
    	
    	cout << "The average number of days absent is  " << calc << endl;
    	
    	return 0;
    }
    int ask(void)
    {
    	int number_of_employees;
    	
    	cout << "Enter the number of employees in the company." << endl;
    	cin >> number_of_employees;
    	
    	if(number_of_employees < 1)
    	{
    		cout << "Invalid input" << endl;
    		cin >> number_of_employees;
    	}
    	
    	return number_of_employees;
    }
    int receive(int number_of_employees)
    {
    	int sum = 0;
    	int days_absent;
    	
    	cout << "Enter the number of absences for each employee" << endl;
    	
    	for (int i= 0; i < number_of_employees; i++)
    	{
    		cin >> days_absent;
    	
    		if(days_absent < 0)
    			cout << "Invalid input" << endl;
    		else
    			sum += days_absent;
    		}
    	
    	return sum;
    }
    float calculate(int number_of_employees, int number_of_absences)
    {
    	return (float) number_of_absences/number_of_employees;
    }

  10. #10
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Thank you, alpha.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  2. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 06:05 PM
  3. Trouble passing args to functions in other files
    By Midnight Coder in forum C Programming
    Replies: 6
    Last Post: 01-03-2009, 04:13 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM

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