Calling function in switch statement, not all control paths return a variable

Not all control paths return a value in the int choose() function. I'm not sure what the issue is, any tips?

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
#include <iostream>
using namespace std;

   int choose(){
	char select;
	cin>>select;

	switch(select){
	case'i':int iCalculator(int x, int y);
		break;
	case 'd': double iCalculator(double dx, double dy);
		break;
	case 'q': exit(1);
		break;
	default:"Select a valid choice";
	}
}

int main(){
cout<<"Calculator Ready"<<endl;
cout<<"Enter i for integer, d for double and q to quit"<<endl;
cout<<choose;

system("pause");
return 0;
}


int iCalculator(int x, int y){
	cout<<"Enter first interger";
	cin>>x;
	cout<<"Enter Second Interger";
	cin>>y;
	cout<<"What operation do you want for your intergers?(+,-.*,/)";
	char operating;
	cin>>operating;
	int calculated=0;

	switch(operating){
	case'+': calculated=x+y;
		break;
	case '-': calculated=x-y;
	break;
	case '*': calculated=x*y;
	break;
	case '/': calculated=x/y;
		if(y==0){cout<<"Cannot divide by zero";
		iCalculator(x,y);};
	break;

	default: cout<<"look here genuis I gave you a choice, now choose a valid response, you dip.";
	break;
	}
	
	return calculated;
	
}

double iCalculator(double dx, double dy){
	cout<<"enter your first double: ";
	cin>>dx;
	cout<<"enter your second double: ";
	cin>>dy;
	cout<<"What operation do you want for your doubles?(+,-.*,/)";
	char operating;
	cin>>operating;
	double calculated=0.0;

	switch(operating){
	case'+': calculated=dx+dy;
		break;
	case '-': calculated=dx-dy;
	break;
	case '*': calculated=dx*dy;
	break;
	case '/': calculated=dx/dy;
	if(dy==0){cout<<"Cannot divide by zero";
		iCalculator(dx,dy);};
	break;

	default: cout<<"look here genuis I gave you a choice, now choose a valid response, you dip.";
	break;
	}
	
	return calculated;
}
To return a value from a function you have to use a return statement. On line 9 and 11 you are not calling the functions, you are simply declaring them. Calling iCalculator(int,int); would look something like this: iCalculator(0, 0); And if you want to return the value that the function return you put the return keyword in front of it, like this: return iCalculator(0, 0);

x and y (and dx and dy) shouldn't really be parameters because the initial values are not used by the function. Instead I think you should give the functions two different names (e.g. iCalculator and dCalculator) and declare x and y as normal variables inside the functions.
Last edited on
I cleaned it up a bit with your suggestions. It still give me the "not all control paths return a value" warning for the int choose function(line 82), and now a conversion data loss error at line 75. The program itself when run gives the statements in main(), but is followed by a random reference I think, and the "press any key to continue."

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
#include <iostream>
using namespace std;



double dCalculator(){
	double x;
	double y;
	cout<<"enter your first double: ";
	cin>>x;
	cout<<"enter your second double: ";
	cin>>y;
	cout<<"What operation do you want for your doubles?(+,-.*,/)";
	char operating;
	cin>>operating;
	double calculated=0.0;

	switch(operating){
	case'+': calculated=x+y;
		break;
	case '-': calculated=x-y;
	break;
	case '*': calculated=x*y;
	break;
	case '/': calculated=x/y;
	if(y==0){cout<<"Cannot divide by zero";
		dCalculator();};
	break;

	default: cout<<"look here genuis I gave you a choice, now choose a valid response, you dip.";
	break;
	}
	system("pause");
	return calculated;
	
}
int iCalculator(){
	int x;
	int y;
	cout<<"Enter first interger";
	cin>>x;
	cout<<"Enter Second Interger";
	cin>>y;
	cout<<"What operation do you want for your intergers?(+,-.*,/)";
	char operating;
	cin>>operating;
	int calculated=0;

	switch(operating){
	case'+': calculated=x+y;
		break;
	case '-': calculated=x-y;
	break;
	case '*': calculated=x*y;
	break;
	case '/': calculated=x/y;
		if(y==0){cout<<"Cannot divide by zero";
		iCalculator();};
	break;

	default: cout<<"look here genuis I gave you a choice, now choose a valid response, you dip.";
	break;
	}
	system("pause");
	return calculated;
	
}
int choose(){
	char select;
	cin>>select;

	switch(select){
	case'i':return iCalculator();
		break;
	case 'd': return dCalculator();
		break;
	case 'q': return 0;
		break;
	default:"Select a valid choice";
	}
	system("pause");
}



int main(){
cout<<"Calculator Ready"<<endl;
cout<<"Enter i for integer, d for double and q to quit"<<endl;
cout<<choose;

system("pause");
}
choose has to return an integer because you specified its return type as an int.

If the user inputs 'q', you will return 0
If the user inputs 'i', you will return whatever iCalculator returned.

But what do you return if the user gives invalid input? You don't specify. Hence the warning.
Last edited on
Thanks for all the help all I need left is to loop it, but I'll figure it out for myself. You guys are fantastic.

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
#include <iostream>
using namespace std;



double dCalculator()
{
	double x;
	double y;
	cout<<"enter your first double: "<<endl;
	cin>>x;
	cout<<"enter your second double: "<<endl;
	cin>>y;
	cout<<"What operation do you want for your doubles?(+,-.*,/)"<<endl;
	char operating;
	cin>>operating;
	double calculated=0.0;

	switch(operating){
	case'+': calculated=x+y;
		break;
	case '-': calculated=x-y;
	break;
	case '*': calculated=x*y;
	break;
	case '/': calculated=x/y;
	if(y==0){cout<<"Cannot divide by zero";
		dCalculator();};
	break;

	default: cout<<"look here genuis I gave you a choice, now choose a valid response, you dip.";
	break;
	}
	system("pause");
	cout<<calculated<<endl;
	return calculated;
	
	
}
int iCalculator(){
	int x;
	int y;
	cout<<"Enter first interger"<<endl;
	cin>>x;
	cout<<"Enter Second Interger"<<endl;
	cin>>y;
	cout<<"What operation do you want for your intergers?(+,-.*,/)"<<endl;
	char operating;
	cin>>operating;
	int calculated=0;

	switch(operating){
	case'+': calculated=x+y;
		break;
	case '-': calculated=x-y;
	break;
	case '*': calculated=x*y;
	break;
	case '/': calculated=x/y;
		if(y==0){cout<<"Cannot divide by zero";
		iCalculator();};
	break;

	default: cout<<"look here genuis I gave you a choice, now choose a valid response, you dip.";
	break;
	}
	cout<< calculated<<endl;
	system("pause");
	return calculated;
	
	
}
double choose(){
	char select;
	cin>>select;

	switch(select){
	case'i':return iCalculator();
		break;
	case 'd': return dCalculator();
		break;
	case 'q': return 0;
		break;

	default:"Select a valid choice"; }

	system("pause");
}



int main(){

cout<<"Calculator Ready"<<endl;
cout<<"Enter i for integer, d for double and q to quit"<<endl;
cout<< choose();


}
Topic archived. No new replies allowed.