Quote:
Assume that a file named HWK5.txt has a single name (last name only -- no first name or middle initial) and a parking sticker number (6 digits) on each line of the file. It is unknown how many names are on the file. (You will need to make up a test file to use.) Write a program that will ask the user for a parking sticker number (000000 to stop) and then search the file for that number. When it finds the number in the file, it will write out "This parking sticker belongs to " followed by the corresponding name on the file, or if the sticker number is not found on the file, the message "This parking sticker is not on file. Try again." should be displayed. The program will then ask for another sticker number and repeat until a sticker number of 000000 (or just plain zero) is entered. To perform the search, the parking sticker number should be sent to a function called findName, which you will write. The function findName should open the file HWK5.txt, search it for the given sticker number, and return the corresponding name to the calling program. Before it returns, the function should close the file HWK5.txt so that it will be ready to be used for the next search. If the sticker number is not found on the file, the function should return a value of "NOT FOUND" to the calling program. The function should NOT display anything on the screen; the main function should handle this task, as well as prompting for the next sticker number.
Quote:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string lastName;
double number, target, parkingSticker;
const double SENTINEL = 000000;
cout << "Please enter a parking sticker number (enter 000000 to exit): " << endl;
cin >> target;
while (target != SENTINEL)
{
ifstream input("HWK5.txt");
if (input.is_open())
{
while (! input.eof())
{
input >> lastName >> parkingSticker;
if (target == parkingSticker)
cout << "The parking sticker number belongs to " << lastName << endl;
else
cout << "This parking sticker is not on file. Try again." << endl;
}
}
input.close();
}
system("pause");
return 0;
}