c2276 error

i get an error in this line
while(!infile.eof) // To get you all the lines.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void main ()
{
     string STRING;
	ifstream infile;
	infile.open ("BOOKINGS.txt");
        while(!infile.eof) // To get you all the lines.
        {
	        getline(infile,STRING); // Saves the line in STRING.
	        cout<<STRING; // Prints our STRING.
        }
	infile.close();
	system ("pause");
}
Last edited on
void main() isn't valid. it should always be int main()

The correct syntax would have been while (!infile.eof())
However, it is not a good idea to loop on eof(). Firstly there may be other errors which mean the eof flag is never set. Secondly, the string is used without checking whether or not the getline() was successful.

this would be a better approach:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main ()
{
    string STRING;
    ifstream infile("BOOKINGS.txt");

    while (getline(infile,STRING))
    {
        cout << STRING << endl;
    }
    
}
Topic archived. No new replies allowed.