0

Hi guys I'm new to this forum and I wanted to ask for some advice on the code that I wrote. I'm still learning how to code and my teacher asked me to create a static allocated array of structs with a string field: my problem is that i don't really know how to use getline() to fill it and so I get an error from my IDE:
[Error] no matching function for call to 'std::basic_istream::getline(std::string [16]

Here is the code that i used to define the struct:

typedef struct{
    string name[16];
    int price;
}dish;


And there is the function that I used to fill it:

void insertDish(dish menu[], int fill){
    for(int i=0; i<fill; i++){
        cout<<"Enter name and price of dish "<<(i+1)<<": "<<endl;
        cout<<"Name: ";
        cin.getline(menu[i].name);

        cout<<"Price: ";
        cin>>menu[i].price;
        cout<<endl;
    }
}


I'm sorry if there will be some misspelling but English isn't my main language and I tried to translate the code so that it's easier for you to understand it.

P.S. My IDE is Dev-C++

Gory
  • 1
  • 2
  • 1
    Please be more specific than "I get an error from my IDE". – molbdnilo Dec 18 '19 at 10:39
  • Whatever your error is, you should definitely read [Why does `std::getline()` skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – molbdnilo Dec 18 '19 at 10:40
  • @molbdnilo yes you're right, my error is ::getline(std::string [16])'> – Gory Dec 18 '19 at 10:49
  • Please add your error message to the question. – molbdnilo Dec 18 '19 at 10:54

1 Answers1

0

You have declared name as an array of sixteen std::strings, and there is no getline overload for reading an array of sixteen std::strings.

You want either string name or char name[16] (I would pick the former).

Also, the member getline only deals with char*.

You need the "free" getline:

std::getline(std::cin, menu[i].name);
molbdnilo
  • 55,783
  • 3
  • 31
  • 71