Issues with a function

I am having issues with one of my functions. it is the option function i am using it to call my switch statement. but I cant seem to be able to get it to work it tells me that the function needs a pointer-to-function.

Yes my program is incomplete but, I am testing as I write to make sure certain elements are working 100% before i move on that way I'm not overloaded with issues later.

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

#include<iostream>
#include<string>
#include<Windows.h>

using namespace std;

void veggies();
void meats();
void menu();
void option(int);

void main()
{
	int option;
	



	//Sleep(120);
	
	cout<<"Hello and welcome to My Grocery List Assistant"<<endl;
	cout<<"How May the Grocery Assistant be of help to you?"<<endl;
	void menu();

	
/*	switch(option)
	{
	case 1:
		{
			system("CLS");

			cout<<"\tWhat would you like to Find?"<<endl<<endl;
			cout<<"1.Prices"<<endl;
			cout<<"2.Available items"<<endl;
			cout<<endl;
			cin>>option;
			if(option == availableItems)
			{
				cout<<"To Be Continued.....";
			}


		}
	case 2:
		{
			system("cls");


		}

	}
	*/

	system ("pause"); 
}

void menu()
{
	int option;

	cout<<"\t\tGrocery List"<<endl<<endl;
	cout<<"1.Find Food"<<endl;
	cout<<"2.Create List"<<endl;
	cout<<"3.Exit"<<endl<<endl;
	cin>>option;

	option(option);

}

void option(int option)
{
	int const availableItems = 1;
	int const menu = 3;

	switch(option)
		{
		case 1:
			{
				system("CLS");

				cout<<"\tWhat would you like to Find?"<<endl<<endl;
				cout<<"1.Prices"<<endl;
				cout<<"2.Available items"<<endl;
				cout<<"3.Return to menu"<<endl;
				cout<<endl;
				cin>>option;
				if(option == availableItems)
				{
					cout<<"To Be Continued.....";
				}

				else if(option == menu)
				{
					main();
				}


			}
		case 2:
			{
				system("cls");


			}

		}
}
Last edited on
main();
Never ever call main()
This indicates that you don't understand loops (or perhaps returning from functions), and this will be a major problem in the future. Learn about loops (or perhaps returning from functions) now.

Line 24 is the same as line 10. Remove line 24.


it tells me that the function needs a pointer-to-function.

What tells you this? In what way does it "need" a pointer-to-function?
Last edited on
I understand to an extent, I was just never told to call main I figured I may have an issue but since I didn't know I figured I would try it out and see what happened. so if I replace that with an exit() function would that be better ?

and where i try to pass the value of option which will be input by the end user.
1
2
3
4
5
6
7
8
9
10
11
12
13
void menu()
{
	int option;

	cout<<"\t\tGrocery List"<<endl<<endl;
	cout<<"1.Find Food"<<endl;
	cout<<"2.Create List"<<endl;
	cout<<"3.Exit"<<endl<<endl;
	cin>>option;

	option(option);

}


it tells me the function needs a poitner-to ?
I have fixed the issue though. it was because I was using the same name as the locally declared int variable which I had also named option. now my function prototype reads

 
void menuOption(int);


and my function call reads

 
menuOption(option)


but I ran it while calling main and it worked just fine. how dod i exit the function without calling main or should just go back and call my menu again?
this is what I now have. please let me know if this is ok or not
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
//Farncisco Diaz
//May 23, 2016
//CIS-17A: C++ Programming

#include<iostream>
#include<string>
#include<Windows.h>

using namespace std;

void veggies();
void meats();
void mainMenu();
void menuOption(int);

void main()
{
	
	//Sleep(120);
	
	mainMenu();

	

	system ("pause"); 
}

void mainMenu()
{
	int option;

	cout<<"Hello and welcome to My Gorcery List Assistant"<<endl;
	cout<<"How May the Grocery Assistant be of help to you?"<<endl<<endl;
	cout<<"1.Find Food"<<endl;
	cout<<"2.Create List"<<endl;
	cout<<"3.Exit"<<endl<<endl;
	cin>>option;

	menuOption(option);

}

void menuOption(int option)
{
	int const availableItems = 1;
	int const menu = 3;

	switch(option)
		{
		case 1:
			{
				system("CLS");

				cout<<"\tWhat would you like to Find?"<<endl<<endl;
				cout<<"1.Prices"<<endl;
				cout<<"2.Available items"<<endl;
				cout<<"3.Return to Main Menu"<<endl;
				cout<<endl;
				cin>>option;
				if(option == availableItems)
				{
					cout<<"To Be Continued.....";
				}

				else if(option == menu)
				{
					system("CLS");
					mainMenu();
				}


			}
		case 2:
			{
				system("cls");


			}

		}
}
Last edited on
Understand that you're calling mainMenu() recursively. Yes, recursion works for this, but IMO, there are times to use recursion and this is not one of them.

Line 68: Instead of calling mainMenu() here, simply return to the caller. Now you can bracket lines 33-39 in a simple loop. Suggestion, make menuOption() a bool function. If it returns true, continue to loop. If it returns false, exit your loop and return to main().

Last edited on
thank you very much.
Topic archived. No new replies allowed.