0

The code below is failed to let me input the data twice because im only able to open the text file and read it once. I have included a part of the file.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;

class Record
{
private:
	string customerID,companyname,contactname,contacttitle,address,city,region,postalcode,country,phone,fax;
	int choice;
public:
	
	void Cout(){
		cout << "customer id: " << customerID << endl;
	    cout << "company name: " << companyname << endl;
	    cout << "contact name: " << contactname << endl;
	    cout << "contact title: " << contacttitle << endl;
	    cout << "address: " << address << endl;
	    cout << "city: " << city << endl;
	    cout << "postalcode: " << postalcode <<endl;
	    cout << "region: " << country << endl;
	    cout << "phone: " << phone << endl;
	    cout << "fax: " << fax << endl;
	    cout << "country: " << country << endl;
	}
	int Choice() {
		cout << "enter the number of what you want to do" <<endl;
		cout << "[1] view a record" << endl;
		cout << "[2] add new record" << endl;
		cout << "[0] quit " << endl;
		cout << "enter choice: ";
		cin >> choice;
		return choice;
	}
	void Assign (string token[]){
		customerID = token[0];
		companyname = token[1];
		contactname = token[2];
		contacttitle = token[3];
		address = token[4];
		city = token[5];
		region = token[6];
		postalcode = token[7];
		country = token[8];
		phone = token[9];
		fax = token[10];
	}

};
int main()
{
   string input;     // To hold file input
   fstream nameFile;     // File stream object
   Record Rec;
   int intfind;
   string customerid;
   char tab = '\t';
   int i=0;
   int choice;
   int size = 11;
   choice = Rec.Choice();
   while (!choice == 0) {
	   if(choice == 1){
		   nameFile.open("J://Customers.txt");
		   if (!nameFile){
			  cout << "ERROR: Cannot open file.\n";
			  return 0;}
		   cout << "enter customer id: ";
		   cin >> customerid;
		   while (!nameFile.eof()) {
			  getline(nameFile,input,'\n');
			  intfind = input.find(customerid,0);
			  if (intfind >= 0) {
				 istringstream iss(input);				
				string token [11];
				while(getline(iss, token[i], '\t')){
					Rec.Assign(token);
					i++;}
				Rec.Cout();}
		   }
		   cin.ignore();}
	   if (choice == 0){
		   return 0;}
	   nameFile.close();
	   choice = Rec.Choice();}
   nameFile.close();
   return 0;
}
CustomerID	CompanyName	ContactName	ContactTitle	Address	City	Region	PostalCode	Country	Phone	Fax
ALFKI	Alfreds Futterkiste	Maria Anders	Sales Representative	Obere Str. 57	Berlin		12209	Germany	030-0074321	030-0076545
ANATR	Ana Trujillo Emparedados y helados	Ana Trujillo	Owner	Avda. de la Constitución 2222	México D.F.		05021	Mexico	(5) 555-4729	(5) 555-3745
ANTON	Antonio Moreno Taquería	Antonio Moreno	Owner	Mataderos  2312	México D.F.		05023	Mexico	(5) 555-3932	
AROUT	Around the Horn	Thomas Hardy	Sales Representative	120 Hanover Sq.	London		WA1 1DP	UK	(171) 555-7788	(171) 555-6750
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Ancient Dragon
0

The loop starting on line 73 is wrong. eof() doesn't work the way you think. And you don't need that third parameter '\n' to getline() because that is the default.

while( getline(nameFile,input) )
{
   // blabla
}
nameFile.clear(); // clear error
nameFile.seekg(0, ios::beg); // back to beginning of file

After reading the end-of-file, you have to clear the stream error and set the file pointer back to the beginning of the file so that the file can be re-read. See above code snippet how to do that.

Also, move the open statement up before line 64 so that you only attempt to open the file once.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.