+ 1

Problem With Files. I don't know why the last is printing twice. I seem to not understand how eof works?

#include<iostream> #include<conio.h> #include<fstream> using namespace std; int main() { ofstream fout; fout.open("Countries.txt"); fout<<"Nepal "<<" India "<<" Afghan "<<endl; fout<<"Britian "<<" France "<< " Paris "; fout.close(); fout.open("Budget.txt"); fout<<10000<<" "<<200<<" "<<2<<endl; fout<<1000<<" "<<10<<" "<<1; fout.close(); string name; long long int b; ifstream fin, sin; //declared two streams one for countries and anther for budget fin.open("Countries.txt"); sin.open("budget.txt"); while(fin.eof()==0) { fin>>name; sin>>b; cout<<name<<": "<<b<<endl; } //this is printing Paris twice. Why? How I deal with this? }

18th Jun 2020, 10:42 AM
Prashant Pant
Prashant Pant - avatar
2 Answers
+ 1
remove the trailing space from Paris " Paris" instead of " Paris " it looks like the because trailing whitespace, the code expect for another input. but since none are found, its using the one that already in the buffer/the last valid input which is Paris EDIT: i notice a typo there in "budget.txt" not using capital B
18th Jun 2020, 11:21 AM
Taste
Taste - avatar
0
Thank You, it did the job.
18th Jun 2020, 12:14 PM
Prashant Pant
Prashant Pant - avatar