-1

I'm working on a program that I've seen other people do online except I'm trying to use functions to complete it to make it somewhat more challenging for me to help me better understand pointers and vectors. The problem I'm having in xcode is I keep getting this error.. Expected ';' after top level declarator

right here on my code,

void showMenu(menuItemType (&menu_List)[8])[], vector<int> numbers) //<<< Error

{

  cout << fixed << setprecision(2);

...

Where I am trying to use vector numbers in my function. Basically I want the numbers from the function passed back so that I can use them in another function I have not created yet. I've googled this error and it seems like no one can give a straight answer on how to fix this problem. Is anyone familiar with how to correct this? By no means is this code finished I'm just trying to get information regarding vectors as a parameter because from what I'm seeing syntax wise on other sites it looks to be correct. Thanks for your feedback.

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
#include <iterator>



using namespace std;

struct menuItemType{
    string menuItem;
    double menuPrice;
};

void getData(menuItemType (&mlist)[8]);
void showMenu(menuItemType (&menu_List)[8], vector<int> numbers);


int main() {
    vector<int> temp;
    
    menuItemType menuList[8];
    
    
    
    getData(menuList);

    
    showMenu(menuList,temp);
    
    
    
    /*
    cout << menuList[0].menuItem << " " << menuList[0].menuPrice << endl;
    cout << menuList[1].menuItem << " " << menuList[1].menuPrice << endl;
    */
    
    return 0;
}

void getData(menuItemType (&mlist)[8]){
    string Str;
    
    ifstream infile;
    
    infile.open("cafe135.txt");
    
    if(infile.is_open())
    {
        for (int i = 0; i < 8; ++i){
            infile >> mlist[i].menuItem >> mlist[i].menuPrice;
        }
    }
    else cout << "Unable to open file";
}

void showMenu(menuItemType (&menu_List)[8])[], vector<int> numbers)
{
    cout << fixed << setprecision(2);
    
    string choice;
    
    cout << "Would you like to view the menu? [Y] or [N]: ";
    cin >> choice;
    
    cout << endl;
    
    int x = 3;
    int count = 1;
    
    while (choice != "Y" && choice != "N" && choice != "y" && choice != "n")
    {

        if (count == 4){
            return;
        }
        cout << "Error! Please try again ["
             << x
             << "] selections remaining: ";
        cin >> choice;
        
        cout << endl;
        x--;
        count++;
        
        
    }
    
    if (choice == "N" || choice == "n"){
        
        return;
    }
    else
    {
        cout << "___________ Breakfast Menu ___________" << endl;
        
        for (int i = 0; i < sizeof(menu_List)/sizeof(menu_List[0]); ++i)
        {
            
            cout << "Item "
                 << (i+1)
                 << ": "
                 << menu_List[i].menuItem
                 << " "
                 << menu_List[i].menuPrice
                 << endl;
        }
        
        cout << endl;
        
        string itemSelection = " ";
        //int str_length = 0;
        
        cout << "Select your item numbers separated"
             << " by spaces (e.g. 1 3 5) Select 0 to cancel order: ";
        
        cin.ignore();
        
        getline(cin, itemSelection);
        
        if (itemSelection == "0")
        {
            return;
        }
        
        vector<int> vectorItemSelection;

        stringstream text_stream(itemSelection);
        
        string item;
        
        while (getline(text_stream, item, ' '))
        {
            vectorItemSelection.push_back(stoi(item));
        }
        
        int n = vectorItemSelection.size();

        int arr[n];
        for (int i = 0; i < n; i++)
        {
                arr[i] = vectorItemSelection[i];
        }
    }
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 1
    what is this supposed to be `menuItemType (&menu_List)[8])[]` ? Why are you not using `std::array` or `std::vector` ? – 463035818_is_not_an_ai Aug 11 '22 at 18:54
  • 1
    oh, you are using a mix of `std::vector`, c-arrays and variable length arrays. Note that `int arr[n];` is not standard C++ [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – 463035818_is_not_an_ai Aug 11 '22 at 18:56
  • You declared showMenu as `void showMenu(menuItemType (&menu_List)[8], vector numbers)`, and implemented as `void showMenu(menuItemType (&menu_List)[8])[], vector numbers)` . The latter isn't even valid C++, and clearly doesn't match the prior formal declaration regardless. So, unless your intent was overloading, and I *seriously* doubt that to be the case, that's a fundamental problem. Why did you hang that spare pair of `[]` on that function parameter in the first place? – WhozCraig Aug 11 '22 at 19:21

1 Answers1

0

Compare how menu_List is declared in this line

void showMenu(menuItemType (&menu_List)[8], vector<int> numbers);

and this line

void showMenu(menuItemType (&menu_List)[8])[], vector<int> numbers)

The first one is correct.

But I have to agree with the comments above, you are mixing up a lot of different things here. Just use vectors, 99% of the time it's the right thing to do anyway. and it's easier to learn one thing at a time.

Prefer to write your code like this

void getData(vector<menuItemType>&);
void showMenu(vector<menuItemType>&, vector<int> numbers);


int main() {
    vector<int> temp;
    
    vector<menuItemType> menuList(8);

...

See? Just use vectors everywhere.

john
  • 85,011
  • 4
  • 57
  • 81