I'm just starting with C++ and I know this is a really simple newbie question .. but I can't seem to find or write anything that works the way I want. I've been programming in Basic since the early 70s (before monitors even) but I suspect all that background is hindering my thinking here. I'm trying to change my thought process to C++ - easier said than done.
I get a bit frustrated with writing little learning programs in a win32console that do one thing one time and then terminate so I want to wrap that code in a loop. For example, say the program figures area. It asks for width and length and then figures the area. As it is, I enter those parameters once and the program spits out the answer and then goes to a DOS pause before closing. I want to be able to run my core program with different parameters and see the result and then have it loop back around on itself to run the core program again once I enter a key. It should check for a particular key in which case it should terminate - otherwise loop back and run the core program again.
IOW = what I'm after is to have the user enter a key and press enter ... then the program does something and loops back around again to ask for another key. If that key is zero (0) then the loop terminates. This waits for an input and then permanently loops the cout until I interrupt it - not at all useful. I hope I expressed myself clearly here.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int KeyIn;
do
{
//core program goes here (example above was to figure area ..
//this is just a nonsense program that does nothing)
cin >> KeyIn; //get input
cout << KeyIn << endl; //spit out the key pressed
}
while ( KeyIn != 0); // if it is not the zero key, loop back and do it all again
return 0; //terminate if the zero key is pressed
}
No .. thank you .. I could easily use getch() for that - running the program without debug in VS2010 I get the DOS pause prompt when return 0 is run.
What I'm looking for is this: I want the user to press a key - if it is not 0, the program will run (whatever the program is. Then I want the program to loop back around and ask the user to press a key again. If it is not 0, the program runs again and loops back to ask for an input ... etc. etc until a 0 is pressed for that input. The underlying program isn't important - just that it is enclosed in a loop.
Thank you for the reply .. yep ... that was it. Thank you very much. Now I need to figure out why using an integer didn't work ... (always something ...)