After I run this code the file "balances.txt" contains no data. Maybe I am misunderstanding how fstream works or I'm confused about the flow direction?
//
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
int main()
{
fstream bank_file;
bank_file.open("balances.txt", fstream::app);
int count = 0;
float number = 0.0;
for (bank_file >> number; count < 100; bank_file >> number)
{
bank_file << number << endl;
number ;
count ;
cout << number << endl;
}
bank_file.close();
CodePudding user response:
To write the numbers 1 to 100 in the file, replace the for
loop in your example with the following:
for( int number = 1; number <= 100; number)
{
bank_file << number << "\n";
}
Note that there are good reasons not to use floating point numbers to represent bank balances. Chief among these is rounding error.
CodePudding user response:
Try this one.
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
int main()
{
fstream bank_file;
bank_file.open("balances.txt", fstream::app);
int count = 0;
float number = 0.0;
for (count= 0; count <= 100; count )
{
bank_file << number << endl;
cout << number << endl;
number ;
}
bank_file.close();
}