trying to print a line from a text file

This is a discussion on trying to print a line from a text file within the C++ Programming forums, part of the General Programming Boards category; I've got an apparent logic problem that I can't seem to solve. This program should open a text file and ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    28

    trying to print a line from a text file

    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.

    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;
    
    }
    sample text file called terminals.txt:
    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
    it prints out the last terminal - the "terminal 99" and loops. Any ideas?

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    Construct loops with actual conditions in them?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple C App to add line breaks to a text file
    By sari in forum C Programming
    Replies: 2
    Last Post: 07-09-2009, 10:45 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21