How is these functions supposed to look like?

So far, I did a function called by main with no parameters like was supposed to, but I also have call another function by main that accepts two arguments.
(The name of the file stream object as a reference parameter of type ofstream, and the number of employees in the company.) This function should ask the user to enter as integers for each employee: the employee number(ID) and the number of days missed should be written to the report file in this function. The total of these days should be returned as int.

This is what I have so far, but it's wrong:

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



int getEmployeeNumber();
int fileAndEmployees (ofstream, int);


int main() {


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

int employees;
employees =getEmployeeNumber();
int fileAndEmployees (ofstream &ofs, int employees);
for (idNumber = 1; idNumber < employees; idNumber++);


return 0;
}

int getEmployeeNumber()

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

struct fileAndEmployee(ofstream, int)
{
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 idNumber, daysAbsent;

}






@imastruggler

1
2
3
4
5
6
7
8
9
10
11
struct fileAndEmployee(ofstream, int)
{
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 idNumber, daysAbsent;

}

Problem 1..
You have the function you're trying to call, made out as a struct. Change struct to int.

Problem 2..
You can't return more than variable from a function.

Problem 3..
Line 12 of your code (counting actual lines with text, not any blank lines), you're declaring a function by starting it with int. Remove the int. Though it still won't compile or run, since you have not declared idNumber, returned any data from fileAndEmployee(ofstream, int) And you also have to put a variable name AFTER the int, in the function call.

You could read up on passing variables to function by reference, then when those variables change, they're changed in memory also, and main() can access them. It's a thought.


I called to main with this:

fileAndEmployees (ofstream &ofs, int employees);

and defined with this


int fileAndEmployees(ofstream, int employees)
{
int idNumber;
for (idNumber = 1; idNumber < employees; 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;

}



but it kept saying "Use of undeclared identifier "ofs" in the main() part

why are u using ofstream. It is not used anywhere in your code.
"The name of the file stream object as a reference parameter of type ofstream"

It asks me to use a sort of file stream as a parameter. If I can't use ofstream then what should I do?
First please use code tags when posting code (Highlight the code then press the <> icon to the right).

Next look at these snippets:

Function call.
fileAndEmployees (ofstream &ofs, int employees);

Function implementation.
int fileAndEmployees(ofstream, int employees)

Function prototype.
int fileAndEmployees (ofstream, int);
First all of these snippets must match as to the type and number of parameters.

Second look at your description of this function from your first post:
but I also have call another function by main that accepts two arguments.
(The name of the file stream object as a reference parameter of type ofstream, and the number of employees in the company.)


So this tells you that you must have a function with a ofstream reference parameter. Do you know what using a reference parameter actually means? Look at your function prototype, where is that reference parameter? Hint: It doesn't exist. To tell the compiler that you're passing a variable by reference you use the & after the parameter type in both the prototype and implementation, by the way the prototype should look exactly like the implementation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Prototype:
int fileAndEmployees (ofstream& file_stream, int number_of_employees);

...
// In main().
   ofstream file_out("output.txt");
   int number_of_employees = 10;
...
   // Function call.
   int value_returned = fileAndEmployees(file_out, number_of_employees);
...

// Implementation.
int fileAndEmployees (ofstream& file_stream, int number_of_employees)
{
    file_stream << number_of_employees << '\n';
   // Your code.
}

Last edited on
Topic archived. No new replies allowed.