Fizzbuzz fileread
Mar 25, 2014 at 11:51am UTC
The file contains
2 3 10
3 5 20
2 4 20
etc..
now when i read it it prints the last line twice. Please help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
const int Size = 50;
int main()
{
ifstream infile;
char filename[Size];
cout << "Enter the file name to check:" ;
cin.getline(filename, 50);
infile.open(filename);
if (!infile.is_open())
{
cout << "\nFile not found." ;
cout << "\nTerminating program." ;
exit(EXIT_FAILURE);
}
int a,b,c;
while (!infile.eof())
{
infile >> a;
infile >> b;
infile >> c;
for (int i = 1; i <= c; i++)
{
if (i % a == 0 && i % b == 0)
cout << " fb " ;
else if (i % a == 0)
cout << " f " ;
else if (i % b == 0 )
cout << " b " ;
else
cout << i << " " ;
}
cout << "\n" ;
}
if (infile.eof())
cout << "End of file reached.\n" ;
else if (infile.fail())
cout << "Input terminated by data mismatch.\n" ;
else
cout << "Input terminated for unknown reason.\n" ;
return 0;
}
Mar 25, 2014 at 1:11pm UTC
It is probably because you only check for end of file at the top of your while-loop. When you fill your variables, you could get end of file on any of your input lines. Perhaps you could check for eof after each read?
Mar 25, 2014 at 4:47pm UTC
Yeah that makes sense thanks kooth.
Topic archived. No new replies allowed.