首先,我使用的是Windows 8.1,Visual Studio 2013 Express和c ++。不知道我正在使用哪种C ++标准。
我对编程完全陌生,所以我可能错过了这个功能的一些基本部分。我正在进行一个10个问题的测验,这个是关于MJ去世的时候。如果用户使用int
输入getline()
之外的其他内容,我会尝试确保程序不会崩溃。
我了解了stringstream
并进行转换。它应该转换为" playerAnswer"到int
。
我正在使用#include <iostream>
#include <string>
#include <sstream>
和using namespace std;
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: ";
}
}
}
如果你能够按照预期的方式工作,并以更好的方式/使用更短的代码,我就会非常有兴趣学习它:)你们的任何意见都是适当的!
/ NICKL
答案 0 :(得分:1)
您没有初始化stringstream以使用用户输入的字符串:
$json = json_decode($row['elements']);
echo $json->book->{0}->title;
应该是
stringstream convHolder;