Thread: getch and EOF

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715

    getch and EOF

    I'm testing this function:

    Code:
    string ucitaj (int n)
    {
    	string str;
    	int i = 0;
    	int ch;
    
    	while ( ((ch = getch()) != EOF))
    	{
    		if ((ch == 0) && (i > 0))
    		{
    			getch();
    			i--;
    		}
    		if (isdigit(ch))
    		{
    			cout<<static_cast < char > (ch);
    			str += ch;
    			i++;
    		}
    		if ( (ch == '\b') && (i > 0))
    		{
    			cout<<"\b \b";
    			i--;
    		}
    		if ( i == n)
    		{
    			break;
    		}
    	}
    	return str;
    }
    I'm entering characters from keyboard and I noticed that Ctrl-Z (EOF under windows) simply doesn't work, which I expected because of that line with ch == 0.
    My question is how to enable that loop quit after user press Ctrl-Z?
    Thanks
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Micko
    My question is how to enable that loop quit after user press Ctrl-Z?
    Maybe this way.
    Code:
    while ( ((ch = getch()) != 26))
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Masking input
    By bradszy in forum C++ Programming
    Replies: 15
    Last Post: 02-23-2008, 10:39 AM
  2. Setting position indicators in Streams
    By @nthony in forum C Programming
    Replies: 15
    Last Post: 07-04-2006, 05:15 AM
  3. function to replace system("pause");
    By willc0de4food in forum C Programming
    Replies: 29
    Last Post: 09-08-2005, 12:52 PM
  4. problem on reverce polish notation
    By dionys in forum C++ Programming
    Replies: 0
    Last Post: 05-14-2004, 01:30 PM
  5. verify EOF
    By threahdead in forum C Programming
    Replies: 1
    Last Post: 10-11-2002, 07:49 AM