output to a .txt file. trouble

Apr 29, 2013 at 12:42am
I am trying to write the output to a .txt file. I have the following code
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
#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\\Project 3\\output.txt");
system("pause");
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;
}


I don't see the output file when I navigate to my 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 ?
Apr 29, 2013 at 1:16am
use outfile instead of cout - when you are wanting to send something to a file
Apr 29, 2013 at 1:25am
closed account (D3pGNwbp)
When opening the file, I would just do 'output.open("output.txt");'. It should automatically save to the location of where your project is saved. And your not actually putting anything into the file, your just creating an empty one.
Last edited on Apr 29, 2013 at 1:46am
Apr 29, 2013 at 3:23am
Still can't get it to work :( .. can somebody run it ?
Apr 29, 2013 at 3:52am
And I don't want to create an empty one.. I want to put the output itself on the file
Apr 29, 2013 at 3:55am
So I was just reading your code, before I run it, I want to ask you what are you trying to output to the file?, at the end I see that you are opening a file, but you are not trying to write to the file anywhere.

for example I didn't see anything like:
 
output << average;


and to make sure things save, you might want to close the file after you write to it to save changes etc.
 
output.close();


let me know what you are trying to write, or if that works, then I will run it
Apr 29, 2013 at 4:40am
Hey ! It works ! Thank You :)
I have included both output << average; and output.close;
I have the average as an output text file Now. Do you know how would I be able to have the entire console on the txt file ?
Apr 29, 2013 at 5:03am
The entire console, if you want to display everything from your console to your text file, I would just take the .

 
output.open("C:\\CS140\\Project 3\\output.txt");


and put it on line 17 instead of where it currently is, so you will have something that looks like:

1
2
ofstream output;
output.open("C:\\CS140\\Project 3\\output.txt");


and I'm not sure where you wrote the output.close(), but I would keep that write before the return 0;

and where ever you have cout in the "main" function, change it to output.

but only the following will be sent to the file:


The average number of days a company's employees are absent is: (average)-variable


if you wanted, I guess you could change all the cout, to output, but then the user wouldn't know what to enter inside the console.
Apr 29, 2013 at 5:26am
Makes perfect sense !
I have used the output to display only the average on the txt file.
Thank You so much for your help :) :*
Topic archived. No new replies allowed.