Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This program demonstrates the usage of
- // a sentinel value to create a continuous loop
- // that can be exited based on a user input.
- #include <iostream>
- using namespace std;
- int main()
- {
- int numInput; // Just a simple number input.
- char userInput = '.'; // Assign a sentinel character.
- // This character is important, as
- // it will be used to check if the user
- // is finished with the program based
- // on the condition.
- while(userInput != 'q') // If userInput is NOT q... execute the loop.
- {
- // Body of content. This can be anything you want it to be.
- // This will get repeated numerous times until the input 'q' is
- // given, which will break the while loop condition.
- cout << "Enter a whole value: " << endl;
- cin >> numInput;
- cout << "The square value of that is " << numInput * numInput << endl;
- // This is the part where the user gets the opportunity to exit
- // the loop.
- cout << "Enter another number? Type 'q' to quit." << endl;
- cin >> userInput;
- }
- // When you're here, you have exited the loop.
- // End program.
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement