Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.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
Stringstream赢得';不能正确转换 首先,我使用Windows 8.1、VisualStudio 2013 Express和C++。不知道我使用的C++标准是什么。_C++_Windows_Visual Studio 2013_Console Application_Stringstream - Fatal编程技术网

Stringstream赢得';不能正确转换 首先,我使用Windows 8.1、VisualStudio 2013 Express和C++。不知道我使用的C++标准是什么。

Stringstream赢得';不能正确转换 首先,我使用Windows 8.1、VisualStudio 2013 Express和C++。不知道我使用的C++标准是什么。,c++,windows,visual-studio-2013,console-application,stringstream,C++,Windows,Visual Studio 2013,Console Application,Stringstream,我对编程一无所知,所以我可能错过了这个函数的一些基本部分。我正在做一个10个问题的测验,这个测验是关于MJ去世的时间。我试图确保如果用户使用getline()输入的不是int,程序不会崩溃 我学习了stringstream和转换。它应该将“playerAnswer”转换为int。 我正在使用#include#include#include和使用名称空间标准 int question_3() { cout << "Question 3:\n"; string pla

我对编程一无所知,所以我可能错过了这个函数的一些基本部分。我正在做一个10个问题的测验,这个测验是关于MJ去世的时间。我试图确保如果用户使用
getline()
输入的不是
int
,程序不会崩溃

我学习了
stringstream
和转换。它应该将“playerAnswer”转换为
int
。 我正在使用
#include
#include
#include
使用名称空间标准

int question_3()
{

    cout << "Question 3:\n";

    string playerAnswer = "";
    int convertedAnswer = 0;    

    cout << "Which year did Michael Jackson die? 2008, 2009 or 2010?\n" \
            "Your answer: ";

    while (true)
    {
        getline(cin, playerAnswer);
        stringstream convHolder;   // EDIT: Got an answer and it now works.
                                   // Forgot  (playerAnswer) in convHolder
        if (convHolder >> convertedAnswer)
        {
            if (convertedAnswer == 2009)
            {
                cout << endl << "Correct! \nOn August 29 1958 the legend was born. \n" \
                                "On June 25 2009 he passed away in his rented mansion in Holmby Hills.\n";
                cout << endl;
                return 1;
            }
            else
            {
                cout << endl << "Wrong. \nOn August 29 1958 the legend was born. \n" \
                                "On June 25 2009 he passed away in his rented mansion in Holmby Hills.\n";
                cout << endl;
                return 0;
            }
        }
        else
        {
            cout << "Invalid number, please try again: ";
        }
    }
}
int问题_3()
{
cout convertedAnswer)
{
如果(convertedAnswer==2009)
{

cout您没有初始化stringstream以使用用户输入的字符串:

stringstream convHolder;
应该是

stringstream convHolder(playerAnswer);

“但这似乎不起作用”是一个含糊不清的问题。请提供一个。也请查看和。谢谢。小错误,但就是看不到。现在我如何关闭此问题并选择您的答案作为问题的解决方案?@NickL您单击我答案箭头/分数附近的复选标记