/* ************************************************************************** * Program name : 010_Arithmetic_operations (Version 1.00) * * Author : Duck Wong * * Language : C / C++ * * Compiler : Boodshed Dec-C++ compiler Ver 3.95 * * Computer : PII350 * * O/S : Windows 98 * ************************************************************************** * Version 1.00 : 2000/06/06 - first version * ************************************************************************** * Description : (a) input 2 numbers * * (b) select operator * * (c) calculation * * (d) output results * ************************************************************************** */ #include #include int main() { // part 1 : declaration char selection; float Num_1, Num_2, Result; // part 2 : input numbers and operator cout << "\nPlease enter the first number : "; cin >> Num_1; cout << "\nPlease enter the second number : "; cin >> Num_2; do { cout << "\nOperators : 1= Addition 2= Subtraction" << "\n 3= Multiplication 4=Division" << "\n\nYour selection is : "; cin >> selection; // part 3 : calculation switch (selection) // Note (1) { case '1' : Result=Num_1+Num_2; break; // Note (2) case '2' : Result=Num_1-Num_2; break; case '3' : Result=Num_1*Num_2; break; case '4' : Result=Num_1/Num_2; break; default : cout << "\n\aIncorrect selection !"; break; } } while (selection<'1' || selection>'4'); // part 4 : output cout << "\n\nThe result of operation is : " << Result << "\n"; system("PAUSE"); return 0; } /* Notes (1) Switch structure : when using the switch structure (like the CASE structure in PASCAL), remember that it can only be used for testing a constant integral expression such data type char or int etc. (2) Break statement : The Break statement, when executed in a while, for, do/while, or switch structure, causes immediate exit from that structure. Common uses of the Break statement are to escape early from a loop, or to skip the remainder of a switch structure. */