C++,字母,数字和空格用户输入字符串

问题描述:

所以我有一个小问题,我找不到一个优雅的解决方案。C++,字母,数字和空格用户输入字符串

我要求用户输入他们的地址。我将如何把它放入一个字符串?它将包含字母,数字和空格。我尝试了getline函数,但没有成功。

COUT < < “\ n输入客户的街道地址。” < < ENDL;
cin >> address;

要做这样的事情,你会想要使用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

可以使用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"; 
}