Monday, April 12, 2021

-- How to create an exit loop in C++ --

 This is another typical question from my students.

#include <iostream>
using namespace std;

int main()
{
    bool exit = false;  // Variable to be used to signal the exit.
    char question; // Variable holding the value to be entered from the keyboard
    int x = 1;
    while (exit == false)  // If the user indicated they want to exit skip the loop
       {
         cout<<"Hello World:" << x << endl ;
         cout << "Do you want to exit (Y/N): "; // Ask the question
         cin >> question;  // Collect the answer from the keyboard
         if (toupper(question) == 'Y') // Evaluate the answer and change the value of exit.
         exit = true;
         
       }
    return 0;

No comments: