As already mentioned, you should avoid testing of eof to control your read loop. This:
Code:
...
while(!inData.eof())
{
inData >> num >> pri;
...
Should be this:
Code:
...
while(inData >> num >> pri)
{
...
Further, you should consider only having one read loop. Just read the data in after you've tested that the file is opened successfully. Later, when you get the users choice for 1 or 2, you can then just have the loops to output the data.
Consider making a struct/class that has both number and price data members and then a vector of this object instead of individual number/price vectors:
Code:
struct product
{
int number;
double price;
// Constructor
product(int num, double pri) : number(num), price(pri) {}
};
int main()
{
vector<product> products;
...
while(inData >> num >> pri)
{
// Create and push back an initialized product object onto vector
products.push_back(product(num,pri));
}
If you want you can even overload the stream extraction operator to make things fancy for your new product struct/class objects:
Code:
ostream& operator>>(ostream& os, product& pr)
{
return os >> pr.number >> pr.price;
}
int main()
{
vector<product> products;
product temp_product;
...
while(inData >> temp_product)
{
products.push_back(temp_product);
}
Your search code is all wrong... you must get a number from the user, then loop through all the items until you find the correct one and then display the results if you find the correct one. Currently you are testing the product number entered by the user against the first number stored in the vector and if they match, then you output everything. You want something more like:
Code:
cout << "Enter product number :\n";
cin >> product_number;
for( loop through the number vector)
if( got a match )
display number/price
if( our loop variable reached the end of the vector )
display "No match found"