Complete listing of Input01.cpp, Listing 1
/*File: Input01.cpp This C++ program illustrates simple standard input from the keyboard. When the type of input data matches the types of the variables into which the data is stored, the program runs correctly producing the following screen output: Enter three values separated by spaces 1 2 3 1 2 3 Press any key to continue However, when the type of the input data fails to match the types of the variables into which the data is stored, the program fails, as shown in the following screen output, but doesn't give any other indication that a failure has occurred. Enter three values separated by spaces 1 1.5 2 1 1 -858993460 Press any key to continue ************************************************/ #include <iostream> using namespace std; class Input01{ public: static void classMain(){ //Instantiate an object in dynamic memory // and save its reference in a pointer // variable. Input01* ptrToObject = new Input01(); //Invoke an instance function on the object. ptrToObject -> doSomething(); }//End classMain function //-------------------------------------------// //An instance function of the Input01 class void doSomething(){ int first; int second; int third; //Display a prompt cout << "Enter three values separated by"; cout << " spaces\n"; //Get three input values. cin >> first >> second >> third; //Alternative syntax //cin >> first; //cin >> second; //cin >> third; //Display the three input values cout << first << " " << second << " "; cout << third << endl; }//end doSomething function };//End Input01 class //---------------------------------------------// int main(){ Input01::classMain(); return 0; }//end main Listing 1 |