O'Reilly logo

SAMS Teach Yourself C++ in 10 Minutes SECOND EDITION by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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(); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required