Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   Press Enter to Add One (http://www.chiefdelphi.com/forums/showthread.php?t=98688)

Any Mouse 07-12-2011 20:29

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.

plnyyanks 07-12-2011 20:46

Re: Press Enter to Add One
 
Try something like this:

Code:

//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

Re: Press Enter to Add One
 
Quote:

Originally Posted by plnyyanks (Post 1089464)
Try something like this:

Code:

//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

Re: Press Enter to Add One
 
Quote:

Originally Posted by SuperNerd256 (Post 1089468)
Trust Phil. He knows this stuff. :cool:

Thanks Andrew, I try. :)

calcmogul 07-12-2011 21:41

Re: Press Enter to Add One
 
You can write this with a for loop too, which has variable incrementing built in:
Code:

#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

Re: Press Enter to Add One
 
Thanks that was very helpful.


All times are GMT -5. The time now is 13:31.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi