Thread: Why does my program require a final hit of return key to exit?

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    54

    Why does my program require a final hit of return key to exit?

    OS: Windows 2000. Compiler: Visual C++ 6.0.

    Basically, I have a simple program in one function to output a typed in line to a file. I call the output function from the Main function. However, the result is in the attached screenshot. In order to end the program, I would hit enter from the current (screenshot) point, and the window would close.

    Two questions:

    1. Why is there a new line after the first line is printed?
    2. Why is "Line to log: " displayed again after I type exit?

    Here is the code:

    Code:
    int main(void)
    {
    
    string currentline;
    
    	while (1 == 1)
    	{
    		cout <<"Line to log: ";
    		getline (cin, currentline); //stores the whole input line into currentline, with a return as end of line
    		if (currentline != "exit" && currentline != "")
    			output (currentline);
    		else if (currentline == "exit")
    			return 0;
    
    	}
    	
    	return 0;
    }
    Thanks guys.
    -Zack

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    What does output() do?

    And getline() is broken in VC++ 6.0: http://www.dinkumware.com/vc_fixes.html

    gg

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    54
    Quote Originally Posted by Codeplug
    What does output() do?

    And getline() is broken in VC++ 6.0: http://www.dinkumware.com/vc_fixes.html

    gg
    Thanks bud.

    Here's the output function. All it really does is take the current time, parse the line of text, and output all of that to a text file:

    Code:
    void output (string currentline)
    {
    	
    	ofstream outfile("sal.log", ios::app); //opens file for append. if file does not exist, creates it.
    
    	//begin timestamp
    	time_t now;
    	struct tm *tm_now;
    	char buff[BUFSIZ];
    
    	now = time ( NULL );
    	tm_now = localtime ( &now );
    
    	strftime ( buff, sizeof buff, "%A, %x %X %p ", tm_now );
    	//endtimestamp
    
    	stringstream tokenizer(currentline); //parses currentline in preparation for writing
    
    	string token; //the token strings, separated by space
    
    	tokenizer >> token;
    
    	//outfile <<"^G";
    
    	outfile<< ( "%s", buff ); //outputs timestamp
    
    	int counter = 0; //counter to see if username is the token
    
    	
    
    	while (tokenizer >> token) //writes as many tokens as there are
    	{
    		if (counter == 0) //if counter is 0, then it's on the username. username <= 8 characters
    		{
    			if (token.length() <= 8)
    			{
    				for(int c = 0; c < token.length(); c++) //outputs username
    				{
    					outfile << token[c];
    				}
    				for(int i = token.length(); i < 8; i++) //outputs padding, up to 8 characters
    				{
    					outfile << " ";
    				}
    			}
    			else
    			{
    				for(int i = 0; i < 8; i++) //if username > 8 characters, outputs first 8
    				{
    					outfile << token[i];
    				}
    			}
    			outfile << " "; //outputs space after username
    
    		}
    
    		else
    		{
    			for (int i = 0; i < token.length(); i++) //checks for " and does not output them
    			{
    				if (token[i] != '"')
    				{
    					outfile <<token[i];
    				}
    			}
    
    			outfile << " "; //writes a space at the end
    		}
    
    		counter++;
    	}
    
    	outfile <<endl; //goes to new line at the end of write
    
    	outfile.close();
    }
    -Zack

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You just need to apply the getline fixes in the link I posted.
    Or use a better STL implementation like STLport.

    gg

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Don't use STL with VC++6 unless you know very well what you're doing.... You'll get lots of problems.. I tried it.. It didn't worked.
    Why do these libs always have lots of stupid macro's ?? all problems come from there...

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Because its MS?

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    54
    I tried that and it didn't work. Any other suggestions?
    -Zack

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You didn't apply the patch [correctly] to getline() in <string> or you are linking dynamically with the CRT (which contains the bug in compiled form).

    To link staticly with the CRT: Project -> Settings -> C/C++ tab -> Code Generation category -> Use run-time library: Single-Threaded*

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM