C++ 如何在不创建新行的情况下使用 cin 输入变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15209370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:10:36  来源:igfitidea点击:

How do I input variables using cin without creating a new line?

c++windowsvisual-studio

提问by Thomas Boxley

Whenever I input a variable using cin, after one hits enter it automatically goes to a new line. I'm curious if there's a way to use cinwithout having it go to a new line. I'm wanting to cinand coutmultiple things on the same line in the command prompt. Is this possible?

每当我使用 输入变量时cin,在一次点击后输入它会自动转到新行。我很好奇是否有一种方法可以使用cin而无需换行。我想cin,并cout在命令提示符下在同一行多件事情。这可能吗?

采纳答案by Benjamin Lindley

You can't use cinor any other standard input for this. But it is certainly possible to get the effect you are going for. I see you're on Windows using Visual Studio, so you can use, for example, _getch. Here's an example that reads until the next whitespace and stores the result in a string.

您不能cin为此使用或任何其他标准输入。但是肯定有可能达到你想要的效果。我看到您在使用 Visual Studio 的 Windows 上,因此您可以使用,例如,_getch。这是一个读取直到下一个空格并将结果存储在字符串中的示例。

#include <conio.h> // for _getch

std::string get_word()
{
    std::string word;
    char c = _getch();
    while (!std::isspace(c))
    {
        word.push_back(c);
        std::cout << c;
        c = _getch();
    }
    std::cout << c;
    return word;
}

It's not very good. For example, it doesn't handle non printing character input very well. But it should give you an idea of what you need to do. You might also be interested in the Windows API keyboard functions.

这不是很好。例如,它不能很好地处理非打印字符输入。但它应该让你知道你需要做什么。您可能还对Windows API 键盘功能感兴趣。

If you want a wider audience, you will want to look into some cross-platform libraries, like SFMLor SDL.

如果您想要更广泛的受众,您将需要研究一些跨平台的库,例如SFMLSDL

回答by Amir

you can also use space for input instead of enter something like this:

您还可以使用空格进行输入,而不是像这样输入:

cin >> a >> b >> c;

and in input you type

并在输入中键入

10 20 30

then

然后

a=10
b=20
c=30 

回答by Simon

As others have noted, you can't do this with cin, but you could do it with getchar(). What you would have to do is:

正如其他人所指出的,你不能用 来做到这一点cin,但你可以用getchar(). 你必须做的是:

  1. collect each character individually using getchar()(adding each to the end of a stringas it is read in, for instance), then
  2. after reading each character, decide when you've reached the end of one variable's value (e.g. by detecting one or more ' 'characters in the input, if you're reading in intor doublevalues), then
  3. if you've reached the end of the text for a variable, convert the string of characters that you've built into a variable of the appropriate type (e.g. int, double, etc.), then
  4. output any content onto the line that might be required, and then
  5. continue for the next variable that you're reading in.
  1. 使用getchar()string例如,在读入时将每个字符添加到 a 的末尾)单独收集每个字符,然后
  2. 读取每个字符后,决定何时到达一个变量值的末尾(例如,通过检测' '输入中的一个或多个字符,如果您正在读入intdouble值),然后
  3. 如果你已经达到了文本的变量结束,转换字符的字符串,你已经内置到合适的类型(例如变量intdouble等等),然后
  4. 将任何内容输出到可能需要的行上,然后
  5. 继续读取您正在读取的下一个变量。

Handling errors robustly would be complicated so I haven't written any code for this, but you can see the approach that you could use.

稳健地处理错误会很复杂,所以我没有为此编写任何代码,但是您可以看到可以使用的方法。

回答by SamiHuutoniemi

I don't think what you want to do can be achieved with cin. What you can do is to write all your input in one line, with a delimiter of your choosing, and parse the input string.

我不认为你想做的事情可以用cin来实现。您可以做的是将所有输入写在一行中,并使用您选择的分隔符,然后解析输入字符串。

回答by Thomas Boxley

It is not possible. To quote @Bo Persson, it's not something controlled by C++, but rather the console window.

这不可能。引用@Bo Persson 的话,它不是由 C++ 控制的,而是由控制台窗口控制的。

回答by user3394217

just use the gotoxy statement. you can press 'enter' and input values in the same line for eg. in the input of a 3*3 matrix:

只需使用 gotoxy 语句。您可以按“输入”并在同一行中输入值,例如。在 3*3 矩阵的输入中:

'#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a[20][20],x,y;
cout<<"Enter the matrix:\n ";
for(int r=2;r<7;r+=2)
for(int c=2;c<7;c+=2)
{gotoxy(c,r);
cin>>a[r][c];
}
getch();}'

回答by Aman Thakkar

I can't comment but if you leave spaces between integers then you can get the desired effect. This works with cintoo.

我无法发表评论,但如果您在整数之间留有空格,那么您可以获得所需的效果。这也适用cin

int a, b, c;
cin>>a; cin>>b; cin>>c;

If you enter your values as 10 20 30then they will get stored in a, b, and c respectively.

如果您输入您的值,10 20 30那么它们将分别存储在 a、b 和 c 中。