开发者

very basic C++ program closes after user input for no particular reason?

I just started learning C++ and I wrote this sample program from the text and when I compile and run it, it just closes after the user inputs any number and presses enter. I'm guessing the answer to this is very obvious so forgive me as newbie here....it's really my first C++ program :P

#include <iostream>

using namespace std;

int main ()
{
  int numberOfLanguages;
  cout << "Hello Reader.\n"
       << "Welcome to C++.\n"

  cout << "How 开发者_StackOverflowmany programming languages have you used? ";
  cin  >> numberOfLanguages;

  if(numberOfLanguages < 1)
      cout << "Read the preface.  You may prefer.\n"
           << "a more elementary book by the same author.\n";
  else
      cout << "Enjoy the book.\n";

  return 0;
}


Imagine you were designing a model for application execution. You have two choices:

A) When the end of a program is reached it shall terminate.

B) When the end of a program is reached the program shall remain alive in some strange limbo state. It will still retain system resources and will not actually be doing anything, but in order to close the user must terminate it explicitly.

I think anyone would go for option A here, and that is what you are seeing. The end of main is reached and your program exits.

If you would like it to pause at the end take some input from the user, i.e.,

char c;
std::cin >> c;
return 0;


The program closes because there's nothing more for the program to do. It outputs the final statements really really fast and then reaches return 0 which causes it to exit. You'll want to do something there to pause the program.

On Windows, a basic way to do that is system("pause"); (you will need #include <stdlib.h>)

cin.getline is a more standard way to do it.


It closes because the execution reaches return 0; and there is nothing left to do.

If you want the program to wait before closing you could add an something like this:

cout << "Press enter to exit...";
cin  >> someVarThatWontBeUsed;

You could also run the program from the command line instead of running the .exe. It will reach end of execution anyway but the prompt will stay open.


Your program ends right after you print out your text. If you want to see anything on the screen you can add a cin right before your return 0 so your program waits for a user response before exiting.

// Wait for user to hit enter
cin >> dummyVar;

return 0;


Either put another read from cin to wait for the user, or open the Command Prompt yourself and run it there.


The program you posted has an error. I was not able to compile what you posted.

  cout << "Hello Reader.\n"
   << "Welcome to C++.\n"

is not terminated with a semicolon. I added a semicolon and it compiles and runs as you expect.

Edit: Of course, you have to run the program in a terminal that stays open after the program exits, or use cin to wait for more input, or something like that.


After the user inputs a number, which is saved to numberOfLanguages, it reaches return 0 which returns from the main function and thus the program ends.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜