2014-09-13 24 views

回答

4

试试这个:

int num; 
cout << "Enter numbers separated by a space" << endl; 
do 
{ 
    cin >> num; 
    /* process num 
    or use array or std::vector to store num for later use 
    */ 

}while (true); 

这可能回答您的查询。

+0

你可能想要定义num。他没有在他的问题中定义变量。 – dejay 2014-09-13 05:47:26

+0

任何修复代码的方法?我用你的代码并修改它。但是,此代码的结果是:输入用空格分隔的数字 1输入用空格分隔的数字 #include using namespace std; int main() { \t int num; \t do { \t cout <<“输入空格分隔的数字”<< endl; \t cin >> num; \t cout << num; \t} \t while(true); } – bobtheboy 2014-09-13 05:56:35

+0

另外,运行时出现错误。我需要的输入是准确的,当我进入。恩。输入:1 2 3;输出:1 2 3 – bobtheboy 2014-09-13 06:01:58

0

getline可能是你在找什么。来自CPlusPlus.com的示例:

// istream::getline example 
#include <iostream>  // std::cin, std::cout 

int main() { 
    char name[256], title[256]; 

    std::cout << "Please, enter your name: "; 
    std::cin.getline (name,256); 

    std::cout << "Please, enter your favourite movie: "; 
    std::cin.getline (title,256); 

    std::cout << name << "'s favourite movie is " << title; 

    return 0; 
} 
+0

这怎么能帮助我输入无限数字?另外,“std ::”是做什么的? – bobtheboy 2014-09-13 05:52:58

+0

嗯......你有估计你想输入多少个数字吗?对于初学者来说,计算机不能存储无限数量的数字...... – dejay 2014-09-13 05:56:20

+0

使用'std ::'东西可以避免说您正在使用像'using namespace std'这样的命名空间。 – dejay 2014-09-13 05:59:53

0

我想出了什么对无限输入最适合。

最好的一个是:

while (true){ 
cin >> i; 
} 

但是,它并没有在空间采取。

如上面Dejay所述,用于包含空格的所有东西都是getline。 最后一个,虽然不如(虽然),但仍然有效的是for(;;)。它和while(true)非常相似,并且不识别空白字符。

Ex。

for(;;){ 
cin >> i; 
} 
相关问题