【问题标题】:How to use enter key as while condition如何使用回车键作为while条件
【发布时间】:2015-04-29 02:22:08
【问题描述】:

我想编写一个程序,它的一部分是输入一些数字,它们之间有一些空格,例如:1 2 3 并使用 while 循环输入它们 我想按回车键作为我的while循环的条件,但是如何?

【问题讨论】:

  • 你使用什么语言?
  • 我使用c++语言

标签: c++ while-loop enter


【解决方案1】:

stringstream 在这里很有用。

#include<iostream>
#include<sstream>
using namespace std;

int main()
{
    int x;
    string input;
    getline( cin, input );
    stringstream ss( input );
    while ( ss >> x )
    {
        // Do something with x
    }
}

【讨论】:

    猜你喜欢
    • 2017-04-16
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 2015-03-02
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多