Log in

View Full Version : Press Enter to Add One


Any Mouse
07-12-2011, 20:29
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.

plnyyanks
07-12-2011, 20:46
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?

Andrew Lawrence
07-12-2011, 20:56
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
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:

plnyyanks
07-12-2011, 21:18
Trust Phil. He knows this stuff. :cool:
Thanks Andrew, I try. :)

calcmogul
07-12-2011, 21:41
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.

Any Mouse
08-12-2011, 18:59
Thanks that was very helpful.