c++ - 用getline()填充结构字段

标签 c++ compiler-errors getline

嗨,大家好,我是这个论坛的新手,我想就我编写的代码寻求一些建议。
我仍在学习如何编码,我的老师要求我创建一个带有字符串字段的静态分配结构数组:我的问题是我真的不知道如何使用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;
    }
}



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

附言我的IDE是Dev-C++

最佳答案

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

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

另外,成员getline仅处理char*

您需要“免费”的getline:

std::getline(std::cin, menu[i].name);

关于c++ - 用getline()填充结构字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59390137/

相关文章:

c++ - 在模板类中使用 "this"关键字

c++ - 从 cin 读取 getline 到字符串流 (C++)

c++ - c++ 中的 ifstream - 编译器问题

c++ - 无法检索 effect.fx 文件

c++ - 通过 Qt 中的 Objective-c 的 lambda( block )槽

C++ 管道字符串到邮件命令

编译器错误消息定制

c++ - ‘->’ token 之前的预期初始值设定项

c++ - std::cin.getline() 与 std::cin

c++ - 如何获取 C++ 格式的当前 DateTime(如 .NET 中的 System.DateTime.Now)