Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   I need help with end of file command (http://www.go4expert.com/forums/i-help-file-command-t24067/)

kjt1991 3Dec2010 08:32

I need help with end of file command
Hey I am having trouble writing my end of file command for this file input. I need it to loop continuously until the end of the file has been reached. My problem is I do not know exactly how to write the file correctly and I cannot figure it out.

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

struct artifact
        string artName;
        string month;
        int year;
        string fName;
        string lName;

int main()
        string x,temp;
        char select;
        ifstream inFile;
        artifact art[31];
        int i = 0;
        cout << "Enter the file name for the artifact list. " << endl;
        cin >> x;
        int totalart = 0;

        while (!x.eof());//read in from file Right here is the end of file command
                        getline(inFile,temp); // uses temp to convert the number to its ascii value
                                art[totalart].year = atoi(temp.c_str()); // and send it to the stream then coverts it back to its number form

                while ( select != 'Q')
                        cout << "If you would like to add an artifact select A." << endl;
                        cout << "If you would like to remove an artifact select R. " << endl;
                        cout << "If you would like to search for an artifact select S. " << endl;
                        cout << "If you would like to print the list select P. " << endl;
                        cout << "If you would like to quit the program select Q. " << endl;
                        cin >> select;

                                if (toupper(select) == 'A')
                                                cout << "Enter the artifact's name, month,year,first name and then last name in that order. " << endl;
                                                cin >> art[totalart].artName;
                                                cin >> art[totalart].month;
                                                cin >> art[totalart].year;
                                                cin >> art[totalart].fName;
                                                cin >> art[totalart].lName;
                                else if (select = 'R')

                                else if (select = 'S')
//                                cout.put(inFile,art.fName);
//                                        cout.put(inFile,art.lName);
//                                        cout.put(inFile,art.month);
//                                        cout.put(inFile,art.year);

                                else if (select = 'P')
                                                for (i=0; i < totalart;i++)
                                                        cout << art[i].month << " " << art[i].year << " " << art[i].fName << " " << art[i].lName <<endl;
                                                cout << " Thank you for visiting the warehouse 31 list " <<endl;

return 0;

jimblumberg 4Dec2010 04:08

Re: I need help with end of file command
You should not try to use eof to terminate the while loop, because the eof flag is not set until after you have read past the end of the file.

Instead if you should test the state of inFile after your call to getline().

/// rest of your getlines.

virxen 4Dec2010 15:09

Re: I need help with end of file command

while (!x.eof());

first of all remove this ( ; ) from there

All times are GMT +5.5. The time now is 02:11.