Press Enter to Add One

I am new to c++ programing and am want to make a program in which if you press enter a number will increase by one. I can make the doesprogram loop and recognize ‘enter’, but when I try to put them together it doesn’t work. Please help.

Try something like this:


//make sure you #include <iostream>
int count = 0; //variable to store count
while(count < 10){ //this will only run 10 times
    system("pause"); //wait for user to press enter - this will send the Windows shell command "pause", and wait for the user to press enter
    count++; //increment count
    std::cout << "Count: " << count << endl; //show count to user
}

You’ll need a variable to store the running count, and after the user presses enter, it’ll have to be incremented. Does that snippet make sense logically?

Trust Phil. He knows this stuff. :cool:

Thanks Andrew, I try. :slight_smile:

You can write this with a for loop too, which has variable incrementing built in:

#include <iostream> //has cout

for ( int count = 1 ; count <= 10 ; count++ ) {
    system("pause"); //waits for user to press enter
    std::cout << "Count: " << count << std::endl;
}

std:: needs to be added to the front of STL functions like cout unless you specify “using namespace std;” at the top of the program.

Thanks that was very helpful.