Thread: cin.get()

  1. #1
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49

    Question cin.get()

    I use cin.get() for the program not to exit unles the user presses the return key, but it does not work all the time, sometimes the program exits without the user pressing the return key. Why is that?

    Thank you,
    Amy
    It ain't illegal until you get caught..

  2. #2
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    cin.get(); may be reading something already in the input buffer, not allowing the user to press enter. Try

    Code:
    system("PAUSE");
    instead. Or you could just add another cin.get(); after the first.

  3. #3
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    Thank You
    It ain't illegal until you get caught..

  4. #4
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Many/most compilers have a Debug option for executing your compiled program instead of just selecting Run. This will provide you with an opportunity to also see the Exit status of your program upon completion, and a prompt to press enter. (I'm assuming that you are having the problem that your program opens a window then closes before you can see the results)

  5. #5
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    Quote Originally Posted by Scribbler
    Many/most compilers have a Debug option for executing your compiled program instead of just selecting Run. This will provide you with an opportunity to also see the Exit status of your program upon completion, and a prompt to press enter. (I'm assuming that you are having the problem that your program opens a window then closes before you can see the results)
    Yes I do debug it to see it, but the problem is not about me seeing the result program, I do not want the exe file to exit itself once it is done with all operations, and I sometimes do not want to add an option at the end of my program like "If you want to quit press *", so I was using cin.get() expecting the user to hit the return key before the program ends, but why isn't it working all the time?
    It ain't illegal until you get caught..

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    avoid using system calls when possible - check out my tip here (you probably used cin somewhere in your code): http://www.cprogramming.com/tips/sho...ount=30&page=0
    Last edited by major_small; 02-07-2005 at 05:09 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    Quote Originally Posted by major_small
    avoid using system calls when possible - check out my tip here (you probably used cin somewhere in your code): http://www.cprogramming.com/tips/sho...ount=30&page=0
    Aha, now I got the terminating character thing, thanks for you too major_small..
    It ain't illegal until you get caught..

  8. #8
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    What's wrong with using system calls?

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    security issues mostly. say I get onto your computer and replace the pause.exe with a pause.exe that I wrote. now every program you wrote runs my program, which will be completely transparent to you because I have it spawn off a keylogger and then pause your thread so you never knew anything ever went wrong.

    now I have your credit card, social security number, passwords, girlfriend's name, anything I could possibly want from you.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    And of course there's portability. pause and cls and many others are meaningless outside of windows.

  11. #11
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Yes, I realized that already.

  12. #12
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    How else can you clear the screen without system("cls")?

  13. #13
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Now doesn't this sound like it would be a Fequently Asked Question to you?
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  14. #14
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Junior89
    How else can you clear the screen without system("cls")?
    try avoiding having to clear the screen


    the answer: there is no standard way. the way I do it is like this:
    Code:
    void clearScreen() { system("clear"); }
    
    int main(){...}
    that way with one quick edit to your code, you can switch that to work in windows...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  15. #15
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    If you want to clear the screen and avoid using system calls, you can include the conio.h libraries for windows, or the ncurses libraries for *nix.

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, 05:32 PM
  3. Confused about cin.get(); and classes.
    By RaccoonKing in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2005, 11:44 AM
  4. cin.get();
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2005, 07: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