/* testarr14.cpp CIS 150 06/23/2005 David Klick This program demonstrates how to search for information in parallel arrays, with the array data coming from a data file. */ #include #include #include using std::cout; using std::cin; using std::setw; using std::left; using std::ifstream; const int ARRAY_SIZE = 100; // must leave more space than needed int find(int id[], int length, int idnum); int main(void) { int n, pos; int numrecs = 0; // number of array elements being used // declare arrays of names and IDs char names[ARRAY_SIZE][40]; int id[ARRAY_SIZE]; // open file containing name and ID data ifstream infile("arraydata.txt"); if (!infile) { cout << "Error: Could not open input file\n"; return 1; } // load arrays from file infile >> id[0]; while (infile && numrecs> id[numrecs]; } } infile.close(); // loop until user chooses to exit do { // find out which ID # the user wants to display cout << "Find which user id? (-999 to exit) "; cin >> n; // display desired name if user didn't choose to exit if (n != -999) { pos = find(id, ARRAY_SIZE, n); if (pos == -1) { cout << "Error: ID number not found\n"; } else { // display names associated with ID # cout << setw(3) << n << ": " << names[pos] << '\n'; } } } while (n != -999); return 0; } int find(int id[], int length, int idnum) { int i; for (i=0; i