Страница 1 из 1

Предотвращение закрытия консоли

Добавлено: 10 июн 2024, 14:33
ya
std::cin >> i;

или

cin.ignore()


вы также можете использовать:
вам понадобится #include<iostream.h>
а затем добавьте следующее в конец вашего main()
....
getche();
}



предлагаю создать этот небольшой инструмент:

Код: Выделить всё

class KeepRunning {
  public:
    ~KeepRunning() {
      // Whatever code you prefer to keep the program running, e.g.
      system("pause"); // "Press enter"
      sleep(5); // Wait 5 seconds
    }
};

В вашей основной функции:

Код: Выделить всё

int main() {
  KeepRunning kr;

  // code

} // Program stops here 
Таким образом, программа должна держать консоль открытой, даже если она не доходит до конца main .


В конце просто поставьте
cin.get;


использовать ли "system ()", "cin.get ()", "cin.ignore ()" или что-то еще

Код: Выделить всё

std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

Код: Выделить всё

int main()
  {
  // Be Eliza
  string s;
  cout << "What is your favorite color? ";
  cin >> s;
  cout << "Why do you like " << s << " so much?\n";

  // gobble everything until the ENTER from the last >>
  cin.ignore( numeric_limits<streamsize>, '\n' );
  // ASSERTED: we know that the input stream is at the beginning of the next line

  // Program's over. Wait for user to press -anything-, up to and including the ENTER key
  cout << "Press ENTER to quit.";
  cin.ignore( numeric_limits<streamsize>, '\n' );
  // ASSERTED: we know the user had to press at least the ENTER key to get this far.

  cout << "bye.";
  return 0;
  }
очистка экрана

Код: Выделить всё

cout << string( 100, '\n' );

использование функции getch();. это останавливает программу и ожидает, пока вы не нажмете клавишу на клавиатуре, вам необходимо включить библиотеку conio.h

Код: Выделить всё

#include <iostream>
#include <conio.h>

int main() {
    cout << "Press any key to continue... ";

    _getch();

    return 0;
}
Некоторые компиляторы скажут вам использовать _getch(); вместо getch(); я не знаю почему

Код: Выделить всё

std::cout << "Press Enter to Exit ... " << std::endl;

//in case a badbit or failbit has been set
std::cin.clear();

//i prefer this to ignore() because ignore sometimes means hitting enter twice
std::cin.sync(); 

std::cin.get();

return 0;

Код: Выделить всё

std::cout << "Press Enter to exit...";
std::cin.clear();  // brilliant!
std::cin.sync();   // brilliant!
std::cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );  // lets the user type anything up-to and including the Enter key