Advertisement
Madotsuki

Sentinel Value Demonstration

Mar 3rd, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. // This program demonstrates the usage of
  2. // a sentinel value to create a continuous loop
  3. // that can be exited based on a user input.
  4.  
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.     int numInput;       // Just a simple number input.
  12.     char userInput = '.';   // Assign a sentinel character.
  13.                 // This character is important, as
  14.                 // it will be used to check if the user
  15.                 // is finished with the program based
  16.                 // on the condition.
  17.  
  18.     while(userInput != 'q')     // If userInput is NOT q... execute the loop.
  19.     {
  20.         // Body of content. This can be anything you want it to be.
  21.         // This will get repeated numerous times until the input 'q' is
  22.         // given, which will break the while loop condition.
  23.  
  24.         cout << "Enter a whole value: " << endl;
  25.         cin >> numInput;
  26.         cout << "The square value of that is " << numInput * numInput << endl;
  27.  
  28.         // This is the part where the user gets the opportunity to exit
  29.         // the loop.
  30.         cout << "Enter another number? Type 'q' to quit." << endl;
  31.         cin >> userInput;
  32.     }
  33.  
  34.     // When you're here, you have exited the loop.
  35.     // End program.
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement