2013-10-29 65 views
-1

所以我想让用户输入他们的名字和身高。程序不会允许2个输入?

我有其他的代码。

我已经得到了这个。

#include <iostream> 
#include <string> 
#include <algorithm> 
using namespace std; 

int main() 
{ 
int name1; 
cout << "What's your name?"; 
cin >> name1; 

int height1; 
cout << "What's your height?"; 
cin >> height1; 

return 0; 
} 

问题是,它不会允许用户输入他们的身高。有任何想法吗?

+1

你'name1'作为'int'。那是错的。另外,'cin >>'输入是空格分隔的。 – crashmstr

回答

6

问题是,你使用的是int变量而不是std :: string。但是,你已经包括了<string>头文件,所以你可能想这样做:

#include <iostream> 
#include <string> 
#include <algorithm> 
using namespace std; 

int main() 
{ 
std::string name1; 
cout << "What's your name?"; 
cin >> name1; 

std::string height1; 
cout << "What's your height?"; 
cin >> height1; 

return 0; 
} 

否则只有当你输入整数工作 - 但是,这并不让一个“名称”输入太大的意义。

编辑:如果它也用空格输入名称您的要求,您可以使用std::getline

#include <iostream> 
#include <string> 
#include <algorithm> 
using namespace std; 

int main() 
{ 
std::string name1; 
cout << "What's your name?"; 
getline(cin, name1); 

std::string height1; 
cout << "What's your height?"; 
getline(cin, height1); 

return 0; 
} 
+0

谢谢!有用! –

+1

那么如果我输入“Bob T. Fish”'会发生什么? (即它有点破) – BoBTFish

+0

多个单词不起作用,他会为此寻求帮助。但作为C++流的快速入门,这并不坏。 –