Thread: cin input error

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    13

    cin input error

    Hi, I am currently working on a console app and I have run into one error that throws off the feel of my program. My program is kind of like the skeleton for a miniature compiler with the feel of python. The problem I am having is in generataing the next line. If the user entered the code for that line, and then pressed enter to terminate the line, everything works fine. The problem is when they don't enter any values and press enter, the program keeps waiting until a value is entered. Does anyone know how I can determine if a user pressed enter at all in a console app?? Thanks in advance.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    getline?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    3
    cin.get()
    ?
    i probably don't understand the question..

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    std::getline(stream, target, delimiter);
    so basicly
    std::getline(std::cin, var, '\n');

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You are probably using cin >> to read in values. As has been recommended you should use getline to read in the line into a string.

    The important next step is to then put that entire string into an istringstream. Then use that istringstream like you were using cin before.

    With the istringstream if the user didn't input any value and you try to read from it, it will set a failbit because it will be empty. If you just used cin then it would wait until the user typed in non-whitespace like you are experiencing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  4. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM