2

I'm not new to programming, but I am relatively new to C++. I would like to distribute simple console applications so I can help others as I learn. The vast majority of machines on the campus of my university are windows based, and have the Borland compiler installed by default. I prefer to do my development on a Linux-based system with g++ and other tools. So I'd like to add some cross-platform way of leaving the program running until the user presses enter. That way, the user is able to view the output even if he or she double clicked on the exe rather than running it in the console in windows. To do this, I wrote something similar to:

#include <iostream>

using namespace std;

int main()
{

    float val1, val2;
    bool wait = true;

    cout << "Please enter the first value to add: ";
    cin >> val1;
    cout << "Please enter the second value to add: ";
    cin >> val2;
    cout << "Result: " << val1 + val2 << endl << endl;

    cout << "Press enter to exit...";

    while (wait)
    {
        if (cin.get() == '\n')
            wait = false;
    }

    return 0;
}

Using the code above, the program exits after displaying the result. However, if you comment out the cin calls, it works as expected. This leads me to believe that cin.getline is picking up my enter key press from my last data entry. I suspect this is due to the tightness of the loop. I have learned that there is no cross-platform sleep function in C++, so that is not an option. What else can I do to make this work?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Sean W.
  • 4,944
  • 8
  • 40
  • 66

2 Answers2

4

You can add

cin.ignore(1);

or

cin.ignore(INT_MAX, '\n');

before you call cin.get(). This will ignore the newline left from the user entering the second number or all the characters in the buffer until a newline.

Also you neither need to compare the return value of get to '\n' nor put it in a loop. The user has to hit enter for get to return, so

cout << "Press enter to exit...";
cin.ignore(INT_MAX, '\n');
cin.get();

Is sufficient.


What happens if you do

cout << "Press enter to exit...";
while (wait)
{
    if (cin.get() == '\n')
    wait = false;
}

Is that the loop is entered, and cin.get() is called. The user can enter any amount of text at the console as he wants. Say they entered

Hello

in the console. Then the user presses the Enter key. cin.get() returns H, and ello\n is still left in the buffer. You compare H with \n and see that they are not equal, continue the loop. cin.get() is called and since there is already text in the buffer, returns e immediately. This loop continues wasting time until it gets to the last character of the buffer which is \n and it compares true with \n so the loop breaks. As you can see, this is a waste of time.

If you do put cin.get() in a loop and compare the return value of it with \n, there is also the danger of cin coming to an end-of-file before an \n is encountered. I believe the effect of this on your program would be an infinite loop, but I'm not sure since I can't try it on Windows.

Also, even though you don't need to use a loop in the first place, you are wasting even more time with a bool because you could reduce the loop to

while (true)
    if (cin.get() == '\n') break;
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • does get() wait for a '\n' character? I'm unsure based off the internet. Though a simple test would clarify for an implementation. – Mooing Duck Sep 02 '11 at 23:35
  • @Mooing yeah, it waits for the user to press enter to return. – Seth Carnegie Sep 02 '11 at 23:36
  • @Sean but it's a waste of time since `cin.get()` waits for you to press enter for it to return to the caller anyway. – Seth Carnegie Sep 02 '11 at 23:37
  • I added that call just before the if statement, and included limits.h, and the program does not seem to react to any key press once in the loop. The cursor moves down a line, but nothing else happens. – Sean W. Sep 02 '11 at 23:45
  • Ah, I see. I have removed the loop and simply called cin.get(). But, the program simply exits as soon as the result is displayed. As usual I'm over thinking it. But I'm still not sure why the program is exiting. – Sean W. Sep 02 '11 at 23:53
  • @Mooing: The behavior of get() is dependent on the underlying istream object. See [this answer for information about cin's default behavior](http://stackoverflow.com/questions/2525352/problem-of-using-cin-twice/2525384#2525384). – Conspicuous Compiler Sep 02 '11 at 23:53
  • @Sean whoops, updated. The `\n` is left in the buffer after `cin >> val2`. Change `cin.ignore(INT_MAX)` to `cin.ignore(1)`. I compiled your program like that and it worked for me. – Seth Carnegie Sep 02 '11 at 23:57
  • @Seth Indeed it does. Thanks! – Sean W. Sep 03 '11 at 00:01
  • `cin.ignore(1)` only works if '\n' is typed directly after the last valid entry. If you feed "12 13 14" to your program then press enter, it will not wait for another enter key. Only the "1" of "14" would have been ignored. – alexisdm Sep 03 '11 at 00:10
  • @alexisdm yes I know, my answer was for a quick fix. I updated a while ago to reflect a longer term solution. – Seth Carnegie Sep 03 '11 at 00:18
  • I've decided to give this guide a try http://www.cplusplus.com/forum/articles/6046/ – Sean W. Sep 03 '11 at 00:19
  • 1
    @Sean The code given there isn't very reasonable. While using getline instead of operator>> is often preferred (just because the input is structured per-line), it's not a catch-all rule. Input and output of integers is covered well by the C++ FAQ (http://www.parashift.com/c++-faq-lite/input-output.html), and is done in an utterly ridiculous way in the link. Generally, I'd be wary of things on cplusplus.com, it's not the best of sites. – Cactus Golov Sep 03 '11 at 10:45
0

After cin >> you should ignore all the characters in the buffer until the `\n' with

#include <limits> // for std::numeric_limits as indicated by Marlon

std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

Then you can wait for the next line with:

cout << "Press enter to exit...";
cin.get();
alexisdm
  • 29,448
  • 6
  • 64
  • 99