switch statements

supcello (10)
Hey, ran into a little bit of a problem.
I'm making a menu that uses switch statements which uses function prototypes.
I guess my question is, do I have to write my switch before int main? or can it be included in my int main?
Fransje (178)
You can write it anywhere you like.
supcello (10)
Okay thanks. I'm having a problem with the switch menu. This is my code (not done).
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
#include <iostream>
using namespace std;

void rows(int);

int main()
{
	int option;
	cout << "**** Menu ****" << endl;
	cout << "(1) 100 x's" << endl;
	cout << "(2) celsius to fahrenheit" << endl;
	cout << "(3) fahrenheit to celsius" << endl;
	cout << "(4) servers tips" << endl;
	cout << "(5) quit" << endl;

	switch (option)
	{
		case 1:
			void rows();
			break;
		
		case 2:
			cout << "test" << endl;
			break;
	}

	char x;
	cin >> x;

	return 0;
}


void rows()
{
	int i,j,y;

	for (int i = 0; i < 10; ++i)
		{
		for (int j = 0; j < 10; ++j)
			cout << 'X';
			cout << endl;
		}
	
	
	int a;
	cin >> a;

}
Fransje (178)
You first have to get the value of option from the user: cin >> option; . Then you can start checking option . Otherwise option will have some garbage value.
Last edited on
supcello (10)
Duh, I knew that... Okay maybe another question that'll be a little harder to answer?
I select 1 and it just runs the program super quick and exits the console.
supcello (10)
By the way, thanks!
firedraco (5413)
That's because line 19 is not a call to the function rows, it is a random function prototype (which does nothing).
supcello (10)
LIttle confused, could you explain more?
Line 19 doesn't call rows because why?
firedraco (5413)
The same reason line 4 does not call rows.
supcello (10)
Does int main need to call rows? I thought it was 'prepped' for rows and then it could be called when case 1 was chosen?
supcello (10)
I figured out, why iit wasn't calling rows. Thanks.
supcello (10)
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>
using namespace std;

void rows();
void convert();

int main()
{
	int option;
	cout << "**** Menu ****" << endl;
	cout << "(1) 100 x's" << endl;
	cout << "(2) celsius to fahrenheit" << endl;
	cout << "(3) fahrenheit to celsius" << endl;
	cout << "(4) servers tips" << endl;
	cout << "(5) quit" << endl;
	cin >> option;

	switch (option)
	{
		case 1:
			rows();
			break;
		
		case 2:
			convert();
			break;
	}


	return 0;
}


void rows()
{
	int i,j,y;

	for (int i = 0; i < 10; ++i)
		{
		for (int j = 0; j < 10; ++j)
			cout << 'X';
			cout << endl;
		}

}


void convert()
{
	float celsius, fahrenheit;
	cout << "enter the temperature" << endl;
	cin >> fahrenheit;

	fahrenheit = ((9.0/5.0) * celsius) + 32.0;
	cout << fahrenheit;
	
}


I get an error after entering a number for the convert()
Registered users can post here. Sign in or register to post.