cin.get() Troubles

This is a discussion on cin.get() Troubles within the C++ Programming forums, part of the General Programming Boards category; Add a function Display_Greeting() to prb05-1.cpp that displays the purpose of the program. After displaying the greeting, the function should ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    48

    Angry cin.get() Troubles

    Add a function Display_Greeting() to prb05-1.cpp that displays the purpose of the program. After displaying the greeting, the function should pause and display

    Press any key to continue



    (Hint: use cin.get().) The program should then proceed as in the text. Use the function in the program. Recompile, execute, and test the resulting program.

    This bastard of a question has really confused me, is this even possible?, to achieve that results" Press any key to continue" using cin.get() if somebody can please respond it would be
    very much appreciated

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Output the message "Press any key to continue" using std::cout. Then, put your std::cin.get() call after that. What's so difficult about that?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    48
    you still have to press enter after pressing any key, i was looking for an answer where you press any key to achieve the same result as pressing return

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    For that, you'd need to use non-standard functions. For Windows, you might use:
    Code:
    #include <cstdlib>
    std::system("pause");
    For Dev-C++, you might be able to get getche() from conio.h to work.

    There's an FAQ about this; I suggest you read it. http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    With non-standard functions, it's possible, yes.
    But my question is: why do you need it and where do you need it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    "Why do you need it?" It looks like an assignment to me.

    An assignment based on this book.
    http://books.google.ca/books?id=_q-6...l=en#PPA204,M1
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    48
    so its impossible ?, also the link didn't work, i would like to examine it, yeah that evil evil book

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    No, it's not impossible. You were told 2 different solutions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Link didn't work, huh? You have to click on it, not copy it.

    It brings you to page 204 of C++ for Business Programmers, which I'm pretty sure is the program you're supposed to be modifying.

    Sure it's possible. Just not with standard C++. You have to use non-standard functions (or non-standard programs, with system()). Read my pos #4 again.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    Quote Originally Posted by dwks View Post
    Link didn't work, huh? You have to click on it, not copy it.
    Didn't work the first time, but the second time... now it worked!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    In case you're wondering how I found it: stick "prb05-1.cpp" into Google.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    48
    what i mean is that it is impossible to acheive this result using only cin.get() by itself, see i am not familiar with the "pause" key

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    That is correct. You can't use cin.get() by itself to get unbuffered keyboard input. (That is, instantaneous input, as soon as you press a key, without waiting for enter to be pressed.)

    You might be able to do it by unbuffering stdin first, but that would just be a hassle. You'd have to use non-standard functions to do that anyway.

    Since they say to use cin.get(), I guess they're not concerned about having to press enter. Most of the time, it doesn't matter. Though I'd probably change the message to "Press enter to continue" in that case . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    May 2008
    Posts
    48
    this fellow Molluzo , is not the first time he makes mistakes with input output stream, earlier much earlier one question asked you to retrieve wit just the cin object letter from input stream , i emailed him and he said it was a mistake, so this question must also be a mistake as he hasnt introduced that into the book yet the 'pause' thing

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I doubt the book mentions system("pause") in any great depth. Or at least, it shouldn't. Calling system("pause") is unportable (you have to have the "pause" program, which only Windows systems usually do). It's also insecure (make a virus called "pause", and it will be executed instead!). It's also very slow and inefficient, because a whole new process has to be spawned just to execute the pause program.

    In case it wasn't clear: the system() function executes a command as if you had typed that string on the command line. In this case, it's executing the program "pause" (unless maybe pause is a shell builtin, but whatever).

    "With just the cin object letter from input stream"? Could you elaborate?
    Last edited by dwks; 06-04-2008 at 12:56 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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

Similar Threads

  1. cin.get() curiosity
    By lilrayray in forum C++ Programming
    Replies: 3
    Last Post: 09-03-2006, 10:07 PM
  2. cin.get() problem
    By Cilius in forum C++ Programming
    Replies: 20
    Last Post: 07-28-2005, 05:32 PM
  3. cin.get();
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2005, 07:51 AM
  4. Problems using while loop with cin.get()
    By Arooj in forum C++ Programming
    Replies: 4
    Last Post: 11-28-2004, 12:58 AM
  5. curiosity about cin.get() and cin.getline()
    By ssjnamek in forum C++ Programming
    Replies: 18
    Last Post: 11-30-2003, 12:26 AM

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