2014-12-03 96 views
0

所以我有一個小問題,我找不到一個優雅的解決方案。C++,字母,數字和空格用戶輸入字符串

我要求用戶輸入他們的地址。我將如何把它放入一個字符串?它將包含字母,數字和空格。我嘗試了getline函數,但沒有成功。

COUT < < 「\ n輸入客戶的街道地址。」 < < ENDL;
cin >> address;

回答

0

要做這樣的事情,你會想要使用stringgetline

string address; 
cout << "\nEnter customer street address: "; 
cin.ignore(numeric_limits<streamsize>::max()); // May or may not be needed. 
getline(cin, address); 

string Reference
getline Reference numeric_limits Reference

+0

當我試圖函數getline(CIN,地址);它只是跳過它,並沒有得到用戶輸入,爲什麼? – flowinwind 2014-12-03 06:32:05

+0

@WhatThePhil,很奇怪,但看到我的編輯。這應該照顧它。 – David 2014-12-03 06:37:01

0

可以使用std::getline

int main() 
{ 
    // greet the user 
    std::string name; 
    std::cout << "What is your name? "; 
    std::getline(std::cin, name); 
    std::cout << "Hello " << name << ", nice to meet you.\n"; 
} 
相關問題