prototype in my function is not declared

#include<iostream>
#include<string>
#include <iomanip>
using namespace std;


int menu(int choice);

int main()
{
menu(choice);

do
{

switch(choice)
{
case 1:
cout<<"welcome to 1";
break;

case 2:
cout<<"welcome to 2";
break;
case 3:
cout<<"The program will end";
break;
}
}while(choice !=3);



return 0;
}



int menu(int choice) //void function that displays the menu, ask users
{ //to input their choice, then reference the choice to the main function.
cout<<"first option "<<endl;
cout<<"second option"<<endl;
cout<<"third option "<<endl;
cout<<"Your choose option: ";
cin>>choice;
return choice;
}


The problem is the line "menu(choice) " tells me it is not declared, but it is in my function.
choice is the name of the parameter in int menu(int choice)
In main you need to create a variable that you can pass to the menu function.
I need help in writing a C++ Program to read grades from the keyboard (Math and Physics ).
Hello zxkun,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

To add to what Thoas165 said the function "menu" returns an int so you will need a variable to capture the return value or pass "choice" by reference, i.e., int menu(int& choice). this will allow you to change the value of "choice in main. Or you would need to call "menu" with choice = menu(choice). Either way you would need to define "choice" in main before you use it.

Hope that helps,

Andy
Thomas1965,


I add int choice=0; before the menu(choice).
Now my program can run, but it stops at the switch.
When I input 1,2,or 3 ,and then enter , the program just stops.

The comment doesn't agree with the code:
 
int menu(int choice) // void function that displays the menu, 


You need to decide whether the function return value is int or void, it can't be both.

If you want to use void here, then the parameter choice would need to be passed by reference. That would make the declaration like this:
 
void menu(int & choice)

Notice the & symbol.

On the other hand if the function is to return an int, there is no need for a parameter at all. The declaration could look like this:
 
int menu()

closed account (48T7M4Gy)
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>
// #include <string> // ???
// #include <iomanip> // ???

using namespace std;

int menu(); // <---

int main()
{
    int selection = 0; // <---
    
    do
    {
        selection = menu(); // <---
        cout << endl; // <---
        switch(selection)
        {
            case 1:
                cout<<"welcome to 1";
                break;
                
            case 2:
                cout<<"welcome to 2";
                break;
                
            case 3:
                cout<<"The program will end\n"; // <---
                break;
                
            default: // <---
                cout << "Invalid selection\n"; // <---
                break; // <---
        }
    }while(selection !=3);
    
    return 0;
}

int menu() // <---
{ //to input their choice, then reference the choice to the main function.
    int choice = 0; // <---
    cout << endl; // <---
    cout<<"first option "<<endl;
    cout<<"second option"<<endl;
    cout<<"third option "<<endl;
    cout<<"Your choose option: ";
    cin>>choice;
    return choice;
}
Topic archived. No new replies allowed.