cin.get()

This is a discussion on cin.get() within the C++ Programming forums, part of the General Programming Boards category; Hi, 1. if you easily get annoyed at stupid questions, don't read on 2. I'm using dev-c++ + mingw. I ...

  1. #1
    Enthusiastic Beginner balazsbotond's Avatar
    Join Date
    Mar 2007
    Location
    Érd, Hungary
    Posts
    20

    cin.get()

    Hi,

    1. if you easily get annoyed at stupid questions, don't read on

    2. I'm using dev-c++ + mingw. I was trying to keep the windows of my console apps open using cin.get(); but in most of the cases it didn't work. Sometimes it did, but I don't really see why (it's _not_ because of some silly syntax error or something, i'm sure). I'm using system("pause >nul"); instead, and it works fine, though I know it isn't very elegant.

    3. I've written a small program to demonstrate this (inspired by PublicAutopsy, hehe ):
    Code:
       #include <iostream>
       using namespace std;
       
       int main () {
          int password;
          do {
             cout << "Enter password: ";
             cin >> password;
             cout << "\n";
          } while (password!=12345);
          cout << "Password OK.";
          cin.get();
          return 0;
       }
    I don't know what's wrong with it, it works fine except the cin.get line. Any guesses?

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    after operator >> finished working - it stopeped on the first white space...
    cin.get reads it from the stream so no waiting occures
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    When you enter your password, you validate your input by pressing enter (which represents the '\n' character). When you read the password with cin, cin stops before the line return which stays in the input stream. cin.get() reads that character and moves on.

    You can add cin.ignore() to your code, to ignore a certain number of characters in the input stream.

  4. #4
    Enthusiastic Beginner balazsbotond's Avatar
    Join Date
    Mar 2007
    Location
    Érd, Hungary
    Posts
    20
    Thank you, guys, this worked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.get(); not working with my switch statement
    By tenor_jazz13 in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2008, 10:33 PM
  2. cin.get() problem
    By Cilius in forum C++ Programming
    Replies: 20
    Last Post: 07-28-2005, 06:32 PM
  3. Confused about cin.get(); and classes.
    By RaccoonKing in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2005, 12:44 PM
  4. cin.get();
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2005, 08:51 AM
  5. curiosity about cin.get() and cin.getline()
    By ssjnamek in forum C++ Programming
    Replies: 18
    Last Post: 11-30-2003, 01:26 AM

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