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):
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
#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;
}
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."