Results 1 to 5 of 5

Thread: [Resolved (Sort of...)] cin.ignore() / cin.get()

Threaded View

  1. #1

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802

    [Resolved (Sort of...)] cin.ignore() / cin.get()

    Why do I have to hit Return an extra time to exit this program?
    Code:
    //char.cpp
    
    #include <iostream>
    using namespace std;
    
    char control();
    
    void main(void)
    {
    	char ans; 
    
    	ans = control();
    	cout << ans << endl;
    	cout << cin.get() << endl;
    }
    
    char control() 
    {
    	char c;
    	cout << "Y or N: ";
    	cin.get(c);
    	while (c!= 'Y' && c!= 'N' && c!='y' && c!='n')
    		{
    		cout << "Y or N: "; 
    		cin.get(c);
    		cin.ignore(10, '\n');
    		}
    	cin.ignore(10,'\n');
    	return (c);
    }
    It seems to me that the program stops at cin.get() if there's nothing in the input buffer. Why does it do this?
    Last edited by McCain; Feb 17th, 2003 at 07:04 PM.
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width