How do I manage to implement a "press enter" command instead of having the user write something? I am terribly new.
using namespace std;
int main() {
int year;
cout << "this is a leap year checker press enter.... \n";
//I want to have a press enter command over here.
cout << "put year here: \n";
cin >> year;
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
cout<<year<<" is a leap year";
else
cout<<year<<" is not a leap year";
return 0;
}
CodePudding user response:
In order to read a whole line of input, and ignore it, you can use std::cin.ignore
. For example:
#include <iostream>
#include <limits>
int main()
{
std::cout << "Please press <ENTER> to continue: ";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
std::cout << "You have pressed <ENTER>.\n";
}
CodePudding user response:
any time you need to wait for enter just use getchar()
function to wait for write char to console and press enter
CodePudding user response:
You can implement something like this as under:
#include <iostream>
#include <string>
void PressEnterToContinue(std::string text = "Press enter to continue...")
{
std::cout << text;
while (true)
{
char key = getchar();
if (key == '\r') break;
}
}
int main()
{
PressEnterToContinue();
}
Or if you want to press any key to continue, you can just do the following:
std::cout << "Press any key to continue...";
char key = getchar();