Status
Not open for further replies.

ragenalien

Distinguished
May 6, 2011
61
0
18,590
I'm a new programmer looking for a bit of help (before you veterans start complaining i'm not looking for anyone to do my project for me). I'm making a simple program and it works perfectly. Only problem is I didn't realize one of the guidelines was that we can't use global variables. I need a little help changing things around. I've tried and tried but can't seem to find a solution that works. Here is my working program -
P.s. the restriction was that we cannot use global variables that are not strictly defined so the "double totaldays = 0" is fine, the others need to be moved.

// Purpose: To calculate the average number of days missed
// for an inputed number of employees
// Program introduces functions

#include <iostream>
#include <iomanip>
using namespace std;

// First function:
void employee();
// Second function
void daysmissed(int);
// Third function
void average(double);

// Global vairables
int number;
int days;
double totaldays = 0;
double final;

// Main function
int main()
{
employee();

daysmissed(days);

average(final);

return 0;
}

// First - Recieves the number of employees
void employee()
{

cout << "How many employees does the company have? ";
cin >> number;

// While loop to prevent negative input
while (number <= 0)
{
cout << "The number of employees must be one or greater. Please re-enter: ";
cin >> number;
}

return;
}

// Second - Recieves number of days missed per employee
void daysmissed(int days)
{
int num;

// For loop to go from employee one to two and so on
for (num = 1; number >= num; num++)
{
cout << "Days missed by employee # " << num << " :";
cin >> days;

// While loop to prevent negative input
while (days <= 0)
{
cout << "Days missed must be zero or greater. Please re-enter: ";
cin >> days;

}

// Uses a variable to keep track of the total days missed
// by employees based on user input
totaldays = days + totaldays;
}
cout << endl;
return;
}

// Third - Calculate the average number of days
// missed by the employees
void average (double final)
{
// Uses the "final" variable to display the answer
final = totaldays / number;
cout << "The average number of days missed per employee is " <<
final << endl;
return;
}
 
Solution
I would structure this program a little differently. You could make a "Company" object or something that would keep track of all the data of the employees. But it seems like the assignment just wants you to use functions to compute the average. Instead of using global variables outside of the main function, you could bring them inside and have each of the functions return a value to them. One thing that can cause major errors in your program is that you're passing uninitialized parameters to your daysmised() and average() functions by value. When you declare the variables, the compiler puts garbage into the variable until it's initialized. But there's no real need to pass the variables anyway because they are global and accessible by...

rockg06

Distinguished
Jun 27, 2011
7
0
18,520
I would structure this program a little differently. You could make a "Company" object or something that would keep track of all the data of the employees. But it seems like the assignment just wants you to use functions to compute the average. Instead of using global variables outside of the main function, you could bring them inside and have each of the functions return a value to them. One thing that can cause major errors in your program is that you're passing uninitialized parameters to your daysmised() and average() functions by value. When you declare the variables, the compiler puts garbage into the variable until it's initialized. But there's no real need to pass the variables anyway because they are global and accessible by all the functions in this file.

So you can arrange it something like this:
[cpp]int main()
{
int numberOfEmployees = employee();
int totalDays = daysmissed(numberOfEmployees);
double ave = average(totalDays, numberOfEmployees);
return 0;
}

int employee()
{
//Essentially the same code here
// Except you want to return an integer
int number=0;
cout << "How many employees does the company have? ";
cin >> number;

// While loop to prevent negative input
while (number <= 0)
{
cout << "The number of employees must be one or greater. Please re-enter: ";
cin >> number;
}

return number;
}
int daysmissed(int numEmployees)
{
// This is essentially the same too, but you'd create an array the size
// of the number of employees. Then insert each number that corresponds
// to the employee. This may be useful if you ever need to figure out other
// stats such as which employee missed the most days or what were the
// max and min days missed.
int employee[numEmployees];
int days;
// For loop to go from employee one to two and so on
for (int i = 0; i < numEmployees; ++i) // Using pre-increment is a little faster. Doesn't matter much here, but it will
//when you encounter iterators and complex objects. Might as well get used to using it now.
{
cout << "Days missed by employee # " << i+1 << " :"; //Not sure about the syntax for displaying i+1. But since
//the array starts with zero, you may want to start off with
//1 for the user's sake. I'd just leave it as "i" if it were me.
cin >> days;

// While loop to prevent negative input
while (days <= 0)
{
cout << "Days missed must be zero or greater. Please re-enter: ";
cin >> days;
}
// Assign the days missed to the corresponding employee.
employee = days;
}
// Now that you have the days missed for each employee, add them all up!
int totalDays=0;
for(int i=0; i < numEmployees; ++i)
{
totalDays += employee;
}

return totalDays;
}

double average(int numDays, int numEmployees)
{
// Here you have to cast the result to a double because it will return an int no matter what. So 11/2 would return
// 5 unless you cast it to a double.
return (double) numdays/numEmployees; //return static_class<double>(numdays/numEmployees); would be better.
}
[/cpp]

I haven't compiled any of this code, so there may be syntax errors in it. However, this should be sufficient to complete the assignment. You would want to put most of the text output in your driver program (i.e. inside the main() function). So before each function call you can do something like:

[cpp]int totalDays = daysmissed(numberOfEmployees);
cout << "The total number of days missed is" << totalDays << endl;[/cpp]

I hope this helps. Let me know if you have any questions.

RGJ

P.S. When I reviewed the code section, the comments make it look untidy because the code window is smaller than the text editor window. I apologize for it being messy.
 
Solution
Status
Not open for further replies.