Hi programmers ! I have resorted to everything from system ("pause") to cin.get() to even sleep. Yet I am unable to have the average at the end of the cmd screen. Help ?!
#include <iostream>
usingnamespace std;
//Function prototypes
int numberEmployees();
int numberDays(int);
double averageDays(int, int);
int main()
{
//Declaring variables
int employees;
int total;
double average;
//Function call for first function
employees = numberEmployees();
//Function call for second function
total = numberDays(employees);
//Function call for last function prototype
average = averageDays(employees, total);
//Performing output by main function
cout<< "The average number of days a company's employees are absent is: " <<average<<endl;
return 0;
}
//Function header for number of employees
int numberEmployees()
{
int workers;
cout<<"Enter the number of employees in the company: ";
cin>>workers;
//Input validation
while(workers<=1)
{
cout<<" Do not accept number less than 1. Please, enter again: ";
cin>>workers;
}
return workers;
}
//Function header for the number of days
int numberDays(int w)
{
int workers = w;
int total = 0;
int absent;
//Creating a loop for every employees' missed days
for (int count=0; count<workers; count++)
{
cout <<"Enter the number of days each employee missed during past year: "<<count+1<<endl;
cin >>absent;
total+=absent;
//Input Validation
while (absent<0)
{
cout<<"Please, do not enter negative number! Try again: ";
cin>>absent;
}
}
return total;
}
//Function header for average number of days absent
double averageDays (int work, int totl)
{
int w = work;
int t = totl;
double aver;
aver=(w*365)/t;
return aver;
}
Try cleaning input buffer at the beginning, it is possible you have leftovers from previous execution, so when you run it that value makes problems. Also, cleaning input stream at the beginning of that while loop as well might do the work.
Try using something like this:
1 2
cin.get();
cin.ignore(1000,'\n');
I couldn't see any obvious errors in your code, but I found it extremely hard to read.
#include <iostream>
#include <fstream>usingnamespace std;
//Function prototypes
int numberEmployees();
int numberDays(int);
double averageDays(int, int);
int main()
{
//Declaring variables
int employees;
int total;
double average;
ofstream output;
//Function call for first function
employees = numberEmployees();
//Function call for second function
total = numberDays(employees);
//Function call for last function prototype
average = averageDays(employees, total);
//Performing output by main function
cout<< "The average number of days a company's employees are absent is: " <<average<<endl;
output.open("C:\CS140\Project3\output.txt");
system ("pause");
return 0;
}
But have the following errors when debug/compiling:
1>------ Build started: Project: DaysOut, Configuration: Debug Win32 ------
1> DaysOut.cpp
1>c:\users\nelson\documents\visual studio 2010\projects\daysout\daysout\daysout.cpp(33): warning C4129: 'C' : unrecognized character escape sequence
1>c:\users\nelson\documents\visual studio 2010\projects\daysout\daysout\daysout.cpp(33): warning C4129: 'P' : unrecognized character escape sequence
1>c:\users\nelson\documents\visual studio 2010\projects\daysout\daysout\daysout.cpp(33): warning C4129: 'o' : unrecognized character escape sequence
1> DaysOut.vcxproj -> C:\Users\Nelson\documents\visual studio 2010\Projects\DaysOut\Debug\DaysOut.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
I tried going to my C:/ drive and I don't find it. Any idea why ?
Thanks. It does run and compile perfectly now. But I still don't see the output file when I navigate to the C:\ drive. I have also tried refining my search to output. still no luck. any suggestions ? Have I placed the code in the right area ?
// Days Out is the programm that calculates the average number of days a company's
// employees are absent
#include <iostream>
#include <fstream>
using namespace std;
//Function prototypes
int numberEmployees();
int numberDays(int);
double averageDays(int, int);
int main()
{
//Declaring variables
int employees;
int total;
double average;
ofstream output;
//Function call for first function
employees = numberEmployees();
//Function call for second function
total = numberDays(employees);
//Function call for last function prototype
average = averageDays(employees, total);
//Performing output by main function
cout<< "The average number of days a company's employees are absent is: " <<average<<endl;
output.open("C:\\CS140\\Project3\\output.txt");
return 0;
}
//Function header for number of employees
int numberEmployees()
{
int workers;
cout<<"Enter the number of employees in the company: ";
cin>>workers;
//Input validation
while(workers<=1)
{
cout<<" Do not accept number less than 1. Please, enter again: ";
cin>>workers;
}
return workers;
}
//Function header for the number of days
int numberDays(int w)
{
int workers = w;
int total = 0;
int absent;
//Creating a loop for every employees' missed days
for (int count=0; count<workers; count++)
{
cout <<"Enter the number of days each employee missed during past year: "<<count+1<<endl;
cin >>absent;
total+=absent;
//Input Validation
while (absent<0)
{
cout<<"Please, do not enter negative number! Try again: ";
cin>>absent;
}
}
return total;
}
//Function header for average number of days absent
double averageDays (int work, int totl)
{
int w = work;
int t = totl;
double aver;
// Days Out is the programm that calculates the average number of days a company's
// employees are absent
#include <iostream>
#include <fstream>
usingnamespace std;
//Function prototypes
int numberEmployees();
int numberDays(int);
double averageDays(int, int);
int main()
{
//Declaring variables
int employees;
int total;
double average;
ofstream output;
//Function call for first function
employees = numberEmployees();
//Function call for second function
total = numberDays(employees);
//Function call for last function prototype
average = averageDays(employees, total);
//Performing output by main function
cout<< "The average number of days a company's employees are absent is: " <<average<<endl;
output.open("C:\\CS140\\Project3\\output.txt");
return 0;
}
//Function header for number of employees
int numberEmployees()
{
int workers;
cout<<"Enter the number of employees in the company: ";
cin>>workers;
//Input validation
while(workers<=1)
{
cout<<" Do not accept number less than 1. Please, enter again: ";
cin>>workers;
}
return workers;
}
//Function header for the number of days
int numberDays(int w)
{
int workers = w;
int total = 0;
int absent;
//Creating a loop for every employees' missed days
for (int count=0; count<workers; count++)
{
cout <<"Enter the number of days each employee missed during past year: "<<count+1<<endl;
cin >>absent;
total+=absent;
//Input Validation
while (absent<0)
{
cout<<"Please, do not enter negative number! Try again: ";
cin>>absent;
}
}
return total;
}
//Function header for average number of days absent
double averageDays (int work, int totl)
{
int w = work;
int t = totl;
double aver;
aver=(w*365)/t;
return aver;
}