In this code, fout is an ofstream object, It supposes to write to a file called output.txt. For a reason output.txt always empty!. I would ask about the mistake I did in the code :
#include<iostream>
#include<fstream>
#include<stdio.h>//to pause console screen
using namespace std;
double Volume(double);
int main() {
ifstream fin; //read object
ofstream fout;// writing object
double Radius;
fin.open("input.txt");
fout.open("output.txt");
if(!fin){
cout<<"There a problem, input.txt can not be reached"<<endl;
}
else{
fin>>Radius;
fout<<Volume(Radius);
cout<<"Done! check output.txt file to see the sphare volume"<<endl;
}
getchar();//to pause console screen
return 0;
}
double Volume(double r){
double vol;
vol= (4.0/3.0)*3.14*r*r*r;
return vol;
}
“output.txt always empty”
I suspect that you are not allowing
foutto flush its output. Do either of these statements apply to you?You check the contents of “output.txt” after the
getchar()isinvoked, but before the program ends?
You end the program with a Ctrl+C?
If so, you have not allowed the data to be written to
fout. You can solve this problem by avoiding those two conditions, or doing one of these:Add
fout << endlafter you write your data, orAdd
fout << flushafter you write your data, orAdd
fout.close()after you write your data.