### Modularize a program into four functions

Write a simple calculator program that allows input of two integer operands and outputs the result of all five arithmetic operations (+, -, *, / , & %).

Modularize into “at-least” the following functions:
getData
getInteger
processData
displayData
Use value returning, void, pass-by-value, & pass-by-reference as is appropriate.

I am not sure if I have even approached this problem correctly. I have done the very beginning but have no idea where to go from there. Not sure how to divide the work into the functions getInteger, processData, and displayData.

 ``12345678910111213141516171819202122232425262728293031323334353637`` `````` #include using namespace std; int getData(int x, int y); //function prototypes int getInteger(); void processData(); void displayData(); int main() { int x = 0; int y = 0; int sum = 0; int diff = 0; int prod = 0; int quot = 0; int mod = 0; cout<<"Please enter an integer: "; cin>>x; cout<<"Please enter an integer: "; cin>>y; getData(x,y); //call function return 0; } int getData(int x, int y) { int sum, diff, prod, quot, mod; sum = (x+y); diff = (x-y); prod = (x*y); quot = (x/y); mod = (x%y); return 0; }``````
Can you elaborate as to what the functions should be doing? For instance, what's the difference between getData and getInteger? getData sounds like it might be used to get user input, but then what is getInteger for? Does processData do the actual calculations? If your instructor gave you any other (more specific) instructions, please post them.
That is exactly what I don't understand. What I posted at the beginning is all I was provided with. To me I could do this in two functions but I have to use at least four which I don't know how to do.
> I could do this in two functions but I have to use at least four

Make it a round dozen.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667`` ``````#include #include int get_integer( const char* prompt ) { int number ; std::cout << prompt << "? " ; if( std::cin >> number ) return number ; std::cout << "error in input. please try again\n" ; std::cin.clear() ; // clear the error state std::cin.ignore( 1000, '\n' ) ; // throw away the junk return get_integer(prompt) ; // try again } void get_data( int& x, int& y ) { x = get_integer( "first number" ) ; y = get_integer( "second number" ) ; ; } int sum( int x, int y ) { return x+y ; } int difference( int x, int y ) { return x-y ; } int product( int x, int y ) { return x*y ; } int quotient( int x, int y ) { assert( y != 0 ) ; return x/y ; } int remainder( int x, int y ) { assert( y != 0 ) ; return x%y ; } void print_results( int x, int y, int sum, int difference, int product ) { std::cout << x << '+' << y << " == " << sum << '\n' << x << '-' << y << " == " << difference << '\n' << x << '*' << y << " == " << product << '\n' ; } void print_div_results( int x, int y, int quotient, int remainder ) { std::cout << x << '/' << y << " == " << quotient << '\n' << x << '%' << y << " == " << remainder << '\n' ; } void process_data( int x, int y ) { print_results( x, y, sum(x,y), difference(x,y), product(x,y) ) ; if( y != 0 ) print_div_results( x, y, quotient(x,y), remainder(x,y) ) ; } bool run_again() { std::cout << "run again? (y/n): " ; char c ; std::cin >> c ; return c == 'y' || c == 'Y' ; } void run_once() { int x, y ; get_data( x, y ) ; process_data(x,y) ; } int main() { do run_once() ; while( run_again() ) ; }``````
Topic archived. No new replies allowed.