Thread: What do you think of my program (intermediate lvl)

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    13

    What do you think of my program (intermediate lvl)

    This is a program I have to do for class and just wanted to get some feedback. Comments and critiques welcomed.
    -------------
    This program will calculate and display the average number of days a company's employees are absent. The program should have the following functions.

    A function called by main that asks the user for the number of employees in the company. This value should be returned as an int. (The function accepts no arguments.)

    A function called by main that accepts one arument, the number of employees in the company. The function should ask the user to enter the number of days eache employee missed during the past year. The total of these days should be returned as an int.

    A function called by main that takes two arguments, the number of employees in the company and the total number of days absent for all employees during the year. The function should return, as a double, the averae number of days absent. (This function does not perform screen output and does not ask the user for input.)

    Input validation: Do not accept a number less than 1 for the number of employees. Do not accept a negative number for the days an employee missed.

    Code:
    //This program calculates and displays the average number of days a company's employees are absent
    //written by: XXXXX  XXXX
    //CS 241
    //Exercise #1
    //Due Date: January 29, 2008
    
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    //Function Prototypes
    int num_Employees();   
    int daysMissed(int);
    double calc_daysAbsent(int, int);
    
    int main()
    {
        int numEmployees,
            total_missedDays;
            
        double absentAverage;
        
        cout <<"This program calculates and displays the average number of days a company's" << endl;
        cout <<"employees are absent." << endl;
        numEmployees = num_Employees();
        total_missedDays = daysMissed(numEmployees);
        absentAverage = calc_daysAbsent(numEmployees, total_missedDays);
        cout <<"The average number of days " << numEmployees << " employee(s) missed is " <<
                                                                  absentAverage <<"." << endl;
        
    system("pause");
    return 0;    
    }
    //************************************************************
    //Definition of function calc_daysAbsent.                    *
    //This function calculates the average number of days absent *
    //************************************************************
    double calc_daysAbsent(int numEmployees, int total_missedDays)
    {
        double absentAverage;
        
        absentAverage = total_missedDays/numEmployees;
        return absentAverage;       
           
    }
    
    
    //****************************************************************************************************
    //Definition of function daysMissed.                                                                 *
    //This function asks the user to enter the number of days each employee missed during the past year. *
    //****************************************************************************************************
    int daysMissed(int numEmployees)
    {
        int count,
            total_missedDays,
            sum = 0;
        
            for(count = 1; count <= numEmployees; count++)
            {
            cout <<"How many days were missed by employee " << count << "?" << endl;
            cin  >> total_missedDays;
                 while(total_missedDays < 0) //test to see if inputed information is valid
                 {
                 cout << total_missedDays << " is invalid. Please enter a non-negative number." << endl;  
                 cout <<"How many days were missed by employee " << count << "?" << endl;  
                 cin  >> total_missedDays;                  
                 }
            sum += total_missedDays;     
                 
            }
            return sum;
    }
    
    
    //******************************************************************************
    //Definition of function numEmployees.                                         *
    //This function asks the user for the number of employees in the company.      *
    //******************************************************************************
    int num_Employees()
    {
        int numEmployees;
        
        cout <<"Please enter the number of employees in the company." << endl;
        cin  >> numEmployees;
        while(numEmployees < 1) // checks to see if the number of employees in the company is less than 1
                                // if less than one, it continues the loop until it becomes valid
        {
        cout << numEmployees << " is invalid. The number of employees must be greater than 0." << endl;
        cout <<"Please enter the number of employees in the company." << endl;               
        cin  >> numEmployees;
        }
        return numEmployees;   
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Some of your indentation has gone awry, especially while loops.

    Be careful of the difference between integer division and floating-point division.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    13
    Quote Originally Posted by tabstop View Post
    Some of your indentation has gone awry, especially while loops.

    Be careful of the difference between integer division and floating-point division.
    I'm not sure what you mean by my use of while loops. I used them to make sure the user entered valid information. For example, if they entered invalid data, instead of executing the program over, it ask them to again until valid information is correctly entered. With the division section, I know some answers can come out as a decimal or floating number, but the function required, calcAverage wants it to be an integer.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by -Syntax View Post
    I'm not sure what you mean by my use of while loops. I used them to make sure the user entered valid information. For example, if they entered invalid data, instead of executing the program over, it ask them to again until valid information is correctly entered. With the division section, I know some answers can come out as a decimal or floating number, but the function required, calcAverage wants it to be an integer.
    We all know what while loops are -- and you're using them properly -- they merely do not appear properly in your source code. (They should be indented one level between the braces just like everything else.)

    As for your division section, your answers can't come out as decimal or floating-point, even though they should (and your specification requires such), because of the way you are doing the division. You need to use floating-point division, not integer division.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    13
    Quote Originally Posted by tabstop View Post
    We all know what while loops are -- and you're using them properly -- they merely do not appear properly in your source code. (They should be indented one level between the braces just like everything else.)

    As for your division section, your answers can't come out as decimal or floating-point, even though they should (and your specification requires such), because of the way you are doing the division. You need to use floating-point division, not integer division.
    I see what you mean now. What it be best if I just did this for the division section:

    absentAverage = static_cast<double>(total_missedDays)/ static_cast<double>(numEmployees); I believe that's right, if not, please inform me.

    With the indentation, thanks for the link. My C++ teachers never made a big deal about how the code should be indented properly. I'll remember that for further use.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I believe you only need to cast the first number:
    absentAverage = static_cast<double>(total_missedDays) / numEmployees;
    That should force the result to be a double.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Elysia View Post
    I believe you only need to cast the first number
    Or the second. As long as one of the operands is a double then it will perform floating point division.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM