fstream beginner program help

This is my very first fstream program, and it is completely confusing to me haha.
1. Consider the following incomplete C++ program:
#include <iostream>
int main()
{
.....
}
A. Write a statement that includes the header files fstream, string, and iomanip in this program.
B. Write statements that declare inFile to be an ifstream and outFile to be an ofstream variable.
C. The program will read data from the file inData.txt and write output to the file outData.txt. Write statements to open both of these files, associate inFile with inData.txt, and outFile with outData.txt.
D. Suppose that the file inData.txt contains the following data:
10.20 5.35
15.6
Randy Gill 31
18500 3.5
A
The numbers in the first line represent the length and width of a rectangle.
The number in the second line represent a radius of a circle.
The third line is a first name, last name, and age.
The fourth line is a savings account balance, as well as an interest rate.
The fifth line containts an Uppercase letter between A and Y(inclusive).

Write statements so that after the program executes, the contents of the file outData.txt are shown below. If necessary, declare additional variables. Your statements should be geenral enough so that if the content of the input file changes and the program is run again (without editing and recompiling), it outputs the appropriate results.


**Here is what I have so far(Granted, I have no idea if this is even the right approach):

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

int main()
{
ifstream inFile;
ofstream outFile;

inFile.open("inData.txt");
outFile.open("outData.txt");
double length;
double width;
double radius;
string name;
int age;
double savings;
double interest;
string letter;

inFile >> length >> width;
outFile << length << width;

inFile >> radius;
outFile << radius;

inFile >> name >> age;
outFile << name << age;

inFile >> savings >> interest;
outFile << savings << interest;

inFile >> letter;
outFile << letter << endl;

inFile.close()
outFile.close()

system("PAUSE");

return 0;
}
Please use code tags(for this topic and all others in the future)

ifstream will read the files data, ofstream will write INTO the file. Unless the program needs to be written into a file, don't use ofstream. Other than that you're on the right track

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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

int main()
{
ifstream inFile;
ofstream outFile;               

inFile.open("inData.txt");
outFile.open("outData.txt");  
double length;                       //variable declarations are correct
double width;
double radius;
string name;
int age;
double savings;
double interest;
string letter;

inFile >> length >> width;
outFile << length << width; //just add an endl after these so it look similar to the file given

inFile >> radius;
outFile << radius;

inFile >> name >> age;
outFile << name << age;   //The file reads "Randy Gill", note that ifstream
                                          //files take whitespaces as the end of reading, which means you're reading 
                                          //Gill into age

inFile >> savings >> interest;
outFile << savings << interest;

inFile >> letter;
outFile << letter << endl;
                                           //simple enough, use cout statements to display
                                           //contents of your vars
inFile.close()
outFile.close()

system("PAUSE");

return 0;
}
Last edited on
Alright, I think I'm understanding it. You say that I'm not writing anything into the file with the "ofstream outFile;" But in Part C of the question above, unless I'm interpreting it wrong, says that I need to write data into the outFile.txt.

"C. The program will read data from the file inData.txt and write output to the file outData.txt. Write statements to open both of these files, associate inFile with inData.txt, and outFile with outData.txt."
I apologize, i was under the impression you were writing to the same file. Ill fix my post, thanks for catching that.
Topic archived. No new replies allowed.