Header files used include iostream include iomanip include cstdlib include

Header files used include iostream include iomanip

This preview shows page 15 - 19 out of 26 pages.

// Header files used #include <iostream> #include <iomanip> #include <cstdlib> //#include <process> // for exit() function prototype using namespace std; // User-defined function prototypes extern char menu( void ); extern void add( int n1, int n2); extern void subtract( int n1, int n2); extern void multiply( int n1, int n2); extern void result( long int res); int main( void ) { int d1, d2; char op; while (1)// true { op = menu(); cout << "\n\nPlease input values for d1 and d2, (integer) only ==> " ; cin >> d1 >> d2; switch (op) { case 'a' : case 'A' : add(d1, d2); break ; 15
Image of page 15
case 's' : case 'S' : subtract(d1, d2); break ; case 'm' : case 'M' : multiply(d1, d2); break ; } } return EXIT_SUCCESS; } // // Function menu() is invoked by main(). This function will display the // menu and passes the user's selection to main for processing. // extern char menu( void ) { char menuOption; int flag = 1; //use bool flag = true; while (flag == 1) { cout << "\ta. Add\t\ts. Subtract\tm. Multiply\t\tq. Quit\n\n\t" ; cout << "\t\t\t\tmake your selection ==> " ; cin >> menuOption; switch (menuOption) { case 'a' : case 'A' : case 's' : case 'S' : case 'm' : case 'M' : flag = 0; //use flag = false; break ; case 'q' : case 'Q' : cout << "\nFinished..." << endl; exit(0); default : cout << "\n\twrong choice, try again\n\n" ; } } return menuOption; } // // Function add() will receive two values from main(), computes the sum, and // then calls function result() for printing the sum. // extern void add( int n1, int n2) { result (( long int ) n1 + ( long int ) n2); } 16
Image of page 16
// // Function subtract() will receive two values from main(), computes the // difference, and then calls function result() for printing the difference. // extern void subtract( int n1, int n2) { result (( long int ) n1 - ( long int ) n2); } // // Function multiply() will receive two values from main(), computes the // product, and then calls function result() for printing the product. // extern void multiply( int n1, int n2) { result ( ( long int ) n1 * ( long int ) n2); } // // Function result() will receive a data from the calling function, and // displays it on the monitor. // extern void result( long int res) { cout << "\nThe result is: " << setw(6) << res << "\n\n" ; } Solution:- a. Add s. Subtract m. Multiply q. Quit make your selection ==> a Please input values for d1 and d2, (integer) only ==> 3 4 The result is: 7 a. Add s. Subtract m. Multiply q. Quit make your selection ==> m Please input values for d1 and d2, (integer) only ==> 3 4 The result is: 12 a. Add s. Subtract m. Multiply q. Quit make your selection ==> 17
Image of page 17
// File Name ................. SP6_7.CPP // Course .................... ELET 2300 Introduction to C++ Programming // Programmer ................ Dr. Farrokh Attarzadeh // Compiler .................. Microsoft Visual Studio 2010 .NET // Operating System .......... Windows Vista // Environment ............... Console Applications // Copyright ................. © 2013 Dr. Farrokh Attarzadeh // Date ...................... 01/01/2013 // // Program Description // ___________________ // // Develop a C++ program that prompts the user for two integers or two floats // depending on the menu selection. The menu options are shown below. // // Letter Meaning // _____________________ // a AddInteger // f AddFloat // q Quit // // The function main() issues the prompt and stores the user's input in the // variables d1 and d2, or fd1 and fd2, and op. Then main() checks the // letter code and invokes appropriate add() function. Function add() invokes // appropriate result() function to print the result. Option q terminates // the program. // Full function prototyping is used in this example. Parameter passing // convention is by value. Function overloadin g is used in this example.
Image of page 18
Image of page 19

You've reached the end of your free preview.

Want to read all 26 pages?

  • Fall '15
  • Marinolent

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture