C++ C++;c2664错误“;无法将参数1从std::string转换为“u Elem*”;

C++ C++;c2664错误“;无法将参数1从std::string转换为“u Elem*”;,c++,string,getline,c2664,C++,String,Getline,C2664,我整个星期都在做作业。就在我让程序最终运行时,我意识到只要使用cin>>bride,如果我的输入有一个空格,它就会破坏代码(因为我的程序需要收集3个单独的变量,首先是int,然后是string,最后是bool)。因为这是第二个变量,所以它会使用带有白色字符的短语来打乱我的代码。当我尝试将其更改为cin.get或cin.getline时,我收到的错误消息如下: c2664错误“无法将参数1从std::string转换为_Elem*” 下面是有问题的代码(中间一行给出了错误)。任何帮助都将不胜感激

我整个星期都在做作业。就在我让程序最终运行时,我意识到只要使用
cin>>bride
,如果我的输入有一个空格,它就会破坏代码(因为我的程序需要收集3个单独的变量,首先是int,然后是string,最后是bool)。因为这是第二个变量,所以它会使用带有白色字符的短语来打乱我的代码。当我尝试将其更改为
cin.get
cin.getline
时,我收到的错误消息如下:

c2664错误“无法将参数1从std::string转换为_Elem*”

下面是有问题的代码(中间一行给出了错误)。任何帮助都将不胜感激

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int birthyear;  
    string breed;    
    bool vaccines;  

    cout << "Please enter value for dog's birth year: ";
    cin >> birthyear;
    cout << "What is the breed of the dog: ";
    cin.getline(breed, 100);
    cin.ignore();
    cout << "Has the dog been vaccinated (1 = Yes/ 0 = No): ";
    cin >> vaccines;
}
#包括
#包括
使用名称空间std;
int main()
{
int生日;
弦品种;
布尔疫苗;
生日;
cout疫苗;
}

> p>首先,您需要注意C++中有两个代码<代码> GETLION>代码>,一个在I/O区域,一个在顶级标准命名空间中。
cin.getline(breed,100)
是I/O区域中的一个(特别是
istream::getline()
),它对字符串一无所知,更喜欢处理字符数组。您可能应该避免使用这个

真正了解字符串的是
std::getline()
,如果您不想回到C-legacy“strings”的糟糕时代,这通常是首选


此外,在混合特定类型的输入时(如<代码>创建代码< STD::GETLIN(CIN,GYES)),您需要在C++中小心。

可能会有帮助。对不起,伙计们,我对编码和这里都是新手。我试图简化整个程序中的代码,并将其重新发布到上面。即使没有类和函数,仍然会收到相同的错误消息。好的,Eljay建议消除错误消息…但是如果输入中有空格或白色字符,它仍然跳过t接下来是几行代码。我认为getline的目的是为了避免这种情况发生?
忽略
属于在流中留下数据的指令之后。不要将它们放在其他指令之前或在整个代码中随机放置,以防万一。如何将每个输入转换为基于行的变量我可以学习C++,不太理解这个概念吗?@加拉,看更新。我知道你还在学习,可能有一点要消化,但这是值得的。非常感谢你的帮助和信息。我已经把你的代码带到目前为止了。它帮助我制定了一个工作程序。也就是说,你能帮助我理解
getResp
的目的吗?它与
cin
有何不同/更好,因为我还没有遇到过它?我真的很喜欢你的代码允许
bool
接受不仅仅是1和0的有效答案。还有其他方法来完成这个任务吗使用
bool
变量执行同一任务?再次感谢您的帮助!@gaara,您不太可能遇到它,因为我刚刚编写了它:-)使用
getResp
的原因只是为了提供和重载函数,这会给您带来很多麻烦(输出一个提示,获取输入字符串,并根据需要对输入内容执行任何检查)。这将使“主”代码可以简单地指定其意图,而不是在其中包含大量转换和/或检查代码。
#include <iostream>
#include <string>
#include <limits>
using namespace std;

int main() {
    int birthyear; string breed; bool vaccines;

    cout << "Please enter value for dog's birth year: ";
    cin >> birthyear;

    cout << "What is the breed of the dog: ";
    cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
    getline(cin, breed);

    cout << "Has the dog been vaccinated (1 = Yes/ 0 = No): ";
    cin >> vaccines;

    // Output what you got.

    cout << birthyear << " '" << breed << "' " << vaccines << '\n';
}
#include <iostream>
#include <string>
#include <limits>
#include <set>
#include <cstdlib>
using namespace std;

// Get string, always valid. Optionally strip leading and
// trailing white-space.

bool getResp(const string &prompt, string &val, bool strip = false) {
    cout << prompt;
    getline(cin, val);
    if (strip) {
        val.erase(0, val.find_first_not_of(" \t"));
        val.erase(val.find_last_not_of(" \t") + 1);
    }
    return true;
}

// Get unsigned, must ONLY have digits (other than
// leading or trailing space).

bool getResp(const string &prompt, unsigned long &val) {
    string str;
    if (! getResp(prompt, str, true)) return false;

    for (const char &ch: str)
        if (! isdigit(ch)) return false;

    val = strtoul(str.c_str(), nullptr, 10);
    return true;
}

// Get truth value (ignoring leading/trailing space),
// and allow multiple languages.

bool getResp(const string &prompt, bool &val) {
    string str;
    if (! getResp(prompt, str, true)) return false;

    const set<string> yes = {"yes", "y", "1", "si"};
    const set<string> no = {"no", "n", "0", "nyet"};

    if (yes.find(str) != yes.end()) {
        val = true;
        return true;
    }

    if (no.find(str) != no.end()) {
        val = false;
        return true;
    }

    return false;
}

// Test driver for your situation.

int main() {
    unsigned long birthYear;
    std::string dogBreed;
    bool isVaccinated;

    if (! getResp("What year was the dog born? ", birthYear)) {
        std::cout << "** ERROR, invalid value\n";
        return 1;
    }

    if (! getResp("What is the breed of the dog? ", dogBreed, true)) {
        std::cout << "** ERROR, invalid value\n";
        return 1;
    }

    if (! getResp("Has the dog been vaccinated? ", isVaccinated)) {
        std::cout << "** ERROR, invalid value\n";
        return 1;
    }

    std::cout
        << birthYear
        << " '" << dogBreed << "' "
        << (isVaccinated ? "yes" : "no") << '\n';
}