這可能是一個非常簡單的問題,但請原諒我,因爲我是新手。 這裏是我的代碼:getline不要求輸入?
#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;
}
的問題是,當被問及how much is that jacket?
函數getline不要求輸入用戶和剛剛輸入的「0」的初始值。爲什麼是這樣?
注:這已經問了很多次。這裏有一些少數http://stackoverflow.com/questions/3731529/program-is-skipping-over-getline-without-taking-user-input http://stackoverflow.com/questions/1744665/need-help-與-函數getline http://stackoverflow.com/questions/4876959/ignore-spaces-using-getline-in-c – cpx