Doing Things Zero or More Times

The do/while loop works fine as long as you can be absolutely sure that users really ran the program with the intent of doing at least one calculation. But what if they ran the program by accident and want to stop right away?

You can use the while loop in this situation. It requires only a minor change to the main() function in Listing 9.1, as shown in Listing 9.4.

Listing 9.4. A while Loop
 1: int main(int argc, char* argv[]) 2: { 3: SAMSErrorHandling::Initialize(); 4: *5: while (SAMSPrompt::UserWantsToContinue("Divide? ")) *6: { 7: try 8: { 9: float Dividend = GetDividend(); 10: float Divisor = GetDivisor(); 11: 12: cout << Divide(Dividend,Divisor) << endl; 13: } 14: catch (...) 15: { 16: SAMSErrorHandling::HandleNotANumberError(); ...

Get SAMS Teach Yourself C++ in 10 Minutes SECOND EDITION now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.