-2

I am currently away from my studies on holiday and am trying to keep up with the work where I can. I have a little experience in C# however C++ is new to me so I am having a little trouble using functions & param passing.

Visual Studio throws an error at me at "menu(selection);" detailing that 'menu': identifer not found, error:CS3861. Any advice/tips would be most appreciated. Here is my code so far:

using namespace std;
int main()
{
    // print msg
    cout << "This program will allow you to select an option 1-4.\nSelect an option to see the output.";
    // declare selection - used to get desired user input
    int selection;
    //call menu
    menu(selection);
}

void menu(int selection)
{
    cout << "Please select an option:" << endl;
    cout << "1. op1\n2. op2\n3. op3\n4. Exit program";
    cin >> selection;
    while (selection < 1 || selection>4)
    {
        cout << "Selection invalid, please choose an option from 1-4";
    }
}
Rennob
  • 9
  • 2
    you cant call a function before you declare it – 463035818_is_not_a_number Oct 15 '18 at 10:22
  • 3
    Please copy and paste error messages, don't paraphrase them. – molbdnilo Oct 15 '18 at 10:23
  • 1
    There is a list of good books [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Oct 15 '18 at 10:23
  • The simplest fix is to move your main function to the bottom of the file, so that menu comes first. So when the compiler gets to the menu call in main it already knows what menu is. Relevant reading: https://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration – Rup Oct 15 '18 at 10:26

2 Answers2

1

There are multiple problems with your code.

The problem you are posting about you can solve by declaring the function before using it. The c++ compiler first encounters the function "menu" in your main funciont, but you define (and declare) it later, hence in your main function, it isn't known what function should be called.

using namespace std;

void menu(int); // << forward declaration of the menu function

int main()
{
    // print msg
    cout << "This program will allow you to select an option 1-4.\nSelect an option to see the output.";
    // declare selection - used to get desired user input
    int selection; 
    //call menu
    menu(selection);
}

In the fragment of your main function, your menu function isn't declared. You can solve that by using the forward declaration of your function, see the comment above. A more nuanced error is that you do not assign a value to the variable selection. This means that menu can (will) be called with with an unanticipated value.

hetepeperfan
  • 3,787
  • 1
  • 23
  • 42
1

As others have pointed out, before calling a function, it must be declared, so either move the function definition to the top of the program or do a forward declaration. It looks like you also want the selection made in the menu() function to be available in main(), so instead of passing the selection variable by value, pass it by reference.

#include <iostream>

using namespace std;

void menu(int& selection); // forward declaration

int main()
{
    // print msg
    cout << "This program will allow you to select an option 1-4.\nSelect an option to see the output.";
    // declare selection - used to get desired user input
    int selection;
    //call menu
    menu(selection);
}

void menu(int& selection) // function definition
{
    cout << "Please select an option:" << endl;
    cout << "1. op1\n2. op2\n3. op3\n4. Exit program";
    cin >> selection;
    while (selection < 1 || selection>4)
    {
        cout << "Selection invalid, please choose an option from 1-4";
    }
}
Ted Lyngmo
  • 37,764
  • 5
  • 23
  • 50