Listing 13, Structures04.cpp
/*File: Structures04.cpp This C++ program illustrates the if else if structure. The program asks the user to enter an integer between 1 and 3 and then displays a message related to the value of the integer. An error message is displayed if the integer is not one of the specified values. The program displays the following output on the screen for several different input values: Enter an integer, 1-3 inclusive: 0 Invalid input Enter an integer, 1-3 inclusive: 1 Thanks for the 1 Enter an integer, 1-3 inclusive: 2 Thanks for the 2 Enter an integer, 1-3 inclusive: 3 Thanks for the 3 Enter an integer, 1-3 inclusive: 4 Invalid input ************************************************/ #include <iostream> using namespace std; class Structures04{ public: static void classMain(){ Structures04* ptrToObject = new Structures04(); ptrToObject -> doSomething(); }//End classMain function //-------------------------------------------// //An instance function of the Structures04 // class void doSomething(){ //Begin sequence structure int temp = 0; cout << "Enter an integer, 1-3 inclusive: "; cin >> temp; //End sequence structure //Begin selection if(temp == 1){ cout << "Thanks for the 1" << endl; }else if(temp == 2){ cout << "Thanks for the 2" << endl; }else if(temp == 3){ cout << "Thanks for the 3" << endl; }else{ cout << "Invalid input" << endl; }//end else //End selection structure }//end doSomething function };//End Structures04 class //---------------------------------------------// int main(){ Structures04::classMain(); return 0; }//end main Listing 13 |