Sign in with
Sign up | Sign in
Your question
Closed

C++ help

Last response: in Applications
Share
June 26, 2011 10:09:00 PM

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

More about : question

Best solution

June 27, 2011 4:16:32 PM
Share

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:
  1. int main()
  2. {
  3. int numberOfEmployees = employee();
  4. int totalDays = daysmissed(numberOfEmployees);
  5. double ave = average(totalDays, numberOfEmployees);
  6. return 0;
  7. }
  8.  
  9. int employee()
  10. {
  11. //Essentially the same code here
  12. // Except you want to return an integer
  13. int number=0;
  14. cout << "How many employees does the company have? ";
  15. cin >> number;
  16.  
  17. // While loop to prevent negative input
  18. while (number <= 0)
  19. {
  20. cout << "The number of employees must be one or greater. Please re-enter: ";
  21. cin >> number;
  22. }
  23.  
  24. return number;
  25. }
  26. int daysmissed(int numEmployees)
  27. {
  28. // This is essentially the same too, but you'd create an array the size
  29. // of the number of employees. Then insert each number that corresponds
  30. // to the employee. This may be useful if you ever need to figure out other
  31. // stats such as which employee missed the most days or what were the
  32. // max and min days missed.
  33. int employee[numEmployees];
  34. int days;
  35. // For loop to go from employee one to two and so on
  36. for (int i = 0; i < numEmployees; ++i) // Using pre-increment is a little faster. Doesn't matter much here, but it will
  37. //when you encounter iterators and complex objects. Might as well get used to using it now.
  38. {
  39. cout << "Days missed by employee # " << i+1 << " :"; //Not sure about the syntax for displaying i+1. But since
  40. //the array starts with zero, you may want to start off with
  41. //1 for the user's sake. I'd just leave it as "i" if it were me.
  42. cin >> days;
  43.  
  44. // While loop to prevent negative input
  45. while (days <= 0)
  46. {
  47. cout << "Days missed must be zero or greater. Please re-enter: ";
  48. cin >> days;
  49. }
  50. // Assign the days missed to the corresponding employee.
  51. employee[i] = days;
  52. }
  53. // Now that you have the days missed for each employee, add them all up!
  54. int totalDays=0;
  55. for(int i=0; i < numEmployees; ++i)
  56. {
  57. totalDays += employee[i];
  58. }
  59.  
  60. return totalDays;
  61. }
  62.  
  63. double average(int numDays, int numEmployees)
  64. {
  65. // Here you have to cast the result to a double because it will return an int no matter what. So 11/2 would return
  66. // 5 unless you cast it to a double.
  67. return (double) numdays/numEmployees; //return static_class<double>(numdays/numEmployees); would be better.
  68. }


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:

  1. int totalDays = daysmissed(numberOfEmployees);
  2. cout << "The total number of days missed is" << totalDays << endl;


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.
June 29, 2011 1:27:34 PM

Have you been able to figure it out?
Related resources
June 29, 2011 6:48:47 PM

I did actually, I didn't end up doing it exactly like you put (I had to make it a tad more simplified) but I did eventually figure out how to implement the "int functions".
June 29, 2011 6:49:09 PM

Best answer selected by ragenalien.
June 29, 2011 7:38:59 PM

This topic has been closed by Area51reopened
!