Passing a function value into a switch statement

Having an issue getting this to work properly. I had it working with the switch statement within the function, but later instructions said that it must be inside of main. I just get a loop no matter what I do. 5 needs to exit the program, all the other selections need to display the corresponding cout statements. I've done something to where the inputs are not actually passing into the switch statement. If any value other than 1-5 is put in, the menu should repeat and prompt the user again. Thanks in advance.

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
#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

int call_menu(int selection);
double kilo_m(double miles);

int main()
{	
	int selection, number;

	do 
	{
	call_menu(selection);
	} while (selection<1||selection>5);
	number = call_menu(selection);
	cout << number << endl;
	switch (number) 
	{
		case 1:
			cout << "You selected the Miles to Kilometers option.\n";
			cout << endl;
			break;
		case 2:
			cout << "You selected the BMI option.\n";
			cout << endl;
			break;
		case 3:
			cout << "You selected the Distance Between Two Points option.\n";
			cout << endl;
			break;
		case 4:
			cout << "You selected the Taylor Series option.\n";
			cout << endl;
			break;
		case 5:
			return 0;
		}
	return true;
	
	return 0;
}

	int call_menu (int selection)
	{
	int choice, result;
	cout << " 1. Miles to Kilometers\n";
	cout <<	" 2. BMI\n";
	cout <<	" 3. Distance Between Two Points\n";
	cout <<	" 4. Taylor Series\n";
	cout <<	" 5. Quit\n";
	cout <<	endl;
	cout <<  " Enter an operation number : ";
	cin >> choice;
	result = choice;
	return (result);
	}
Your main should look like this -

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
        int selection = 0;
	int number = 0;
	bool run = true;

	do
	{

	    number = call_menu(selection);

		switch (number)
		{
		case 1:
			cout << "You selected the Miles to Kilometers option.\n";
			cout << endl;
			break;
		case 2:
			cout << "You selected the BMI option.\n";
			cout << endl;
			break;
		case 3:
			cout << "You selected the Distance Between Two Points option.\n";
			cout << endl;
			break;
		case 4:
			cout << "You selected the Taylor Series option.\n";
			cout << endl;
			break;
		case 5:
			run = false;
		}


		} while (run == true);


and your call_menu function should look like this -

1
2
3
4
5
6
7
8
9
10
11
12
int call_menu(int selection) 
{
	cout << " 1. Miles to Kilometers\n";
	cout << " 2. BMI\n";
	cout << " 3. Distance Between Two Points\n";
	cout << " 4. Taylor Series\n";
	cout << " 5. Quit\n";
	cout << endl;
	cout << " Enter an operation number : ";
	cin >> selection;
	return selection;
}


If you want a decent explanation PM Me.
Last edited on
Much appreciated. I had it set up with booleans before, but when I rearranged it I was uncertain of how to modify them.
Topic archived. No new replies allowed.