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;
}
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;
}