Why is the loop repeating 12 times?

It's only supposed to be repeating 5 times...

I'm sorry it appears as if I'm spamming you guys with this, but I want to learn how to use this thing properly.


What went wrong with my code?:



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



int getEmployeeNumber();
int fileAndEmployees (ofstream& outFile, int);
double getAverage(int, int);
double total=0;



int main() {

string name;
int employeeNum, employeeID, daysMissed;


ofstream outFile;
outFile.open ("employeesAbsent.txt");

cout << fixed << showpoint << setprecision(1);


employeeNum = getEmployeeNumber();

while (employeeNum <1)
{
cout << "Invalid amount.The number of employees can't be less than 1."<<endl;
cin >>employeeNum;
}

//Loop
for (employeeNum = 0; employeeNum <= 5; employeeNum) {
fileAndEmployees (outFile, employeeNum);
outFile << fileAndEmployees (outFile, employeeNum);
}

total = 0;
total += daysMissed;
outFile <<"The "<<employeeNum<< "were absent a total of "<<total <<"days."<<endl;
outFile << "Your average number of days missed is: "<<getAverage (employeeNum, daysMissed)<<endl;



cout << "Programmer: "<< name<<endl;
outFile << "Programmer: "<<name<<endl;

outFile.close();


return 0;
}


int getEmployeeNumber()

{
int employeeNumber;
cout << "Please enter the number of employees in the company: "<<endl;
cin >> employeeNumber;
return employeeNumber;


}
//Where I defined it
int fileAndEmployees(ofstream& outFile, int employees)
{
outFile << employees;


int idNumber;
cout << "Please enter an employee ID: "<<endl;
cin >> idNumber;
int daysAbsent;
cout << "Please enter the number of days this employee was absent: "<<endl;
cin>> daysAbsent;
return employees;

}


double getAverage (int employees, int absences)
{
double average = 0;
int total;
total += absences;
average = total/employees;
return average;


}

for (employeeNum = 0; employeeNum <= 5; employeeNum)
There's no increment in your last part.
C++ requires you to specify what value you want to change and how.
By just writing employeeNum, C++ doesn't assume you mean to increment it by one.

I hope this is what you're looking for.
I added an increment and it's still doing that :/
Hi, imastruggler,
I don't really want to depress you, but there's more than one error in your code.
Is it possible that you have moved variable from main() to other function keeping them duplicates?
Your function getAverage() doesn't need to define any variable - perhaps one, if you want, but it could be avoided.

But the main point is that you don't save all the values you receive from the user, because you keep the same variables to store them. It means that at every cycle of your loop, you loose the previous values, which are replaced by the new ones.
You should consider saving them into a container, such as a vector or an array (or at least, not recommanded, a C-style array).



What follows id an example of code with a possible solution based on vectors.
If you want to try to get the result by yourself, just ignore it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iomanip>
#include <fstream>
#include <iostream>
#include <limits>
#include <string>
#include <vector>

using namespace std;

struct Employee {
    int id;
    int days_of_absence;
};

int getEmployeeNumber();
void fileAndEmployees(vector<Employee>& abs_per_id, int how_many);
double getAverage(int, int);
double total=0;

int main()
{
    // Get the total number of employees
    int employeeNum = getEmployeeNumber();

    // Create a container where store their data
    vector<Employee> employes;

    // Fill the container with employees' data, such as ID and
    // days of absence for each of them.
    fileAndEmployees (employes, employeeNum);

    // Calculate the total days of absence
    int totalabsences = 0;
    for(int i=0; i<employes.size(); i++) {
        totalabsences += employes.at(i).days_of_absence;
    }

    // Open a file and write down:
    //    - number of employees
    //    - ID and days of absence of each of them
    //    - total days of absence
    ofstream outFile("employeesAbsent.txt");
    outFile << "Total employee number = " << employeeNum << "\n\n";
    for(int i=0; i<employes.size(); i++) {
        outFile << "Employee ID " << employes.at(i).id 
                << " has been absent " << employes.at(i).days_of_absence
                << " days.\n";
    }
    outFile <<"\nIn total, they have been absent " << totalabsences 
            << " days.\n";
    outFile << "\nYour average number of days missed is: "
            << getAverage(employeeNum, totalabsences) << endl;

    string name = "imastruggler";
    cout << "Programmer: "<< name << endl;
    outFile << "\nProgrammer: " << name << endl;

    outFile.close();

    cout << "\n\nPress ENTER to continue...\n";
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    return 0;
}

int getEmployeeNumber()
{
    int employeeNumber = 0;
    while(employeeNumber < 1) {
        cout << "\nPlease enter the number of employees in the company: ";
        cin >> employeeNumber;
        if(employeeNumber < 1) {
            cout << "Invalid amount. The number of employees can't be "
                    "less than 1.\n";
        }
    }

    return employeeNumber;
}

//Where I defined it
void fileAndEmployees(vector<Employee>& abs_per_id, int how_many)
{
    for (int i = 0; i < how_many; i++) {
        Employee tmpempl;
        cout << "Please enter an employee ID: ";
        cin >> tmpempl.id;
        cout << "Please enter the number of days this employee was absent: ";
        cin >> tmpempl.days_of_absence;

        abs_per_id.push_back(tmpempl);
        cout << "Employee n. " << i << ": ID: " << abs_per_id.at(i).id
             << "; days of absence: " << abs_per_id.at(i).days_of_absence
             << " --> recorded\n";
    }
}

double getAverage (int employees, int absences)
{
    return double(absences)/double(employees);
}



My output:

Please enter the number of employees in the company: 3
Please enter an employee ID: 11
Please enter the number of days this employee was absent: 4
Employee n. 0: ID: 11; days of absence: 4 --> recorded
Please enter an employee ID: 22
Please enter the number of days this employee was absent: 8
Employee n. 1: ID: 22; days of absence: 8 --> recorded
Please enter an employee ID: 33
Please enter the number of days this employee was absent: 16
Employee n. 2: ID: 33; days of absence: 16 --> recorded
Programmer: imastruggler


Press ENTER to continue...


My employeesAbsent.txt:
1
2
3
4
5
6
7
8
9
10
11
Total employee number = 3

Employee ID 11 has been absent 4 days.
Employee ID 22 has been absent 8 days.
Employee ID 33 has been absent 16 days.

In total, they have been absent 28 days.

Your average number of days missed is: 9.33333

Programmer: imastruggler

@Enoizat

I wasn't supposed to void anything and I have no idea where those i's came from, or what these limits or vectors are (and I'll doubt I'll use them for this), but I'll definitely keep what you put in mind. I really appreciate you using your time on explaining it. I was also supposed to output two columns with the employee id and days absent, but I don't think I've gotten to that part yet.

I wasn't supposed to void anything

So, do you have restrictions? Are we forced to use those functions with those prototypes? And we can’t use containers?
That’s made things a bit more complicated, but won’t be a problem.

Hope this scheme can help you, otherwise just ask:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <fstream>

int getEmployeeNumber();
int fileAndEmployees(ofstream& outFile, int);
double getAverage(int, int);

int main()
{
    int employees = getEmployeeNumber();
    ofstream outFile("employeesAbsent.txt");
    write the total number of employees on outfile

    create a variable of an appropriate type and
        call fileAndEmployees() in a way that it's return value
        will be stored into that variable.
        Don't forget to pass the file and the number of employees.

    create a variable of an appropriate type and
        call getAverage() in a way that it's return value
        will be stored into that variable.
        Don't forget to pass the number of employees and the absence days.
    write the stored result into outFile

    return 0;
}

int getEmployeeNumber()
{
    // This function asks for the number of employee.
    // There must be at least one employee.
    // No further controls will be implemented.

    start with a variable which stores the number of employees set to zero
    loop until that number is less than one (at the beginning is zero, so...)
    ^    ask the user for a number
    |    insert that number into the variables which stores the num. of empl.
    ^    check if it is at least one, and in case...
    |        ...print out a message error
    |--<--<-- 

    return number of employees
}

int fileAndEmployees(ofstream& outFile, int employees)
{
    // This function writes on file and sends to screen the IDs and 
    // the absence days of each employee.
    // To perform such a task, it enters a loop which iterates as many
    // times as the number of employees.
    // At each iteration, it asks the user for
    //   - employee ID 
    //   - employee's absence days.
    // Then, it writes them on the given file.
    // If the data are to be output on screen, it's possible to add an
    // instruction which says: "written on file: " << ID << days of absence.
    // Before asking for new data, the loop will increase the total number
    // of absence days, which will be later returned to the caller.

    create a variable to store the total absence days and set it to zero
    for a number (for example 'i') which varies from 0 to the number
    ^   of employees less 1 --> if employees == 5, 'i' must run from 0 to 4.
    |   
    |   ask the user for an employee's ID
    ^   create a variable to store that number
    |   insert the user's answer in that number
    |   
    ^   ask the user for an employee's days of absence
    |   create a variable to store that number
    |   insert the user's answer in that number
    ^
    |   write employee's ID and absence days on the given file
    |
    ^   increase the variable which store the total absence days by
    |       the current absence days
    |<--<--

    return total days of absence of all the employees
}

double getAverage (int employees, int absences)
{
    // This function calculate the average of days of absence,
    // i.e. absences / employees
    // To avoid data loss, it turns those, which are ints, into doubles
    // before performing any calculation.

    turn employess into double
    turn absences into double

    return the ratio of absences to employees (both turned into doubles)
} 

Topic archived. No new replies allowed.