Results 1 to 5 of 5

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

  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

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Try this:

    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.ignore();
    		cin.get(c);
    		}
    	return (c);
    }
    Edit: Nevermind, I still see problems.
    Last edited by The Hobo; Feb 3rd, 2003 at 05:01 PM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    What exactly does cin.ignore() do? I know that cin.ignore(10, '\n') deletes 10 characters from the input buffer or untill it finds a \n
    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

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by McCain
    What exactly does cin.ignore() do? I know that cin.ignore(10, '\n') deletes 10 characters from the input buffer or untill it finds a \n
    cin.ignore() will just clear out the first character in the buffer. I realize now that if someone enters something more than one character, the buffer is going to be larger than that and mess up your program.

    This is how I'd do it:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    string control();
    
    void main(void) {
        string ans; 
    
        ans = control();
        cout << ans << endl;
    }
    
    string control() {
        string c;
    
        cout << "Y or N: ";
        getline(cin, c);
    
        while (c != "Y" && c != "N" && c != "y" && c != "n") {
            cout << "Y or N: ";
            //cin.ignore(); //if you're using MSVC++, you may need this line...
            getline(cin, c);
        }
    
        return (c);
    }
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    I know I can use getline, I can also use cin >> but the exersice was to use cin.ignore with cin.get (I'm doing the exercises in a book I've got...)
    Thanks anyway for taking the time to help me
    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