Switch menu does not call function!!

Apr 19, 2014 at 4:41am
The switch menu does not call the function.
compiler says undefined function!! Why???
Is the description on top of the functions right for doxygen?? makes sense??
Thanks for looking into it!!
have a nice Easter!!

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  /**************
Author: Bla bla
Date: 17/04/2014
Description: Menu program w/ 4 option, for lab 7, question 2.
A-Display name. B-Display tuto time. Q-Quit
C- Read in a positive number between 1-50 and
display all numbers from 0 up to the number entered
**************/
#include <stdio.h>
#include <ctype.h>
/*************
Description: display_menu (only displays menu)
Parameters in: N/A
Parameters out: N/A
Return: N/A
*************/
void display_menu (void)
{
	printf("You have 4 choices.");
	printf("Enter a for my name.");
	printf("Enter b for my tutorial date.");
	printf("Enter c to read a positive number & display all numbers.");
	printf("Enter q to quit.\n");
	
	return;
}

/*************
Description: get_choice 
Parameters in: N/A
Parameters out: response
Return: Users choice.
*************/
char get_choice (void)
{	
	char response;
	
	scanf("%c%*c",&response);
	response = tolower(response);
							
	return(response);
}

/*************
Description: menu 
Parameters in: choice
Parameters out: N/A
Return: Doesnt return, process the user choice.
*************/
void performe_action (char choice)
{		
	switch (choice)
	{
		case 'a':
			print_name();
			break;
		case 'b':
			print_lab();
			break;
		case 'c':
			get_calculate_number();
		default:
			printf("Invalid input\n");	
	}
	
	return;
}

/*************
Description: Print function - name
Parameters in: N/A
Parameters out: N/A
Return: N/A
*************/
void print_name (void)
{
	printf ("My name is bla bla");
	
	return;
}

/*************
Description: Print function - lab time
Parameters in: N/A
Parameters out: N/A
Return: N/A
*************/
void print_lab (void)
{
	printf ("My lab is on bla bla");
	
	return;
}

/*************
Description: get and calculate number
Parameters in: get number from function get_number
Parameters out: print the result from print_number
Return: N/A
*************/
void get_calculate_number (void)
{
	int number;
			
	number = get_number();
	print_number(number);
	
	return;
}	

/*************
Description: get a number
Parameters in: N/A
Parameters out: number choice
Return: N/A
*************/
int get_number (void)
{
	int number;
	
	printf ("What is your number");
	scanf ("%d%*c", &number);
		
	return(number);
}

/*************
Description: get a number
Parameters in: N/A
Parameters out: number choice
Return: N/A
*************/
void print_number(int number)
{
	int x;
	
	for(x=1; x<=number; x++)
	{
		printf ("The result is %d", x);
	}

	return;
}

int main()
{
	char choice;
	
	do
		display_menu();
		choice = get_choice();
		performe_action(choice);
	while(choice != 'q')	

	return(0);
}
Last edited on Apr 19, 2014 at 4:42am
Apr 19, 2014 at 4:45am
You are missing braces here:

1
2
3
4
5
do {
    display_menu();
    choice = get_choice();
    performe_action(choice);
} while (choice != 'q'); // and semicolon. 
Apr 19, 2014 at 5:11am
thanks, but i think that is not the reason its not working!!
Thanks
Apr 19, 2014 at 5:23am
Your code works for me here:
http://ideone.com/O8YpEN

It seems that your compiler can't locate the header files.
Last edited on Apr 19, 2014 at 5:24am
Apr 19, 2014 at 5:30am
choice = get_choice(); this will not work.

try this.


1
2
3
4
5
6
do
          {
		display_menu();
		cin >> choice;
		performe_action(choice);
	while(choice != 'q')	
Apr 19, 2014 at 5:31am
I figured it out!!!

At C, when we call a function, it has to be on top the function which is calling.
Thats the reason main(), has to be down on the bottom!!!

At least it worked at my program!!
Is it right??
Apr 19, 2014 at 5:44am
Yes I think you are right.
Good job..!
Apr 19, 2014 at 5:47am
A tip from me would be to always prototype your functions above(outside) of main and then define them below(outside) of main. This will clean up your code and save you a hassle.
Apr 22, 2014 at 5:42am
Thanks guys, those were great ideas!! thanks for he help!! :)
Topic archived. No new replies allowed.