Can someone show me how to change the cases down below to call functions for add, subtract, divide, multiply.

Also return the answer back to the case
to display the result in the case.
Prototype the functions before main
and add the function definitions
after main.

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	int first;
	int second;
	char op;

	cout << "Enter the first number: ";
	cin >> first;
	cout << endl;

	cout << "Enter the operator: + or - or * or / : ";
	cin >> op;
	cout << endl;

	cout << "Enter the second number: ";
	cin >> second;
	cout << endl;

	if (second == 0) 
	cout << "ERROR: cannot divide by zero" << endl;
	else
	switch (op)
	{
	case '+':
		cout << first << " " << op << " " << second << " = " << first + second << endl;
		break;
	case '-':
		cout << first << " " << op << " " << second << " = " << first - second << endl;
		break;
	case '*':
		cout << first << " " << op << " " << second << " = " << first * second << endl;
		break;
	case '/':
		cout << first << " " << op << " " << second << " = " << first / second << endl;
		break;
	default:
		cout << first << " " << op << " " << second << " = " << "Illegal operation" << endl;
		break;
	}

	return 0;
}

Recommended Answers

All 3 Replies

Can you start with ONE function, like add?

Assuming you are confused about the requirements, you would need to put this line somewhere above where you declare main():

int add(int first, int second);

...and this would go after the last brace of main

int add(int first, int second)
{
	return first + second;
}

... then you would need to modify the cout inside of the case statement so it uses the call to that function.

THEN do the same for the other types of operations.

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.