2

I am trying to learn C++ using Visual Studio 2013 but I have an issue that prevents me to proceed. After starting the console on debug and getting input from a user console immediately closing. How can I make my program wait my command to close?

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double number, answer;
    cout << "Enter a number: ";
    cin >> number;
    answer = sqrt(number);
    cout << "Square root is " << answer << endl;
    cin.get();
    return 0;
}
Omer
  • 8,194
  • 13
  • 74
  • 92
  • 4
    I always just put a breakpoint on the `return` line. – Mark Ransom Jul 14 '14 at 19:11
  • 1
    I was just about to say that. Modifying the code to do it is really unnecessary. – chris Jul 14 '14 at 19:12
  • 1
    There are lots of duplicates for this: [How to keep the console window open in visual c++?](http://stackoverflow.com/q/454681/1227469), [How to stop console from closing on exit?](http://stackoverflow.com/q/4118073/1227469), [Preventing console window from closing on Visual Studio C/C++ Console application](http://stackoverflow.com/q/1775865/1227469), [How to keep the console window open in visual c++?](http://stackoverflow.com/q/454681/1227469) – JBentley Jul 14 '14 at 19:19
  • @MarkRansom When I've suggested that in the past, I've had complaints that it becomes difficult where a program has multiple exit points. To which my response is that if you're unsure where a program is terminating, then you probably need to debug elsewhere prior to termination anyway. – JBentley Jul 14 '14 at 19:28
  • @JBentley, , What if I run the program from .exe? – Omer Jul 14 '14 at 19:33

3 Answers3

1

Remove statement

cin.get();

and use Ctrl+F5 to run the program from the IDE.

This statement

cin.get();

reads the new line character that is present in the input buffer of the standard stream after entering a number in the input statement above. Or use a call of cin.ignore (at least as cin.ignore()) with an appropriate argument before calling cin.get() to clear the buffer.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • If you start with `Ctrl-F5`, what prevents the console window from closing after the program exits? – Mark Ransom Jul 14 '14 at 19:15
  • @MarkRansom, Whatever VS uses to run your program pauses it afterward. I was never sure why this behaviour exists only while not debugging. – chris Jul 14 '14 at 19:15
  • @Mark Ransom MS VS inserts its own pause befors closing the window. – Vlad from Moscow Jul 14 '14 at 19:16
  • But only if your project is configured properly: see [this answer](http://stackoverflow.com/a/1152873/1227469) for normal projects, and [this one](http://stackoverflow.com/a/23288778/1227469) for makefile projects. – JBentley Jul 14 '14 at 19:21
  • @VladfromMoscow, What if I run the program from .exe? – Omer Jul 14 '14 at 19:32
  • 1
    @osman Then use std::cin.ignore as I pointed out in my post. – Vlad from Moscow Jul 14 '14 at 19:34
  • @VladfromMoscow, thanks, I understood the case, but I think it will be better if you explain it in your answer for both IDE and exe cases. – Omer Jul 14 '14 at 19:39
1
How can I make my program wait my command to close?

Ctrl+F5 works for me.

tagoma
  • 3,896
  • 4
  • 38
  • 57
  • 1
    With this answer, you have to make sure your project is configured properly: see [this answer](http://stackoverflow.com/a/1152873/1227469) for normal projects, and [this one](http://stackoverflow.com/a/23288778/1227469) for makefile projects. – JBentley Jul 14 '14 at 19:20
1

Looks like you are missing an ignore statement. The carriage return from the previous cin is still in your buffer.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double number, answer;
    cout << "Enter a number: ";
    cin >> number;
    answer = sqrt(number);
    cout << "Square root is " << answer << endl;
    cin.ignore(INT_MAX, '\n');
    cin.get();
    return 0;
}
Roy Folkker
  • 407
  • 3
  • 8