I need help with end of file command

Discussion in 'C++' started by kjt1991, Dec 3, 2010.

  1. kjt1991

    kjt1991 New Member

    Joined:
    Oct 30, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    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.
    Code:
    #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;
    	inFile.open(x.c_str());
    	int totalart = 0;
    
    	while (!x.eof());//read in from file [B]Right here is the end of file command[/B]
    		{
    			getline(inFile,art[totalart].artName);
    			getline(inFile,art[totalart].month);
    			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
    			getline(inFile,art[totalart].fName);
    			getline(inFile,art[totalart].lName);
    
    			totalart++;
    		}
    		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;
    					
    						totalart++;
    		
    					}
    				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;
    						}
    					}
    				else
    					{
    						inFile.close();
    						cout << " Thank you for visiting the warehouse 31 list " <<endl;
    					}
    	}
    
    system("PAUSE");
    return 0;
    		
    }
     
  2. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    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().

    Code:
    while(getline(inFile,art[totalart].artName))
    {
       /// rest of your getlines.
     
  3. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    first of all remove this ( ; ) from there
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice