猿问

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”。为什么是这样?


慕盖茨4494581
浏览 487回答 3
3回答
随时随地看视频慕课网APP
我要回答