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