Write a program that prompts the user to enter an item#, the program should determine if the item is in the file and print the price of the corresponding item. If the item is not in the file an error message should be printed.
int item_no = 0; // we'll use int because it will automatically convert
cout << "Enter an item#" << endl;
cin >> item_no;
// search for the item number in the file
ifstream inFile("filename");
while (inFile)
{
// get the item number and cost
int fileItemNumber = 0;
inFile >> fileItemNumber;
// get the cost
double itemCost = 0;
inFile >> itemCost;
//Check if they're equal, if so we're done
if(fileItemNumber == item_no)
{
cout << "Cost: " << itemCost << endl;
return 0; // exit the program
}
// if we get here then the item is not in the file
cout << "Error: Item not found in file: " << "file name" << endl;
return 0;
}
And I already responded with some pseudocode as well. By double posting it's gonna be hard for people to track the progress of your problem because answers are going to be split between the two threads.