So i want to make a directory, when a certain scope of work is searched, the details of the supplier will appear, my problem is how do i loop through the lines on the file?
My code is like this:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
inFile1.open("TelephoneDirectory.txt");
//exit if file cannot be opened
if (!inFile1)
{
cout <<"Cannot open input file."<<endl;
return 0;
}
//Order of data from file
inFile1 >>Scopedoc >> cnamedoc >> cpersondoc >> cnumberdoc;
do
{
//Ask user to enter scope of work.
cout<<"Please enter the scope / nature of work: " <<endl;
cin>> Scope;
else
cout<<"Number was not in directory."<<endl;
}
cout <<"Want to continue looking? 0 for no, 1 for yes. " <<endl;
cin >>playagain;
}
while (playagain == 1);
system("PAUSE");
return 0;
}
and my directory is like this:
Tiles FloorToGo Hans 09351864545
ToiletPartition Jebsen RA 09564875560
Lockset Dorma Steve 09546556123
Tiles Itrade Sandy 09224568916
As it is right now only the first line appears when i search for "Tiles". and when i tried to search for Lockset, Number was not in directory will appear.
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
usingnamespace std;
int main()
{
string Scopedoc;
string cnamedoc;
string cpersondoc;
string cnumberdoc;
ifstream inFile1("TelephoneDirectory.txt");
// exit if file cannot be opened
if (!inFile1)
{
cout << "Cannot open input file." << endl;
return 0;
}
int playagain = 1;
while (playagain == 1)
{
// Ask user to enter scope of work.
string Scope;
cout << "Please enter the scope / nature of work: " << endl;
cin >> Scope;
// read data from file
bool found = false;
while (inFile1 >> Scopedoc >> cnamedoc >> cpersondoc >> cnumberdoc)
{
if ( Scope == Scopedoc)
{
found = true;
cout << "\nCompany Name: " << cnamedoc
<< "\nContact Person: " << cpersondoc
<< "\nContact Number: " << cnumberdoc
<< endl;
}
}
if (!found)
{
cout << "Number was not in directory." << endl;
}
cout << "\nWant to continue looking? 0 for no, 1 for yes. " << endl;
cin >> playagain;
if (playagain == 1)
{
inFile1.clear(); // reset flags
inFile1.seekg(0); // reposition to start
}
}
system("PAUSE");
}
Please enter the scope / nature of work:
Tiles
Company Name: FloorToGo
Contact Person: Hans
Contact Number: 09351864545
Company Name: Itrade
Contact Person: Sandy
Contact Number: 09224568916
Want to continue looking? 0 for no, 1 for yes.
1
Please enter the scope / nature of work:
Lockset
Company Name: Dorma
Contact Person: Steve
Contact Number: 09546556123
Want to continue looking? 0 for no, 1 for yes.
0
Press any key to continue . . .