Returning function within a function (if you know what I mean)

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <string>
#include <math.h>

using namespace std;

int addNum();
int multiNum();
int subNum();
int divNum();
int sqroot();
float powrNum();
double circCalc();
double circArea();
double circCircum();
float DegToRad();

int num1, num2;
int sum;
char userInput;
char mathOp;
double radius, area, circum, diam;
long double pi = 3.141592653589793238462643383279502884197169399375;


int main()
{
	do
	{
		 cout << "Please choose a Calculation you want to do: \nAddition - 1 \nMultiplication - 2 \nSubtraction - 3 \nDivison - 4 \nPower - 5 \nSquare Root - 6 " << endl;
			cout << "Cirlce Calculations - 7 \nDegree to Radian - 8" << endl;
		cin >> userInput;

		switch(userInput)
		{
		case '1' : addNum();
			break;
		case '2' : multiNum();
			break;
		case '3' : subNum();
			break;
		case '4' : divNum();
			break;
		case '5' : powrNum();
			break;
		case '6' : sqroot();
			break;
		case '7' : circCalc();
			break;
		case '8' : DegToRad();
			break;
		}

		cout << "\nWould you like to do another operation? (y/n)" << endl;
			cin >> userInput;	
	}while(userInput != 'n'); //just to be awkward :D

}

double circCalc()
{
	cout << "You chose circle calculations! " << endl;
		cout << "What do you want to do? " << endl;
			cout << "Area - 1 \nCircumfrence - 2" << endl;
	cin >> userInput;

	switch(userInput)
	{
		case '1' : circArea();
			break;
		case '2' : circCircum();
			break;
		default:
			cout << "Error - Invalid Input! " << endl;
	}

	return circArea();
	return circCircum();
}

double circArea()
{
	cout << "You chose area! " << endl;
		cout << "Enter the radius of your cirlce: " << endl;
			cin >> radius;
	area = pi * (radius * radius);

	cout << "= " << area;

	return area;
    
}

double circCircum()
{
	cout << "You chose cirumfrence! " << endl;
		cout << "Enter the diameter of your circle: " << endl;
	circum = pi * diam;

	cout << "= " << circum;

	return circum;
}

(For the sake of space I've removed all my other functions)
So I made/making a scientific calculator, so I thought I would practice with some basic functions; I put the program into functions, all went well till I tested it and came to the circle calculations.
If you look at line 77 and 78, which ever function is at the top, it will output that after the current function has just been done.
Last edited on
This is the wrong way to ask a question.
You have to make an effort, by isolating the issue, and asking a concise question while posting only relevant code.

Anyway, you can only return a single value out of a function. Which means that you can't have multiple returns. In your case, the second return circCircum(); is ignored.

In your case, again, the solution is simple: simply store the return values of your circArea() and circCirum() in a variable, and return the variable instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
double circCalc()
{
	cout << "You chose circle calculations! " << endl;
		cout << "What do you want to do? " << endl;
			cout << "Area - 1 \nCircumfrence - 2" << endl;
	cin >> userInput;

	double r; // stores either circArea() or circCircum()

	switch(userInput)
	{
		case '1' : r = circArea();
			break;
		case '2' : r = circCircum();
			break;
		default:
			cout << "Error - Invalid Input! " << endl;
	}

	return r;
}

Thanks for that :D
You have to make an effort
While posting this I actually had about 3 problems I wanted to ask, but I fixed them on my own, and due to this I spent about 10 minutes trying work out the current problem. So you can't really say that I was making no effort to work out the problem without me actually saying that I made no effort. Also, I thought that it clearly shows that I was implying for help with that certain piece of code.
I never post for any help on here until I've spent at least 10 minutes trying to work out the problem by myself.
You're missing the point. You never explained what the "problem" is in the first place.
If you look at line 77 and 78, which ever function is at the top, it will output that after the current function has just been done.
If you call the function, it's kind of obvious that it will be called. What did you expect to happen or what do you want to happen instead?
Sorry, I see where you're coming from. I feel that I was struggling to word the question for some reason
Topic archived. No new replies allowed.