Student Programmer help with error in function calls.

Nov 15, 2013 at 12:14am
I am writing a program using functions and I am already to compile when I get a lot of these errors. Can anyone tell what it means?

Error 2 error C2601: 'groceriesMenu' : local function definitions are illegal c:\users\kimberly\documents\visual studio 2012\projects\practice 14\practice 14\source.cpp 98 1 Practice 14
Nov 15, 2013 at 12:37am
At a guess, you have tried to define one function inside another function, which isn't valid in C++.
Nov 15, 2013 at 1:25am
i decided to post the code i have. So you can see what i am dealing with. We are to write a program using functions. Our model is a store with three departments in those departments are three more options to choose from.
#include <iostream>
#include <string>

using namespace std;

int main()
{
//Function prototype
void displayMenu();
int getChoice();
void groceryMenu();
void clothingMenu();
void produceMenu();




// Constant doubles
const double apples =.59, oranges = .79, bananas = .29;
const double dresses = 15.00, jeans = 9.00, tshirts = 3.50;
const double beef = 1.50, pork = 1.75, chicken = .89;
char again;
int choice;
double lbs, num, cost, price;

do
{ displayMenu(); // Displays main menu.
choice = getChoice();
cout << "Would you like to try again?";
cin >> again;
}while(again == 'y' || again == 'Y');

system("pause");
return 0;
/****************************************************
* Display Menu *
* This Function clears the screen and then diplays *
* the menu choices. Then using if statements for *
* the users choice and calls the correct function. *
****************************************************/
void displayMenu()
{
system("cls");
cout << "Welcome to Your Community Grocery Store" << endl;
cout << "What area will you be shopping in today?"<<
cout << "1. Grocery" << endl;
cout << "2. CLothing" << endl;
cout << "3. Produce" << endl;
cout << "4. Quit" << endl;
cin >> choice;

if (choice == 1)
{void groceriesMenu();
}
if (choice == 2)
{void clothingMenu();
}
if (choice == 3)
{void clothingMenu();
}
}


/************************************************
* Get Choice
* This function inputs , validates, and returns *
the user's menu choice. *
************************************************/
int getChoice();
{
int choice;
cin >> choice;
while (choice < 1 || choice >4)
{ cout << "The only valid choices are 1-4." << endl;
cin >> choice;
}
return choice;
}
/******************************************
* Grocery menu *
* This function diplays the groceries *
* menu. *
******************************************/
void groceriesMenu();
{system("cls");
cout << "You have chosen the groceries menu." << endl;
cout << "Choose One:" << endl;
cout << "1. Beef" << endl;
cout << "2. Pork" << endl;
cout << "3. Chicken " << endl;
cin >> choice;

if (choice ==1)
{cout << " Beef is $1.50 a pound. How many pounds would you like?" << endl;
cin >> lbs;
cost = beef * lbs;
price = cost * .07;
cout << "Your cost is $" << price << ". This isnludes a 7% sales tax." << endl;
}
if (choice == 2)
{cout << "Pork is $1.75 a pound. How many pounds would you like?" << endl;
cin >> lbs;
cost = pork * lbs;
price = cost * .07;
cout << "The cost is $" << price << ". This incudes a 7% sales tax." << endl;
}
if (choice == 3)
{cout << "Chicken is $0.89 a pound. How many pounds would you like?" << endl;
cin >> lbs;
cost = chicken * lbs;
price = cost * .07;
cout << "The cost is $" << price << ". This incudes a 7% sales tax." << endl;
}}

/************************************************
* Get Groceries Choice *
* This function inputs , validates, and returns *
* the user's menu choice. *
************************************************/
void getMenuChoice()
{
int choice;
cin>> choice;
while (choice < 1 || choice >3)
{ cout << "The only valid choices are 1-3" << endl;
cin >> choice;
}
}

/*********************************************
* Clothing menu *
* This function diplays the clothing menu. *
*********************************************/
void clothingMenu()
{system("cls");
cout << "You have chosen the clothing menu." << endl;
cout << "Choose one:" << endl;
cout << "1. Dresses" << endl;
cout << "2. Jeans" << endl;
cout << "3. T-shirts" << endl;
cin >> choice;

if (choice == 1)
{ cout << "Dresses are $15.00 each. How many would you like to purchase?" << endl;
cin >> num; // The number of dresses the user wishes to purchase.
cost = dresses * num;
price = cost * .07;
cout << "The cost is $" << price << ". This incudes a 7% sales tax." << endl;
}
if (choice == 2)
{cout << "Jeans are $9.00 each. How many would you like to purchase?" << endl;
cin >> num;
cost = jeans * num;
price = cost *.07;
cout << "The cost is $" << price << ". This includes a 7% sales tax." << endl;
}
if (choice == 3)
{cout << "The T-shirts are $3.50 each. How many would you like to purchase?" << endl;
cin >> num;
cost = dresses * num;
price = cost * .07;
cout << "The cost is $" << price << ". This includes a 7% sales tax." << endl;
}}

/***********************************************
* Produce Menu *
* This function displays the Produce menu. It *
* gives the user shopping ooptions and allows *
* user input. *
***********************************************/
void produceMenu()
{system("cls");
cout << "You have chosen the clothing menu." << endl;
cout << "Choose one:" << endl;
cout << "1. Apples" << endl;
cout << "2. Oranges" << endl;
cout << "3. Bananas" << endl;
cin >> choice;

if (choice == 1)
{cout << "Apples are $0.59 a pound. How many pounds would you like?" << endl;
cin >> lbs;
cost = apples * lbs;
price = cost *.07;
cout << "The cost is $" << price << ". This includes a 7% sales tax." << endl;
}
if (choice == 2)
{cout << "Oranges are $0.79 a pound. How many pounds would you like?" << endl;
cin >> lbs;
cost = oranges * lbs;
price = cost * .07;
cout << "The cost is $" << price << ". This includes a 7% sales tax." << endl;
}
if (choice == 3)
{cout << "Bananas are $0.29 a pound. How many pounds would you like?" << endl;
cin >> lbs;
cost = bananas * lbs;
price = cost * .07;
cout << "The cost is $" << price << ". This includes a 7% sales tax." << endl;
}}

}
Nov 15, 2013 at 2:00am
A bit of indentation would help to make that legible, aided by the use of code tags, [code] your code here [/code]

But the answer is as I surmised previously, you've defined the other functions inside of main(). Your code is a bit like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    // Function prototype
    void displayMenu();

    // Variables 
    const double apples =.59;
    
    // some code
    return 0;

    
    // function definition
    void displayMenu()
    {
        // etc
    }
} 


Whereas it should look more like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Function prototype
void displayMenu();

int main()
{
    // Variables 
    const double apples =.59;
    
    // some code
    return 0;
}

// function definition
void displayMenu()
{
    // etc
} 
Nov 15, 2013 at 4:24pm
I changed it like you suggested but it is still telling me local function definitions are illegal.
Nov 15, 2013 at 4:35pm
Could you post the updated version of your code please.
Topic archived. No new replies allowed.