C++ getline 不适用于 int?
C++ getline won't work with int?
我一直在为我的 C++ class 开发这个项目,其中我必须从用户那里获得 collection 的信息,包括用户的出生年份和当前年份(我们有尚未了解如何访问计算机的日期,因此必须手动检索)。我在这个过程中还处于相当早的阶段,我已经 运行 遇到了这个我似乎无法解决的障碍。
虽然我已经使用此过程轻松地让名称系统使用自定义 class 工作,但我无法让它为年份值工作。我只能假设问题是年份是一个整数而不是一个字符串,但我不可能想出任何其他方法来让它工作。有人可以看看这段代码并帮助我找出问题所在吗?
主要class:
#include <iostream>
#include <string>
#include "Heartrates.h"
using namespace std;
int main() {
Heartrates myHeartrate;
cout << "Please enter your name (First and Last): ";
string yourName;
getline (cin, yourName);
myHeartrate.setName(yourName);
cout << "\nPlease enter the current year: ";
int currentYear;
getline (cin, currentYear);
myHeartrate.setCyear(currentYear);
cout << "\nYou entered " << currentYear;
}
心率class:
#include <string> //enables string use
class Heartrates {
public:
void setName(std::string yourName) {
name = yourName;
}
std::string getName() const {
return name;
}
void setCyear(int currentYear) {
Cyear = currentYear;
}
int getCyear() const {
return Cyear;
}
private:
std::string name;
int Cyear{ 0 };
};
我一直 运行ning 出错,指出没有找到匹配的重载函数,但如您所见,我在主 class 和 header 这个名字很好用。
您尝试在那里使用的 std::getline 版本不接受 int 作为参数。请参阅标记为 2 (C++11) 的函数,了解您尝试调用的函数 here。 std::istringstream 可以包含在 sstream 中。我会在最终打印输出中添加一个 std::endl(或新行)以使其看起来也更好。
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
Heartrates myHeartrate;
cout << "Please enter your name (First and Last): ";
// read line
string line;
getline (cin, line);
// line is yourName
myHeartrate.setName(line);
// read line, read int from line
cout << "\nPlease enter the current year: ";
int currentYear;
getline (cin, line);
std::istringstream ss(line);
ss >> currentYear;
myHeartrate.setCyear(currentYear);
cout << "\nYou entered " << currentYear << endl;
return 0;
}
我一直在为我的 C++ class 开发这个项目,其中我必须从用户那里获得 collection 的信息,包括用户的出生年份和当前年份(我们有尚未了解如何访问计算机的日期,因此必须手动检索)。我在这个过程中还处于相当早的阶段,我已经 运行 遇到了这个我似乎无法解决的障碍。
虽然我已经使用此过程轻松地让名称系统使用自定义 class 工作,但我无法让它为年份值工作。我只能假设问题是年份是一个整数而不是一个字符串,但我不可能想出任何其他方法来让它工作。有人可以看看这段代码并帮助我找出问题所在吗?
主要class:
#include <iostream>
#include <string>
#include "Heartrates.h"
using namespace std;
int main() {
Heartrates myHeartrate;
cout << "Please enter your name (First and Last): ";
string yourName;
getline (cin, yourName);
myHeartrate.setName(yourName);
cout << "\nPlease enter the current year: ";
int currentYear;
getline (cin, currentYear);
myHeartrate.setCyear(currentYear);
cout << "\nYou entered " << currentYear;
}
心率class:
#include <string> //enables string use
class Heartrates {
public:
void setName(std::string yourName) {
name = yourName;
}
std::string getName() const {
return name;
}
void setCyear(int currentYear) {
Cyear = currentYear;
}
int getCyear() const {
return Cyear;
}
private:
std::string name;
int Cyear{ 0 };
};
我一直 运行ning 出错,指出没有找到匹配的重载函数,但如您所见,我在主 class 和 header 这个名字很好用。
您尝试在那里使用的 std::getline 版本不接受 int 作为参数。请参阅标记为 2 (C++11) 的函数,了解您尝试调用的函数 here。 std::istringstream 可以包含在 sstream 中。我会在最终打印输出中添加一个 std::endl(或新行)以使其看起来也更好。
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
Heartrates myHeartrate;
cout << "Please enter your name (First and Last): ";
// read line
string line;
getline (cin, line);
// line is yourName
myHeartrate.setName(line);
// read line, read int from line
cout << "\nPlease enter the current year: ";
int currentYear;
getline (cin, line);
std::istringstream ss(line);
ss >> currentYear;
myHeartrate.setCyear(currentYear);
cout << "\nYou entered " << currentYear << endl;
return 0;
}