I've got an apparent logic problem that I can't seem to solve. This program should open a text file and search for a terminal and then print the entire line.
It does not work as it should and I can't seem to figure out why.
sample text file called terminals.txt:Code:/*********************************** * Pre-Compiler Directives * ***********************************/ #include <iostream> #include <fstream> #include <sstream> #include <cassert> #include <string> using namespace std; /*********************************** * Global Variables * ***********************************/ int terminalNumber; string inputFileName = "terminals.txt"; // input file name string lineOfText; string stringTerminal; ifstream inStream; string number; string output; string input; /*********************************** * Main * ***********************************/ int main() { cout << "This program takes a terminal number\n" "and prints out the terminal associated\n" "with that number.\n" "Enter a terminal to retrieve 1-99 or 0 to quit --> "; cin >> terminalNumber; stringstream str; str << "terminal" << terminalNumber << endl; string stringTerminal = str.str(); // The final string combined for(;;) { if (terminalNumber <= 0) break; inStream.open(inputFileName.data()); // opens the file to read from assert( inStream.is_open() ); for (;;) { inStream >> input; if (input == stringTerminal) break; getline(inStream, lineOfText); output = lineOfText; } if( inStream.eof() ) break; } cout << "Your terminal information is:\n" << output << endl; cout << "Enter another terminal number 1-100 or 0 to quit: "; cin >> terminalNumber; } inStream.close(); // closes the file // OUTPUT cout << "No more terminals for you!\n" "Have a good day"; cin.ignore(); return 0; }
it prints out the last terminal - the "terminal 99" and loops. Any ideas?Code:terminal1 4100 100 a 06012006 terminal2 4101 100 a 06012006 terminal3 4102 100 a 06012006 terminal4 4103 100 a 06012006 terminal5 4104 100 a 06012006 terminal99 4107 100 a 06102006



LinkBack URL
About LinkBacks


