user637965
user637965

Reputation:

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 many 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;
}

Upvotes: 9

Views: 18666

Answers (7)

Ed Swangren
Ed Swangren

Reputation: 124642

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;

Upvotes: 15

user732933
user732933

Reputation: 278

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.

Upvotes: 2

dee-see
dee-see

Reputation: 24078

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.

Upvotes: 6

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798666

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

Upvotes: 3

lumberjack4
lumberjack4

Reputation: 2872

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;

Upvotes: 5

Morten Kristensen
Morten Kristensen

Reputation: 7613

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.

Upvotes: 0

Shirik
Shirik

Reputation: 3701

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.

Upvotes: 7

Related Questions