我看了一些別人的質疑,我在C++知道,如果應用到這裏,我的問題,他們也newbish ..函數getline()將無法讀取字符串的第一個字時,我把它給一個變量
基本上,當顯示「名稱」的輸出時,如果輸入我的全名,它只會顯示第二個單詞。之前,它甚至沒有采取任何措施,只是略過了它。我現在很困惑,看起來很簡單。謝謝。
#include <iostream>
#include <string>
using namespace std;
int main()
{
double money;
string name;
int age;
// Prompt for age and receive
cout<<"How old are you? ";
cin >> age;
// Prompt for money and receive
cout<<"How much money do you have?";
cin >> money >> endl;
// Prompt for name and receive
cout<<"What's your name?\n\n"<< endl;
getline(cin, name);
// Display all information to user
cout<<"Hello, "<< name <<".";
cout << "You are " << age << " years old";
cout<< " and have $" << money << ".\n";
system("PAUSE");
return 0;
}
請勿混用'getline'和''>>除非你真的* *瞭解如何處理換行符。就你而言,只需要使用'getline'。 –
'cin >> money >> endl;'這是否編譯?我在這裏嘗試並得到了大量的錯誤... – zentrunix