Very simple C ++ program closes after user input for no particular reason?

I just started to learn C ++, and I wrote this sample program from the text, and when I compile and run it, it just closes after the user enters any number and presses. I guess the answer to this is very obvious, so forgive me as a newbie here ... this is 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;
}
+5
source share
7 answers

Imagine that you were developing a model to run an application. You have two options:

A) When the end of the program is reached, it will end.

B) , - . , , , .

, - A , . main, .

, , ,

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

, return 0;, .

, , - :

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

.exe. , .

+5

, . , return 0, . - , .

Windows - system("pause"); ( #include <stdlib.h>)

cin.getline - .

+4

. - , cin return 0, .

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

return 0;
+4

cin, , " " .

+3

. , .

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

. , , .

: , , cin - .

+2

After the user enters the number that is stored in numberOfLanguages, he reaches return 0which returns from the function mainand, therefore, the program ends.

0
source

All Articles