getch and EOF

This is a discussion on getch and EOF within the C++ Programming forums, part of the General Programming Boards category; I'm testing this function: Code: string ucitaj (int n) { string str; int i = 0; int ch; while ( ...

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

    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,006
    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, 06:15 AM
  3. function to replace system("pause");
    By willc0de4food in forum C Programming
    Replies: 29
    Last Post: 09-08-2005, 01:52 PM
  4. problem on reverce polish notation
    By dionys in forum C++ Programming
    Replies: 0
    Last Post: 05-14-2004, 02:30 PM
  5. verify EOF
    By threahdead in forum C Programming
    Replies: 1
    Last Post: 10-11-2002, 08:49 AM

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