Jun 25th, 2013, 12:58 PM
loop input question
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 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.
//core program goes here (example above was to figure area .. this is just a dumb program)
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
Click Here to Expand Forum to Full Width
Survey posted by VBForums.