/*
* @topic Q00506 2/6/2014 Flow control
* @brief in-class demo -- if, else, and while loop
*/
#include <iostream>
#include <string>
using namespace std;
void pause()
{
//----------------------------------------------
// prevent window from closing before exiting:
//----------------------------------------------
char ch; // ch is a variable that holds a character
// curious \n is a "line feed" control character,
// it moves the cursor to the next line:
std::cout << "\nEnter X to Exit: ";
std::cin.ignore( 1, '\n' ); // extract and discard characters
std::cin.clear(); // this clears cin state
std::cin.get( ch ); // this reads a character from the keyboard,
// and pauses the program from before exiting
}
int main()
{
// string data type is a "dynamic" string in C++.
string name;
cout << "What is your name? ";
// we ask the user to enter the string:
cin >> name;
cout << "Your name is " << name << '\n';
// Example of an if statement.
// double-equal sign == compares if the strings are equal:
if ( name == "Joe" ) {
// we will use count varialble as a loop control variable:
int count = 3;
while ( count > 0 ) {
// the following text is printed three times:
cout << "I know who you are!\n";
count = count - 1;
}
}
// Example of an if-else statement.
// Again, double-equal sign == compares two values for equality:
if ( 5 == 123 ) {
cout << "bad";
} else {
cout << "good";
}
pause();
}