0

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;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Please decribe what having a "press enter command" would look like. What would be a story in which it is visible? What should it cause? In, contrast, how does not having one look like? What is the difference? – Yunnosch Feb 26 '22 at 15:02
  • Why is reading in a string, or a whole line, and ignoring it not what you want? – Yunnosch Feb 26 '22 at 15:03
  • I don't know but if this is what you want you can simply make `cin >> [anything];` and wait for the user to press enter. – seghair tarek Feb 26 '22 at 15:07
  • Do you mean you want to enter the values on the command line, like this: `myleapyearprogram 2012`? That would involve [using `argc` and `argv` to get the arguments](https://www.cprogramming.com/tutorial/lesson14.html), which you would then have to convert from a c-string to an integer. – Schol-R-LEA Feb 26 '22 at 15:25
  • @Yunnosch Like I said I am entirely new. I just wanted to see how it would be inserted, but I probably should post that when I am one month or longer in to c++. – user18316905 Feb 26 '22 at 15:30

3 Answers3

1

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";
}
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • You don't need the manual flush. By default, `cin` and `cout` are `tie()`'d together, so that reading input will automatically flush any pending output first. – Remy Lebeau Feb 26 '22 at 16:01
  • @RemyLebeau: Although I have never experienced it myself, [on some platforms, it is necessary to manually flush the output buffer before input](https://stackoverflow.com/questions/16877264/c-c-printf-before-scanf-issue). This is probably because [§7.21.3 ¶3 of the ISO C11 standard](http://port70.net/~nsz/c/c11/n1570.html#7.21.3p3) does not guarantee that the output is flushed manually before input. However, this probably only applies to C and not C++, as [29.7.4.2.4 of the ISO C++20 standard](https://timsong-cpp.github.io/cppwp/n4868/input.output#istream.sentry) does make such a guarantee. – Andreas Wenzel Feb 26 '22 at 20:08
  • @RemyLebeau: [continued from previous comment] Therefore, you are probably right that the explicit flush is not necessary. For this reason, I have removed it. – Andreas Wenzel Feb 26 '22 at 20:09
  • those links are for C I/O not C++ I/O. What I said is true for C++'s `cin`/`cout` streams specifically. – Remy Lebeau Feb 26 '22 at 20:13
  • @RemyLebeau: Ah, yes, you are right that C++ probably also has the same issue when using C-style I/O. It is only C++-style I/O that provides the guarantee that flushing is not necessary (unless you untie the streams). – Andreas Wenzel Feb 26 '22 at 20:18
-1

any time you need to wait for enter just use getchar() function to wait for write char to console and press enter

Bitor
  • 1
  • Are you suggesting calling `getchar()` in a loop, until the function call returns `'\n'`? You may want to add a short code example. – Andreas Wenzel Feb 26 '22 at 15:11
  • No do not use it in loop. just place it. this function will pause program until user press ENTER any other chars will print in console window – Bitor Feb 26 '22 at 15:15
  • @Bitor that is incorrect. `getchar()` will return the next key entered, whatever it happens to be. It will not wait for ENTER specifically. So it would have to be called in a loop. – Remy Lebeau Feb 26 '22 at 15:57
-1

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();
The Coding Fox
  • 1,488
  • 1
  • 4
  • 18