Program loop

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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
}


Thanks,
Ken
I don't know if I got you right, but I think you want a method to freeze the screen before the program terminates.

I use the system("Pause"); function which is generated by creating a shell similar as the one in CMD.

You can use it directly, but some compilers want you to #include <cstdlib> .

I recommend that you use this function for training purpose only, because it is not efficient way to pause screen.
Last edited on
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.
Use char for KeyIn type

change line 19 to:

while ( KeyIn != '0');
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 ...)

Ken
Last edited on
Topic archived. No new replies allowed.