1. Check out OCAU's review of the SpaceX Starlink satellite internet service!
    Dismiss Notice

Damn C++ iostream

Discussion in 'Programming & Software Development' started by tvjames, Aug 18, 2002.

  1. tvjames

    tvjames Member

    Joined:
    Jan 8, 2002
    Messages:
    46
    Location:
    Brisbane, QLD
    Basically i need to use
    std::getline(std::cin, reason)

    reason is a std::string
    and well i would have expected this to work perfectly except for the fact that the program does not wait for user input and just goes on to the next prompt in the program ... this is quite frustrating

    yet it works as expected if used from main() ... but where i am using it from is deep inside a class.

    Thanks :)
     
  2. Geo

    Geo Member

    Joined:
    Jun 27, 2001
    Messages:
    2,368
    Location:
    Adelaide
    Have you tried cin.readline(reason) ?

    I think you should also look at your class structure, its generally not good practice for something deep inside a class to be getting user input, it reduces the reusability of the code. Perhaps you should get the user input somewhere else and pass it as a parameter to the class that needs it.
     
  3. Boney

    Boney Member

    Joined:
    Jun 27, 2001
    Messages:
    903
    Location:
    Westside, Ippaswitch
    You probably need to clear your input buffer cin.clear()? before attempting to read user input.
     
  4. OP
    OP
    tvjames

    tvjames Member

    Joined:
    Jan 8, 2002
    Messages:
    46
    Location:
    Brisbane, QLD
    Thanks

    BUT

    i have tried as a function (not in class, local to the file) and that produces the same output, also
    getline(cin, reason);
    and
    cin.getline(buff, buff_size);

    (buff is a char[buff_size])
    both cause the same problem in the class and also in a function ... yet if i put the same code in main it works fine :confused:
     
  5. almghty

    almghty Member

    Joined:
    Jun 28, 2001
    Messages:
    1,172
    You need to flush the buffer for your class.

    cin.ignore();


    or consequently munch through each character till you get to the newline using cin.get()

    but cin.ignore() should fix it.
     
  6. ocean

    ocean Member

    Joined:
    Jun 26, 2001
    Messages:
    259
    Location:
    Melbourne
    *looks at people above him funny*

    you should be using

    Code:
    std::string a;
    std::getline(std::cin, a);
    
    to ignore all characters upto and including the next '\n' on the input stream do

    Code:
    std::cin.ignore(std::numeric_limits<std::streamsize>::infinity(), '\n');
    
    or, more readably, but not object oriented

    Code:
    while(cin.get() != '\n');
    // or
    cin.ignore(10000, '\n');
    
    note: the above examples will *wait* for a \n if none exists on the stream.

    feel free to post your code so we can look and debug/help :)
     
  7. OP
    OP
    tvjames

    tvjames Member

    Joined:
    Jan 8, 2002
    Messages:
    46
    Location:
    Brisbane, QLD
    thanks everyone .. but the problem is solved .. and btw i didnt was to ignore the input .. i wanted to get it .. but thanks anyway

    also as for the one 2 posts above, get also fails the only working one was read() .. but all fixed now

    std::cin.sync()

    fixed it all :)

    thanks all
     

Share This Page

Advertisement: