storing and writing to a Text file.

I need help with getting my information to store in the txt file.

#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;

int main( )
{
int id = 0;
double workerId = 0.0;
double hourlyRate = 0.0;
double workHours = 0.0;


ifstream workData;
workData.open("workers.txt", ios::in);




if (workData.is_open()== true)
{

{

}

for (int count = 0; count < 5; count = count + 1)
{

cin>>workerId;
cin>>hourlyRate;
cin>>workHours;
cout << "Worker ID: " << workerId << endl;
cout << " Hourly Rate: " << hourlyRate <<endl;
cout <<"Hours Worked: "<<workHours<< endl; workData>>workerId>>hourlyRate>>workHours;
}
workData.close();
}
else
{
cout << "The file could not be opened." << endl;
}

system("pause");
return 0;
}
Have you written any code that outputs to a file?

You need ofstream to output to a file.
Try declaring a ofstream instance, and opening an output file to put the information into (if one doesn't exist, it will create one for you).

Please put your code in the code braces.
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
int main( )
{
    int id = 0;
    double workerId = 0.0;	
    double hourlyRate = 0.0;
    double workHours = 0.0;


    ifstream workData;
    workData.open("workers.txt", ios::in);




    if (workData.is_open()== true)
    {
        //are you not doing anything here?
    {


}//right here, you just signaled the end of "main" - this is an extra bracket

    for (int count = 0; count < 5; count = count + 1) //this for-loop should be moved to inside the "if" block above
// (otherwise it will try to execute even if the file failed to open).
    {

        cin>>workerId;
        cin>>hourlyRate;
        cin>>workHours;
        cout << "Worker ID: " << workerId << endl;
        cout << " Hourly Rate: " << hourlyRate <<endl;
        cout <<"Hours Worked: "<<workHours<< endl;
        workData>>workerId>>hourlyRate>>workHours;
        }

        workData.close();
    }

    else
    {
        cout << "The file could not be opened." << endl;
    }

system("pause"); 
return 0;
}
Last edited on
Can I declare both ifstream workData; and ofstream workData;?
Here is my changes, but I do not see the information going into my file.

#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;

int main( )
{
int id = 0;
double workerId = 0.0;
double hourlyRate = 0.0;
double workHours = 0.0;


ofstream workData;
workData.open("workers.txt", ios::in);

if (workData.is_open()== true)
{

ifstream workData;

for (int count = 0; count < 5; count = count + 1)
{

cin>>workerId;
cin>>hourlyRate;
cin>>workHours;
cout << "Worker ID: " << workerId << endl;

cout << " Hourly Rate: " << hourlyRate <<endl;
cout <<"Hours Worked: "<<workHours<< endl;
workData>>workerId>>hourlyRate>>workHours;
}
workData.close();
}
else
{
cout << "The file could not be opened." << endl;
}

system("pause");
return 0;
}
Sorry for the late reply. I am very busy with school.

We need to declare two different instances, one for the input and one for the output.
1
2
ifstream inFile;
ofstream outFile;


Now we can read in using "inFile" and send data out using "outFile"

We don't want to redeclare a variable, as it won't do what we expect.

Do the same thing you were trying to do before, but use "inFile" for reading FROM the text file, and use "outFile" to send data TO the text file.

Remember to close the streams once you are done using them.
Last edited on
Topic archived. No new replies allowed.