Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;getline赢得';不能使用int? 我一直在为我的C++类工作这个项目,我必须从用户那里收集信息,包括用户生日和当前年(我们还没有学会如何访问计算机的日期,所以必须手动检索)。我还处于这个过程的早期阶段,我遇到了这个我似乎无法克服的障碍_C++_Header_Int_Overloading - Fatal编程技术网

C++;getline赢得';不能使用int? 我一直在为我的C++类工作这个项目,我必须从用户那里收集信息,包括用户生日和当前年(我们还没有学会如何访问计算机的日期,所以必须手动检索)。我还处于这个过程的早期阶段,我遇到了这个我似乎无法克服的障碍

C++;getline赢得';不能使用int? 我一直在为我的C++类工作这个项目,我必须从用户那里收集信息,包括用户生日和当前年(我们还没有学会如何访问计算机的日期,所以必须手动检索)。我还处于这个过程的早期阶段,我遇到了这个我似乎无法克服的障碍,c++,header,int,overloading,C++,Header,Int,Overloading,虽然我使用这个过程很容易使用自定义类让名称系统工作,但我无法让它为年份值工作。我只能假设问题是年份是一个整数而不是一个字符串,但我不可能想出任何其他方法来实现这一点。有人能看看这个代码,帮我找出问题所在吗 主要类别: #include <iostream> #include <string> #include "Heartrates.h" using namespace std; int main() { Heartrates myHeartrate;

虽然我使用这个过程很容易使用自定义类让名称系统工作,但我无法让它为年份值工作。我只能假设问题是年份是一个整数而不是一个字符串,但我不可能想出任何其他方法来实现这一点。有人能看看这个代码,帮我找出问题所在吗

主要类别:

#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;
}
#包括
#包括
#包括“心率.h”
使用名称空间std;
int main(){
心率我的心率;

cout您尝试在那里使用的std::getline版本不接受int作为参数。请参阅您尝试调用的函数的标记为2(C++11)的函数。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;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
心率我的心率;
cout当前年份;
我的心率(当前年份);

cout不能与
int
一起使用,是的。请使用
std::getline()
使用
std::string
std::istringstream
进行第二步操作。非常感谢您的帮助!我自己永远也不会想到这一点,我的教科书甚至还没有提到sstream。或者,您可以使用
cin>>当前年度
而不使用
istringstream
。另外,请不要忘记
 >>
(在任何流上)都可能失败,因此您应该在使用值之前检查错误(如错误的用户输入)。
#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;
}