This program runs perfect just want to know if I have to do this arithmetic operations using functions how can I do that?

// Arithmetic.cpp - This program performs arithmetic, ( +. -, *. / ) on two numbers.
// Input:  Interactive
// Output:  Result of arithmetic operation

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;
int main(int argc, char *argv[]) 
{
   double numberOne, numberTwo;              	
   string operation;
   double result; 	

   cout << "Enter the first number: ";
   cin >> numberOne; 
   cout << "Enter the second number: ";
   cin >> numberTwo; 
   cout << "Enter an operator (+.-.*,/): ";
   cin >> operation;
		
   if (operation == "+")
   {
          result = numberOne + numberTwo;
                 }
          else if (operation == "-")
   {
          result = numberOne - numberTwo;
                }
          else if (operation == "*")
    {
          result = numberOne * numberTwo;
              }
          else if (operation == "/")
    {
          result = numberOne / numberTwo;
             } 

          cout << numberOne;
          cout << " " << operation << " ";
          cout << numberTwo;
          cout << " = ";
          cout << result << endl;

system("PAUSE");
return EXIT_SUCCESS;
} // End of main() function

Recommended Answers

All 2 Replies

Wait.....what?


What do you mean? You want to do what in functions? Call a separate function to read in the input and a function to perform the math? A separate function for each arithmetic operation? You only want to have variable declarations and function calls in your main()?

Just create some functions, for example a function for addition would
look like this :

int addNumbers(int number1, int number2){
   return number1 + number2;
}

And you can do similar for other operation.

It might seem like its not worth it but doing this will make your program
neater and better.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.