14 Replies - 3430 Views - Last Post: 15 July 2012 - 08:01 PM Rate Topic: -----

#1 trendygirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 05-July 12

C++: Using Functions & Pass-by-reference

Posted 14 July 2012 - 07:29 PM

Hey guys,
I'm back looking for help on an assignment, which I have attached, so you can read the specifics(you will need to in order to understand what the Prof. wants). I do think I did pretty good(didn't have much practice, did use info here & elsewhere for assistance) in setting up my code, but it is not working.
The final outcome is to have the Average Number of Days a Companys Employees are absent.
Thanks for your AWESOMENESS!!

This program calculates the average number of days a company's employees are absent. 
Also, keeps track of total number of days missed.  Then, returns an average number of days absent.
*/

#include<iostream>
#include<iomanip>

using namespace std;

//function prototypes*****************************************************
//return type: void
//parameters:  int - multiples (reference)
//Purpose: This function asks the user for the number of employees in the company 
//and value is passed by-reference to the function.

	void GetNumEmployees (int&);

//----------------------------------------------
//return type: int
//parameters:  int
//Purpose: This function accepts one argument; the number of employees in the company and then 
//asks the user to enter the number of days each employee missed during the past year.  While 
//keping track of the total number of days missed(returning an int).

	int TotalDaysMissed (int);

//-----------------------------------------------
//return type: float
//parameters:  2 ints
//Purpose: This function takes two arguments; the number of employees in the company and the total 
//number of days absent for all employees in the past year (365 days).  While tracking 
//the average number of days absent(returning a float).

	float AverageDaysMissed(int, int);

//-----------------------------------------------
//**************************************************************
int main ()
{
	//Declare and Initialize Variables

	int numemps = 0;
	int numdays = 0;
	double daysmissed = 0.0;
	double total = 0.0;
	float AverageDaysMissed = 0.0;

		//Display - how many employees in company?
		cout<<"How many employees are in the company?\n\n";
		cout<<"\nThere are "<<numemps<<" in the company.\n\n";

		//Display - how many absent days work were there?
		cout<<"\nHow many absent days of work were there?\n\n";
		count<<"\nThere were "<<TotalDaysMissed<<" for a 12 month period.\n\n";

		//Display - What is the average of absent work days per number of employees?
		cout<<"\nWhat is the average of absent work days per number of employees?\n\n";
		count<<"\nThere were "<<AverageDaysMissed<<" for "<<numemps<<" for a 12 month period.\n\n";

	return 0;
}
//function declarations**************************************************************

	void GetNumEmployees (int &numemps)
	{
		do
		{//prompt for the number of employees
		cout<<"\nPlease enter the number of employee's at the company:  \n";
		cin>>numemps;
		
		//Input Validation
		if (numemps < 1)
		cout<<"\nInvalid.  Please enter a number higher than one: \n";
		}
		while (numemps < 1);

		//FOR loop
		for (int i = 1; i <= numemps; i++)
	}
//-----------------------------------------------

	int TotalDaysMissed (int numdays);
	//daysmissed by each employee (each emp must be identified somehow and then counted and show the count)
	//prompt must cycle for as many employees as given by user.
	//# of emps:
	//for each emp, prompt to enter # of missing days
	//keep track of # of missing days
	{
		do
		{//Prompt user for the number of days each employee missed during the past year (365 days).
		cout<<"\nPlease enter the number of days that each employee missed work for the last 12 months:  \n";
		cin>>numdays;	

		//Input Validation
		if (numdays < 0)
		cout<<"\nInvalid.  Please enter a number higher than zero.\n";
		}
		while (numdays < 0);

		//FOR loop
		for (int i = 1; i <= numdays; i++)
	}
		//calculate-add to total
		//Keep track of the total number of days missed(365 days).
		total += TotalDaysMissed;	//acculumulate running total
	{
	return TotalDaysMissed;
	}
//-----------------------------------------------

	float AverageDaysMissed(int, int);

	//Display the average number of days the company's employees are absent (365 days).
	AverageDaysMissed = TotalDaysMissed / 365;
	{
	return (AverageDaysMissed, numemps);
	}



Error Messages:

days.cpp: In function ‘int main()’:
days.cpp:58: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘const char [13]’ to binary ‘operator<<’
days.cpp:62: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘const char [13]’ to binary ‘operator<<’
days.cpp: In function ‘void GetNumEmployees(int&)’:
days.cpp:83: error: expected primary-expression before ‘}’ token
days.cpp:83: error: expected `;' before ‘}’ token
days.cpp: At global scope:
days.cpp:92: error: expected unqualified-id before ‘{’ token


Is This A Good Question/Topic? 0
  • +

Replies To: C++: Using Functions & Pass-by-reference

#2 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,286
  • Joined: 28-March 11

Re: C++: Using Functions & Pass-by-reference

Posted 14 July 2012 - 07:53 PM

trendygirl, when is your birthday? I will get ya a pair of glasses, you posted in the wrong forum again. :crazy:
Was This Post Helpful? 0
  • +
  • -

#3 healix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 29-May 11

Re: C++: Using Functions & Pass-by-reference

Posted 14 July 2012 - 08:02 PM

you have semicolons at the end of your function definitions. You only use them when prototyping.

View Posthealix, on 14 July 2012 - 08:00 PM, said:

you have semicolons at the end of your function definitions. You only use them when prototyping.

you also spelled "count" on line 58
Was This Post Helpful? 1
  • +
  • -

#4 trendygirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 05-July 12

Re: C++: Using Functions & Pass-by-reference

Posted 14 July 2012 - 08:16 PM

Gunner, sorry :online2long: I go to Forum & C++ Challenges & click on "Post new topic/question." Please tell me where I should be going??
Was This Post Helpful? 0
  • +
  • -

#5 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,286
  • Joined: 28-March 11

Re: C++: Using Functions & Pass-by-reference

Posted 14 July 2012 - 08:19 PM

Here
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: C++: Using Functions & Pass-by-reference

Posted 14 July 2012 - 08:19 PM

View Posttrendygirl, on 14 July 2012 - 11:16 PM, said:

Gunner, sorry :online2long: I go to Forum & C++ Challenges & click on "Post new topic/question." Please tell me where I should be going??

Post C/C++ questions here, under C/C++. The challenges are for C/C++ challenges, not questions.
Was This Post Helpful? 0
  • +
  • -

#7 FrozenSnake  Icon User is offline

  • En man från Sverige!

Reputation: 122
  • View blog
  • Posts: 998
  • Joined: 30-July 08

Re: C++: Using Functions & Pass-by-reference

Posted 14 July 2012 - 08:32 PM

View Posttrendygirl, on 15 July 2012 - 05:16 AM, said:

Gunner, sorry :online2long: I go to Forum & C++ Challenges & click on "Post new topic/question." Please tell me where I should be going??

To simplify it the "Challenges" sub-forum is for "mini contests".
For normal daily problems you should post in "C and C++" only no sub-forum in "C and C++".

On line 55 and 59 there are typos (I think you see them if you take a look ;))
And some of your for-loops you have forgot to add ';' in some places.

You also put stuff outside the body of a function sometimes. Best tip to avoid this is indention.

You code like this now (example on where lack of indentation can be bad) I removed some of the comments to make it more compact here.
	int TotalDaysMissed (int numdays)
	{
		do
		{
		cout<<"\nPlease enter the number of days that each employee missed work for the last 12 months:  \n";
		cin>>numdays;

		//Input Validation
		if (numdays < 0)
		cout<<"\nInvalid.  Please enter a number higher than zero.\n";
		}
		while (numdays < 0);

		//FOR loop
		for (int i = 1; i <= numdays; i++);
	}
		total += TotalDaysMissed;
	{
	return TotalDaysMissed;
	}
If you made it like this
int TotalDaysMissed (int numdays)
{
    do
    {
        cout<<"\nPlease enter the number of days that each employee missed work for the last 12 months:  \n";
        cin>>numdays;

        //Input Validation
        if (numdays < 0)
            cout<<"\nInvalid.  Please enter a number higher than zero.\n";
    } while (numdays < 0);
    for (int i = 1; i <= numdays; i++);
}
total += TotalDaysMissed;	//acculumulate running total
{
return TotalDaysMissed;
}
You would see right away where your code is outside the body. I left it outside so you could see how easy it is to spot!

Also regarding variables, you can't use them in different functions like you do (not in this case anyway).
You declare 'total' in main, but never in 'int TotalDaysMissed (int numdays)'.
This is not a fix for all problems but for some! I hope it help you on your way!

This post has been edited by FrozenSnake: 14 July 2012 - 08:33 PM

Was This Post Helpful? 1
  • +
  • -

#8 trendygirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 05-July 12

Re: C++: Using Functions & Pass-by-reference

Posted 14 July 2012 - 08:36 PM

UUMM...ok, so I need to click on the "New Topic/Question" link at the bottom...it's a little confusing because the one I am clicking is in the middle of those 2 sections....sorry.... :dontgetit: (my topics are a challenge for me though, heh heh)
Was This Post Helpful? 0
  • +
  • -

#9 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,286
  • Joined: 28-March 11

Re: C++: Using Functions & Pass-by-reference

Posted 14 July 2012 - 08:41 PM

Attached Image
Was This Post Helpful? 1
  • +
  • -

#10 trendygirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 05-July 12

Re: C++: Using Functions & Pass-by-reference

Posted 14 July 2012 - 08:44 PM

:gunsmilie: thanks GUNNER & sista pencil.... :rolleyes2:
Was This Post Helpful? 0
  • +
  • -

#11 trendygirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 05-July 12

Re: C++: Using Functions & Pass-by-reference

Posted 14 July 2012 - 09:17 PM

thanks, sssssnnnaaaakkkeeee! :mellow:
Was This Post Helpful? 0
  • +
  • -

#12 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 678
  • View blog
  • Posts: 2,337
  • Joined: 31-December 10

Re: C++: Using Functions & Pass-by-reference

Posted 15 July 2012 - 11:05 AM

View Posttrendygirl, on 14 July 2012 - 11:44 PM, said:

:gunsmilie: thanks GUNNER & sista pencil.... :rolleyes2:

Not to derail the topic, but this just made me laugh. Sista pencil!
Was This Post Helpful? 0
  • +
  • -

#13 trendygirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 05-July 12

Re: C++: Using Functions & Pass-by-reference

Posted 15 July 2012 - 03:07 PM

Glad I could entertain!!
ok, so I have some modifications....did more research, still confused about this....if I have an
accumulator TOTAL
isn't that supposed to be a
double
? But between trying to follow the criteria given, either I can't do that or I have to do it a different way(that I wouldn't know of!) So, I am not sure how to code the return
AverageDaysMissed
at the end.... I removed extra comments & errors are at bottom. Thank you!! Suggestions?? :whistling: :sweatdrop:

#include<iostream>
#include<iomanip>

using namespace std;

	void GetNumEmployees (int&);
//----------------------------------------------
	int TotalDaysMissed (int);
//-----------------------------------------------
	float AverageDaysMissed(int, int);
//-----------------------------------------------
int main ()
{
	//Declare and Initialize Variables

	int numemps = 0;
	int numdays = 0;
	int TotalDaysMissed = 0;
	float AverageDaysMissed = 0.0;

		//Display - how many employees in company?
		cout<<"How many employees are in the company?\n\n";
		cout<<"\nThere are "<<numemps<<" in the company.\n\n";

		//Display - how many absent days work were there?
		cout<<"\nHow many absent days of work were there?\n\n";
		cout<<"\nThere were "<<numdays<<" for a 12 month period.\n\n";

		//Display - What is the average of absent work days per number of employees?
		cout<<"\nWhat is the average of absent work days per number of employees?\n\n";
		cout<<"\nThere were "<<TotalDaysMissed<<" Total Days Missed with  \n";
		cout<<AverageDaysMissed<<" Average Days Missed for a 12 month period.\n\n";

	return 0;
}
//function declarations**************************************************************

	void GetNumEmployees (int &numemps)
	{
		do
		{//prompt for the number of employees
		cout<<"\nPlease enter the number of employee's at the company:  \n";
		cin>>numemps;
		
		//Input Validation
		if (numemps < 1)
		cout<<"\nInvalid.  Please enter a number higher than one: \n";
		}
		while (numemps < 1);

		//FOR loop
		{
		for (int i = 1; i <= numemps; i++);
		}//end FOR loop
	}
//-----------------------------------------------

	int TotalDaysMissed (int numdays)
		{
		double total = 0.0;
		double Totalnumdays = 0.0;
		do
		{//Prompt user for the number of days each employee missed during the past year (365 days).
		cout<<"\nPlease enter the number of days that each employee missed work for the last 12 months:  \n";
		cin>>numdays;	

		//Input Validation
		if (numdays < 0)
		cout<<"\nInvalid.  Please enter a number higher than zero.\n";
		}
		while (numdays < 0);

		//FOR loop
		{
		for (int i = 1; i <= numdays; i++);

		//calculate-add to total
		//Keep track of the total number of days missed(365 days).
		{
		total += Totalnumdays;	//acculumulate running total
		}//end FOR loop
		}//end Total
		return TotalDaysMissed;
	}
//-----------------------------------------------

	float AverageDaysMissed(int, int)
	{
	//Display the average number of days the company's employees are absent (365 days).
	AverageDaysMissed = TotalDaysMissed / 365;
	
	return ;
	}



days.cpp: In function ‘int TotalDaysMissed(int)’:
days.cpp:113: error: invalid conversion from ‘int (*)(int)’ to ‘int’
days.cpp: In function ‘float AverageDaysMissed(int, int)’:
days.cpp:120: error: invalid operands of types ‘int ()(int)’ and ‘int’ to binary ‘operator/’
days.cpp:122: error: return-statement with no value, in function returning ‘float’

Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1368
  • View blog
  • Posts: 4,737
  • Joined: 19-February 09

Re: C++: Using Functions & Pass-by-reference

Posted 15 July 2012 - 07:36 PM

Hi, you can't use the same identifier name in the same scope. You have the function TotalDaysMissed and a variable called TotalDaysMissed. You could change a letter to lowercase.


08	    int TotalDaysMissed (int);
09	//-----------------------------------------------
10	    float AverageDaysMissed(int, int);
11	//-----------------------------------------------
12	int main ()
13	{
 ...
18	    int TotalDaysMissed = 0;
19	    float AverageDaysMissed = 0.0;




In AverageDaysMissed the return value is of type float, you could create a variable of that type.
Was This Post Helpful? 0
  • +
  • -

#15 healix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 29-May 11

Re: C++: Using Functions & Pass-by-reference

Posted 15 July 2012 - 08:01 PM

Passing in two ints is unnecessary , you're not doing anything with them and you're getting an error because the two ints don't have a name such as (int x, int y)

In this function you are trying to calculate two functions not what those functions produce. AverageDaysMissed and TotalDaysMissed are function names.



float AverageDaysMissed()

    {

    //Display the average number of days the company's employees are absent (365 days).

    float AvgDaysMiss;//local variable to return

    AvgDaysMiss = TotalDaysMissed / 365;

     

    return AvgDaysMiss; 


    }




now just go back into your TotalDaysMissed function and change all the "TotalDaysMissed" to something else.

This post has been edited by healix: 15 July 2012 - 08:02 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1