这可能是一个非常简单的问题,但请原谅我,因为我是新手。 以下是我的代码:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
string name;
int i;
string mystr;
float price = 0;
cout << "Hello World!" << endl;
cout << "What is your name? ";
cin >> name;
cout << "Hello " << name << endl;
cout << "How old are you? ";
cin >> i;
cout << "Wow " << i << endl;
cout << "How much is that jacket? ";
getline (cin,mystr);
stringstream(mystr) >> price;
cout << price << endl;
system("pause");
return 0;
}
问题在于,当被问到“那件夹克多少钱?”时,getline不会要求用户输入,而只是输入初始值“0”。为什么会这样呢?