value-returning function

Hello,
I am very new to C++ programming. We are learning about value returning functions and the assignment is:

Create the Following Menu in a loop so the menu will continually show until the user chooses to exit.
1. Add
2. Multiply
3. Exit
Add a value returning function to menu option 1 and menu option 2. This means that you will create two functions: an Addition function and a Multiplication function. The functions will receive two doubles and return a double.

Below is the code I have written which will not compile. Any help would be greatly appreciated. Thanks!




#include <iostream>
#include <iomanip>

using namespace std;

int options ();
int options1 ();
int options2 ();
int options3 ();
double addition (double num1, double num2);
double multiplication (double num1, double num2);

int main ()

{
int option = 0;
cout << "1.Add" << endl;
cout << "2.Multiply" << endl;
cout << "3. Exit" << endl;

cout << "Enter option 1 for addition, option 2 for multiplication, or option 3 to exit"
"\n Enter an option:";
cin >> option;

switch (option)

{
case 1: option 1;

int option 1 ();

{
double num1 = 0;
double num2 = 0;
void int key = 0;

cout << "\n Enter a number:";
cin >> num 1;

cout << "\n Enter a second number:";
cin >> num2;

double sum = addition (num1, num2);
          
            cout << "\n Sum is: " << sum <<endl;
            cout << "Press any key to continue: ";

           
            break;
           
            case 2: option2;
            int option2 ();
{
            double num1 = 0;
            double num2 = 0;

cout << "\n Enter a number:";
cin >> num1;
cout << "\n Enter a second number: ";
cin >> num2;

double product = multiplication(num1, num2);
           
cout << "\n Product is: " << product << endl;

}
            break;
            case 3: option3;
            int option3();
            {
return 0;
}
            break;
            default:
                        cout << "\n Invalid number entered: ";
}

}
double addition(double num1, double num2)
{
            return num1 + num2;
}
double multiplication(double num1, double num2)
{
            return num1*num2;
}

I'm learning too and my advice to you is try to make the code as neat as possible so you wont get lost on longer programs....

The value return functions would be better if you put them outside the int main in my opinion.

when calling a function in main remember to put the value you want to pass to the function.
in this case i passed num1 and num2.

i forgot to add the default that would say invalid input. but its easy to put it..

try to understand the code i provided you and if you have any question feel free to ask :)

try to use comments aswell it helps alot and follow the comments


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <iomanip>
using namespace std;

double option1(double num1, double num2);
double option2(double num1, double num2);

int main()
{
	int option;
	double num1, num2, ans;

	do
	{
		cout << "1. Add \n 2. Multiply \n 3. Exit" << endl;
		cout << "What would you like to do? : ";
		cin >> option;

		switch(option)
		{
		case 1:
			{
				cout << "Enter 2 number you would like to add: ";
				cin >> num1 >> num2;
				ans = option1(num1, num2);
				cout << "The sum of " << num1 << " and " << num2 << " is: " << ans << endl << endl;
				break;
			}
		case 2:
			{
				cout << "Enter 2 number you would like to multiply: ";
				cin >> num1 >> num2;
				ans = option2(num1, num2);
				cout << "The product of " << num1 << " and " << num2 << "is: " << ans << endl;
				break;
			}
		case 3:
			{
				cout << "Thanks for using this program \n\n";
				break;
			}

		}

	}while(option != 3);

	return 0;
}

double option1(double num1, double num2)
{
	return num1 + num2;
}
double option2(double num1, double num2)
{
	return num1 * num2;
}
Topic archived. No new replies allowed.