Add message to cin.get(); statement?

This is a discussion on Add message to cin.get(); statement? within the C++ Programming forums, part of the General Programming Boards category; So in my codes, I figured out you can do Code: cin.get(); instead of what I always do, which is ...

  1. #1
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76

    Add message to cin.get(); statement?

    So in my codes, I figured out you can do
    Code:
    cin.get();
    instead of what I always do, which is
    Code:
    system ("pause");
    So I'm using cin.get(); now. Not sure why, but I hear it's better.

    ANYWAY, I was wondering if there's any way to add a fancy little message on the end like in system ("pause") when it says "Press any key to continue."

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,620
    std::cout << "Press any key to continue.";
    std::cin.get();

    As to why it's better... it's platform independent, and using system is typically a big security risk.
    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.

  3. #3
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    Hmm. I know there was mention of a getcha() being platform independent, but that one didn't work. I guess this is in the same family.

    Thanks!

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,620
    getcha is not a standard function. Thus, some compilers will have it, some not.
    system is perfectly standard, but it relies on executing external executables. Pause only exists on Windows, hence not portable.
    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.

  5. #5
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    Now two other quick things:
    1. How can I make it to press any key to go back to a defined point in the program?
    2. How can I make it wait a defined amount of time, THEN go back to a defined point? Like say "Wait 1 second, then go here. "

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,620
    1) You use some logic and a loop. Hint: make a flowchart.
    2) This will use some OS dependent APIs. On Windows, there is a function called Sleep which will basically sleep for n milliseconds before continuing.
    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.

  7. #7
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    Never mind, got #1. I cheated and used goto though. :/
    Code:
    cout << "Press any key to refresh. ";
      cin.get();
      goto start;
    #2 anyone?

  8. #8
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    Quote Originally Posted by Elysia View Post
    2) This will use some OS dependent APIs. On Windows, there is a function called Sleep which will basically sleep for n milliseconds before continuing.
    Just sleep(x);?
    x meaning a number of milliseconds.
    [Edit- it says sleep is an undeclared function when I tried my theory of how to use it. To Google!]
    [Edit 2- This is helpful since I use Dev C++, but still no luck- http://www.dreamincode.net/forums/to...command-in-c/]
    Last edited by muffinman8641; 03-01-2011 at 07:59 PM.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,620
    Quote Originally Posted by muffinman8641 View Post
    Never mind, got #1. I cheated and used goto though. :/
    Code:
    cout << "Press any key to refresh. ";
      cin.get();
      goto start;
    #2 anyone?
    Don't use goto. Use a loop. Ie:
    Code:
    while (refresh)
    {
    //...
    cout << "Press any key to refresh. ";
    cin.get();
    }
    Quote Originally Posted by muffinman8641 View Post
    Just sleep(x);?
    x meaning a number of milliseconds./
    No, like so
    Code:
    #include <Windows.h> // Don't forget!!
    Sleep(x);
    Where x is the number of milliseconds.
    (C++ is case sensitive; don't forget!)
    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.

  10. #10
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    But I used the windows.h header and Sleep (1000); but it says "Permission denied: Id returned 1 exit status."
    My goodness this is confusing.
    I have to go sleep I'll check back tomorrow, thanks for all the help Elysia.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,620
    Erm... permissions trouble? Make sure you have admin rights or store your project in a location where you have write privileges (eg my documents)?
    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.

  12. #12
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    No this is on my compiler on my home pc! That's what confuses me, I'm just using Dev C++ and the compiler is giving me crap.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,620
    Same thing. If you have UAC enabled, this is a common trouble.
    Oh and Dev-C++ is bad. Suggest you check for a new IDE: http://sourceforge.net/apps/mediawik....php?title=IDE
    (Dev-C++ is old and no longer maintained.)
    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.

  14. #14
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    OK thanks for the info.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why won't this simple statement work?
    By nick753 in forum C++ Programming
    Replies: 10
    Last Post: 10-18-2010, 11:20 PM
  2. bizarre print statement glitch
    By spongefreddie in forum C Programming
    Replies: 4
    Last Post: 09-23-2010, 10:39 AM
  3. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  4. cin.get(); not working with my switch statement
    By tenor_jazz13 in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2008, 09:33 PM
  5. enum switchcase and windows message q
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 11-27-2006, 12:16 PM

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