我整个星期都在做作业。就在我让程序最终运行时,我意识到通过使用cin >> breed,如果我的输入有一个空格,它就会破坏代码(因为我的程序需要收集3个独立的变量,首先是int,然后是字符串,最后是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;
}发布于 2019-06-13 03:08:03
首先,您需要注意,在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';
}https://stackoverflow.com/questions/56572276
复制相似问题