I'm getting wrong output using fstream
.
I create a text file with 3 words in it. When I run the program from a text file I got the last word in the output twice (The word Apple I got twice in the output). I don't know why.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string fruit;
int point = 0;
ifstream infile;
infile.open("darbas.txt");
while(!infile.eof()){
infile >> fruit;
cout << fruit << endl;
point++;
}
cout << "The number of fruit: " << point << endl;
infile.close();
return 0;
}
I expect the output to be
Banana, Orange, Apple, The number of fruit: 3
Actual output:
Banana, Orange, Apple, Apple, The number of fruit: 4