1

I have a problem with the c++ code below. The problem is that after the user has typed in the input, the black screen disappears very fast. I would like the black screen to stay up until I have pressed the enter button. I have tried using the cin.get(), but I am new to c++, and I don't understand what is wrong. (I don't get an error code, it's just that I would like the black screen to stay). I am using Visual Studio.

    #include <iostream>
#include<string>

using namespace std;

int main() {
    string password = "Hello";

    cout << "Enter password" << flush;
    cin.get();
    string input;
    cin >> input;

    if (input == password) {
        cout << "The password is correct" << endl;
        cin.get();
    }


    if (input != password) {
        cout << "Access denied" << endl;
        cin.get();
    }

    return 0;
}
Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
David
  • 159
  • 8

1 Answers1

4

It closes because it reaches the end of the main(), which means there is nothing more to do, and the function returns. A simple solution would be to use getChar() function before the return statement, this will leave the window open until you type a character (any character) on the keyboard.

Paul
  • 473
  • 6
  • 19