system("pause");, cin.get(); while (!kbhit()); ... - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

system("pause");, cin.get(); while (!kbhit()); ...

StephenCStephenC Posts: 7Member
Okay. I've been doing some searching around and I don't understand why I'm having issues using cin.get().

I realize that the issue of portability is at hand when using the localized system("pause"), thus, I'd like to rid that. I've been using DevC++, on my work machine since I'm unable to install VC++, and through my journey of Goooooooogle, I've noticed that some people apparently have an issue with cin.get() while using it (DevC++).

Okay, so assume each of the following (specific code examples with everything else intentionally missing but including iostream and conio.h when needed):

1 -

{
cout << "Thanks for playing!" << endl;
system("pause");
}

2 -

{
cout << "Thanks for playing!" << endl;
cin.get();
}

3 -

{
cout << "Thanks for playing!" << endl;
while (!kbhit());
}

1 and 3 each work great with 1 being the obvious instance I'm trying to get away from but cin.get(); is just completely skipped for me UNLESS I apply a cin.ignore() just prior to it:

{
cout << "Thanks for playing!" << endl;
cin.ignore()
cin.get()
}

My examples provided, when placed within the full context of my sample application, are coming after 2 cin's. (Person enters an integer (int integer1;), then another (int integer2;), then the results are added and placed into int sum;.

You know what... I just commented out all prior cin's and using ONLY cin.get() worked great. Does cin always retain the very last thing the user inputs? I guess that would make sense but I assumed initially that cin dumped the user's input into the variable and automatically "emptied" itself but I guess not... so then, I guess cin.get() is essentially saying "I want you to get input from the user and if the user has already given me some input somewhere else, just use that," thus, cin.get() appears as though it's being skipped unless you clean cin beforehand so that it waits for a user's input... is that correct? Why do so many people just assume a beginner knows to clear cin or whatever is necessary and just say "use cin.get() and you'll be fine," then get upset with the beginner when they say "cin.get() didn't work for me?" Anyway, I'm just thinking aloud with that last sentence.

What about #3, though? What kind of issues could that potentially cause if used or would it be fine to use that method in the context of solely making a user hit a key before a program exits?

I apologize if this post is annoying in any way - I'm just trying to get a solid grasp of this stuff before I continue on. Thanks guys.

By the way, I'm Deitel's "How to Program C++: 4th Edition" which seems to be great so far and I'm sure the answers to the questions I'm asking in this thread are apparent in the book but I just tend to venture off into other things and attempt things through trial and error. It also helps me to get a feel for what you seasoned programers do, what good coding practices are, etc.

Thanks again, y'all.

-Stephen

Comments

  • MT2002MT2002 Posts: 1,444Member
    : You know what... I just commented out all prior cin's and using ONLY
    : cin.get() worked great. Does cin always retain the very last thing
    : the user inputs? I guess that would make sense but I assumed
    : initially that cin dumped the user's input into the variable and
    : automatically "emptied" itself but I guess not... so then, I guess
    : cin.get() is essentially saying "I want you to get input from the
    : user and if the user has already given me some input somewhere else,
    : just use that," thus, cin.get() appears as though it's being skipped
    : unless you clean cin beforehand so that it waits for a user's
    : input... is that correct? Why do so many people just assume a
    : beginner knows to clear cin or whatever is necessary and just say
    : "use cin.get() and you'll be fine," then get upset with the beginner
    : when they say "cin.get() didn't work for me?" Anyway, I'm just
    : thinking aloud with that last sentence.
    :

    [color=Blue]
    I heard you :-)

    std::cin doesnt have an internal buffer--it retrieves its input from
    stdin. So, if stdin isnt flushed (cleared), then cin will indeed
    return whatever value was left in the buffer.

    Some routines do this automatically (Such as cin::operator>> () )

    To flush it manually, just use [b]fflush (stdin)[/b][/color]

    : What about #3, though? What kind of issues could that potentially
    : cause if used or would it be fine to use that method in the context
    : of solely making a user hit a key before a program exits?

    [color=Blue]There is nothing wrong with #3 besides that it is nonportable.
    It gets input directly, hence doesnt need to read from stdin.

    Im not sure if the input read is stored in stdin though. I dont think so.
    [/color]

    : I apologize if this post is annoying in any way - I'm just trying to
    : get a solid grasp of this stuff before I continue on. Thanks guys.
    :
    : By the way, I'm Deitel's "How to Program C++: 4th Edition" which
    : seems to be great so far and I'm sure the answers to the questions
    : I'm asking in this thread are apparent in the book but I just tend
    : to venture off into other things and attempt things through trial
    : and error. It also helps me to get a feel for what you seasoned
    : programers do, what good coding practices are, etc.
    :
    : Thanks again, y'all.
    :
    : -Stephen
    :

    [color=Blue]I personally prefer using getchar() when terminating:[/color]
    [code]
    cout << "Thanks for playing!" << endl;
    int c = getchar();
    getchar ();
    [/code]
  • LundinLundin Posts: 3,711Member
    : [color=Blue]
    : I heard you :-)
    :
    : std::cin doesnt have an internal buffer--it retrieves its input from
    : stdin. So, if stdin isnt flushed (cleared), then cin will indeed
    : return whatever value was left in the buffer.
    :
    : Some routines do this automatically (Such as cin::operator>> () )
    :
    : To flush it manually, just use [b]fflush (stdin)[/b][/color]

    [color=Green]No, that isn't ANSI C, don't teach it, especially since he's trying to make his code portable. Read [link=http://c-faq.com/stdio/stdinflush.html]this[/link] and [link=http://c-faq.com/stdio/stdinflush2.html]this[/link].

    The best methods are either cin.get() for C++, or getchar() for C.
    There should be no need to flush the stdin if you have control over your input. If the final cin.get() doesn't work, it means there is an error in a cin-line elsewhere in the code that needs to be fixed.

    [/color]

  • StephenCStephenC Posts: 7Member
    : : [color=Blue]
    : : I heard you :-)
    : :
    : : std::cin doesnt have an internal buffer--it retrieves its input from
    : : stdin. So, if stdin isnt flushed (cleared), then cin will indeed
    : : return whatever value was left in the buffer.
    : :
    : : Some routines do this automatically (Such as cin::operator>> () )
    : :
    : : To flush it manually, just use [b]fflush (stdin)[/b][/color]
    :
    : [color=Red]No, that isn't ANSI C, don't teach it, especially since
    : he's trying to make his code portable. Read
    : [link=http://c-faq.com/stdio/stdinflush.html]this[/link] and
    : [link=http://c-faq.com/stdio/stdinflush2.html]this[/link].
    :
    : The best methods are either cin.get() for C++, or getchar() for C.
    : There should be no need to flush the stdin if you have control over
    : your input. If the final cin.get() doesn't work, it means there is
    : an error in a cin-line elsewhere in the code that needs to be fixed.
    :
    : [/color]

    [color=Green]Thank you for the replies, guys. I appreciate the advice.

    [b]Lundin[/b]: Assume the following:[/color]

    [code]
    // Testing...

    #include
    //#include "conio.h"

    using namespace std;

    int main()
    {
    int integer1 = 0;
    int integer2 = 0;
    int sum;

    cout << "Enter a number: " ;
    cin >> integer1;

    cout << "
    Enter another number: " ;
    cin >> integer2;

    sum = integer1 + integer2;

    cout << "
    The total is " << sum << "." << endl;

    //do (kbhit());

    cout << "
    Press a key of your choice to exit the program." << endl;


    //cin.clear();
    //cin.ignore();
    cin.get();

    //while (!kbhit());

    return 0;
    }
    [/code]

    [color=Green]If I uncomment the cin.ignore();, I get the results I'm after. If I leave it commented out, I never see the sum of interger1 and integer2 due to the program closing. Could this be an issue with the compiler I'm using for this (DevC++), an issue with how I'm using cin, or were you implying in your reply that cin.ignore(); would also be a necessary component of what I'm trying to achieve with making my application pause as well as maintaining code portability?

    Thanks again for the replies, guys.

    -Stephen
    [/color]
  • LundinLundin Posts: 3,711Member
    It is the way you are using cin, you aren't handling the line feed characters that will be added to stdin when the user presses enter. Changes in [color=Red]red[/color] below:

    : [color=Green]Thank you for the replies, guys. I appreciate the
    : advice.
    :
    : [b]Lundin[/b]: Assume the following:[/color]
    :
    : [code]:
    : // Testing...
    :
    : #include
    : //#include "conio.h"
    :
    : using namespace std;
    :
    : int main()
    : {
    : int integer1 = 0;
    : int integer2 = 0;
    : int sum;
    :
    : cout << "Enter a number: " ;
    : [color=Red](cin >> integer1).get();[/color]
    :
    : cout << "
    Enter another number: " ;
    : [color=Red](cin >> integer2).get();[/color]
    :
    : sum = integer1 + integer2;
    :
    : cout << "
    The total is " << sum << "." << endl;
    :
    : //do (kbhit());
    :
    : cout << "
    Press a key of your choice to exit the program." << endl;
    :
    :
    : //cin.clear();
    : //cin.ignore();
    : cin.get();
    :
    : //while (!kbhit());
    :
    : return 0;
    : }
    : [/code]:
    :
  • StephenCStephenC Posts: 7Member
    [color=Green]Aha, I see. I guess I just haven't made it that far in my studies yet. Thank you for the clarification.[/color]

    : It is the way you are using cin, you aren't handling the line feed
    : characters that will be added to stdin when the user presses enter.
    : Changes in [color=Red]red[/color] below:
    :
    : : [color=Green]Thank you for the replies, guys. I appreciate the
    : : advice.
    : :
    : : [b]Lundin[/b]: Assume the following:[/color]
    : :
    : : [code]: :
    : : // Testing...
    : :
    : : #include
    : : //#include "conio.h"
    : :
    : : using namespace std;
    : :
    : : int main()
    : : {
    : : int integer1 = 0;
    : : int integer2 = 0;
    : : int sum;
    : :
    : : cout << "Enter a number: " ;
    : : [color=Red](cin >> integer1).get();[/color]
    : :
    : : cout << "
    Enter another number: " ;
    : : [color=Red](cin >> integer2).get();[/color]
    : :
    : : sum = integer1 + integer2;
    : :
    : : cout << "
    The total is " << sum << "." << endl;
    : :
    : : //do (kbhit());
    : :
    : : cout << "
    Press a key of your choice to exit the program." << endl;
    : :
    : :
    : : //cin.clear();
    : : //cin.ignore();
    : : cin.get();
    : :
    : : //while (!kbhit());
    : :
    : : return 0;
    : : }
    : : [/code]: :
    : :
    :

  • MT2002MT2002 Posts: 1,444Member
    : : [color=Blue]
    : : I heard you :-)
    : :
    : : std::cin doesnt have an internal buffer--it retrieves its input from
    : : stdin. So, if stdin isnt flushed (cleared), then cin will indeed
    : : return whatever value was left in the buffer.
    : :
    : : Some routines do this automatically (Such as cin::operator>> () )
    : :
    : : To flush it manually, just use [b]fflush (stdin)[/b][/color]
    :
    : [color=Green]No, that isn't ANSI C, don't teach it, especially since
    : he's trying to make his code portable. Read
    : [link=http://c-faq.com/stdio/stdinflush.html]this[/link] and
    : [link=http://c-faq.com/stdio/stdinflush2.html]this[/link].
    :
    : The best methods are either cin.get() for C++, or getchar() for C.
    : There should be no need to flush the stdin if you have control over
    : your input. If the final cin.get() doesn't work, it means there is
    : an error in a cin-line elsewhere in the code that needs to be fixed.
    :
    : [/color]
    :
    :

    Ah--I see... fflush() is only defined by ANSI for output streams, not
    input.

    Thanks for the fix and links :-)
Sign In or Register to comment.