由于某种原因,outfile 没有在 Windows 中输出 txt 文件。它在 Mac 上运行良好(在下面的代码中注释掉的行),但在 Windows 中我无法将其输出。任何指导将不胜感激。谢谢!
#include <iostream>
#include <fstream>
using namespace std;
int iNumberOfEmployees(); // this function prompts the user for the number of employees.
int iMissedDays(int employees); // this function prompts the user for the number of days each employee missed and returns the total.
double dAverageMissedDays(int employees, double missedDays); // this function calculates the average missed days per employee.
int main() {
int iGetEmployees = iNumberOfEmployees();
int iGetMissedDays = iMissedDays(iGetEmployees);
cout << "Average missed days: "<< dAverageMissedDays(iGetEmployees,iGetMissedDays) << endl;
// outputs results to a text file
ofstream outfile;
// outfile.open ("/Users/chrisrukan/Documents/CS140/Project 3/output.txt");
outfile.open ("C:\CS140\Project 3\output.txt");
if (outfile.is_open()) {
outfile << "Average missed days: "<< dAverageMissedDays(iGetEmployees,iGetMissedDays);
outfile.close();
}
else {
cout << "Error opening file" << endl;
}
}
int iNumberOfEmployees () {
int iTotalEmployees = 0;
// loop checks that the user doesn't enter a negative number of employees
while (iTotalEmployees <= 0) {
cout << "Enter the number of employees: ";
cin >> iTotalEmployees;
}
return iTotalEmployees;
}
int iMissedDays (int iEmployees) {
int iTotalMissedDays = 0;
int iIndividualMissedDays;
// loop asks the user the missed days for each individual employee
for (int i = 1; i <= iEmployees; i++) {
iIndividualMissedDays = -1;
// loop checks that user doesn't enter a negative number
while (iIndividualMissedDays < 0) {
cout << "Enter employee " << i << "'s number of missed days: ";
cin >> iIndividualMissedDays;
}
iTotalMissedDays += iIndividualMissedDays;
}
return iTotalMissedDays;
}
double dAverageMissedDays (int iEmployees, double dMissedDays) {
return dMissedDays / iEmployees;
}