C++ c2664 错误 "cannot convert argument 1 from std::string to _Elem *"

标签 c++ string getline c2664

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

c2664 error "cannot convert argument 1 from std::string to _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;
}

最佳答案

首先,您需要知道有两个 getline C++ 中的东西,一个在 I/O 区域,一个在顶级标准命名空间。

cin.getline(breed, 100)是 I/O 区域中的一个(特别是 istream::getline(),它对字符串一无所知,更喜欢在字符数组上工作。你应该避免那个。

确实知道字符串的是std::getline()如果您不想回到 C 语言遗留“字符串”的糟糕过去,那通常是首选。

此外,在 C++ 中混合特定类型的输入(如 << )和特定行的输入(如 getline )操作时需要小心。了解每次操作前后文件指针的位置很重要。

例如,cin << someInt将在读入的整数之后立即保留文件指针。这意味着,如果您的下一个操作是 getline() ,它可能会在该整数之后的行中找到所有内容(最低限度,这将是您输入的换行符以处理整数),不是您要去的下一行输入您的字符串。

针对您的情况的一个简单解决方法是在尝试获取下一行之前忽略包括换行符在内的所有内容。这可以用 ignore() 来完成:

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

您还可以选择确保所有输入都是基于行的(一旦输入这些行就将它们转换为正确的类型),因为这可能会简化您确保指针位于正确的位置,并且可以更好地处理输入错误(例如输入 xyzzy 作为整数)。

这样的事情应该是一个好的开始:

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

关于C++ c2664 错误 "cannot convert argument 1 from std::string to _Elem *",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56572276/

相关文章:

c++ - 如何计算 2 个数字 L 和 R(均包括在内)之间的数字,以使所选数字的数字乘积为偶数?

C++ UTF-16 到 char 转换 (Linux/Ubuntu)

php - 转储到 JavaScript 数组的 HTML 文本破坏了我的代码

c++ - MinGW 和添加库

c++ - 使用 std::getline c++ 函数时遇到问题

c++ - 在 C++ 中读取文件时跳过 EOF

c++ - 在 C++ 类中保存函数

c++ - 模板类的 typedef 声明

javascript - 转义的反斜杠在字符串中显示为 "\\"

python - 子进程 "TypeError: a bytes-like object is required, not ' str'"