使用getline()填充结构字段

问题描述 投票:0回答:1

大家好,我是这个论坛的新手,我想就我编写的代码寻求一些建议。我仍在学习如何编码,我的老师要求我用字符串字段创建一个静态分配的结构体数组:我的问题是我真的不知道如何使用getline()来填充它,所以我得到了我的IDE中出现错误:[错误]没有匹配的函数调用'std :: basic_istream :: getline(std :: string [16]这是我用来定义结构的代码:

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


还有我用来填充的功能:

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;
    }
}


很抱歉,可能会有一些拼写错误,但是英语不是我的主要语言,所以我尝试翻译代码,以使您更容易理解它。

P.S。我的IDE是Dev-C ++

c++ compiler-errors getline
1个回答
0
投票

您已将name声明为16个std::string s的数组,并且没有getline重载来读取16个std::string s的数组。

您想要string namechar name[16](我会选择前者)。

© www.soinside.com 2019 - 2024. All rights reserved.