simple cin question

This is a discussion on simple cin question within the C++ Programming forums, part of the General Programming Boards category; You ran into the exact problem that Daved described. Change: Code: cout << "\nEnter B for book or R for ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    You ran into the exact problem that Daved described.

    Change:
    Code:
    cout << "\nEnter B for book or R for recording: " << endl;
    cin >> holdingChoice;
    to:
    Code:
    cout << "\nEnter B for book or R for recording: " << endl;
    cin >> holdingChoice;
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    You should #include <limits> too. The reason for this is that your cin >> holdingChoice leaves a newline in the buffer, and you need to ignore this otherwise your cin.getline() call will read until this newline, instead of the next.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    May 2007
    Posts
    88
    The problem is that your first call to cin leaves a newline on the buffer. getline reads this and returns. A call to ignore should do the trick
    Code:
    std::ignore('\n', 10);
    std::cin.getline(buff, 100);

  3. #18
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    Quote Originally Posted by Daved View Post
    Are you using cin >> for other data before you call getline?
    and yes I'm using cin >> on a char input above ... see the code....thanks.

  4. #19
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    Quote Originally Posted by UMR_Student View Post
    The problem is that your first call to cin leaves a newline on the buffer. getline reads this and returns. A call to ignore should do the trick
    Code:
    std::ignore('\n', 10);
    std::cin.getline(buff, 100);
    sorry so many "noob" dumb questions but it says that ignore is not a member of std....

  5. #20
    Registered User
    Join Date
    May 2007
    Posts
    88
    You're right, it's not. That's a mistype, sorry. Use laserlight's solution anyway, it's better.

  6. #21
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    Nevermind I just need to do std::cin.ignore('\n', 10);

    Is that need after I use cin.getline() as well or just if I happen to use cin. Meaning will using cin.getline leave a '\n' character in the buffer??

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    UMR_Student made another typographical error, I am afraid. The arguments are swapped, in that the number of characters to ignore is the first argument, not the second.

    Is that need after I use cin.getline() as well or just if I happen to use cin. Meaning will using cin.getline leave a '\n' character in the buffer?
    All forms of getline() for input streams will remove the newline sequence from the buffer, hence there is no need to use ignore() with them.
    Last edited by laserlight; 05-09-2007 at 01:01 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Registered User
    Join Date
    May 2007
    Posts
    88
    Yeah, that was all kinds of wrong. Haste makes waste, as they say. Thanks for the corrections anyway.

  9. #24
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Now that you've fixed your problem with ignore(), go back to using string instead of character arrays.

  10. #25
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    Quote Originally Posted by Daved View Post
    Now that you've fixed your problem with ignore(), go back to using string instead of character arrays.
    haha....i agree except this is the parameters the professor set...so I'm screwed....he also appauls at int main(...)...he prefers us to use void main(void)...go figure...I can't wait to get out in the real world that way I can be thoroughly frustrated but at least I'll be doing it correctly....

  11. #26
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by verbity View Post
    haha....i agree except this is the parameters the professor set...so I'm screwed....he also appauls at int main(...)...he prefers us to use void main(void)...go figure...I can't wait to get out in the real world that way I can be thoroughly frustrated but at least I'll be doing it correctly....
    If he is appauled at the standard and fights against it, why is he teaching C++?

    Sorry, rhetorical question I guess.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. cin question
    By Zico in forum C++ Programming
    Replies: 10
    Last Post: 04-12-2002, 05:25 AM
  4. cin question....
    By Alien_Freak in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2002, 10:29 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

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