// This program searches a file for a given number. // The program will prompt the user to enter a number. It will then // read the file numbers.dat, and search for the number entered by the // user. At the end of the search the program will print a message telling // whether the number was found. #include <iostream.h> #include <iomanip.h> #include <fstream.h> #include <csstring.h> // only needed in Turbo C++ int main() { int number; int searchnum; int i; bool found = false; ifstream numfile; // prompt the user to enter the number they are looking for cout << "Enter the number to search for: "; cin >> searchnum; // open the input file numfile.open("numbers.dat"); // read in first number from the file numfile >> number; // read in the numbers, check for the user's number while (numfile) { if (number == searchnum) found = true; numfile >> number; } // print message telling whether number was found if (found == true) cout << "Your number (" << searchnum << ") was found in the file\n"; else cout << "Your number (" << searchnum << ") was not found in the file\n"; }
33 55 99 11 88 -12 44 22 77 5 66
Enter the number to search for: 99 Your number (99) was found in the file
Enter the number to search for: 125 Your number (125) was not found in the file
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2002