简体   繁体   English

当从键盘输入数据时,当用户在C ++中按“输入”时如何完成输入?

[英]when inputing data from keyboard, how to complete input when user press “enter” in C++?

The problem is, in C++, I hope to get numbers from user and put every int to an vector. 问题是,在C ++中,我希望从用户那里获取数字并将每个int放到一个向量中。 So I write the following code: 所以我写下面的代码:

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

vector<int> readVals()
{
    vector<int> read;
    int temp;
    cin >> temp;
    while (!cin.fail() && !cin.eof())
        {
            read.push_back(temp);
            cin >> temp;
        }
    return read;
}

void printVals(vector<int> v)
{
    if(v.size() >= 1)
        {
            for (vector<int>::size_type i = 0; i < v.size()-1; i++ )
        {
            cout << v[i] << " ";
            cout << v[v.size()-1] << "\n";
        }
    }
}

int main()
{
    vector<int> a = readVals();
    printVals(a);
    return 0;
}

Then, I compile it to create an a.out file. 然后,我编译它来创建一个a.out文件。 I have some numbers in in1 . 我在in1有一些数字。 And when I do the command: a.out < in1 , I got what I want. 当我执行命令时: a.out < in1 ,我得到了我想要的东西。 But I'm confused when I hope the data can be inputted by user. 但是当我希望数据可以由用户输入时我很困惑。 The user can input some numbers and press Enter to pass the numbers in. However, I used getline() , failed. 用户可以输入一些数字并按Enter键传入数字。但是,我使用了getline() ,失败了。 !="\\n" , failed. !="\\n" ,失败了。 Everytime when I press Enter , it seems that the program is still waiting for more numbers and not print out the result. 每当我按Enter键时 ,似乎程序仍在等待更多数字而不打印结果。 Does any one can help me to make it successfully? 有没有人可以帮我成功? Thank you! 谢谢!

Your loop is waiting for cin to be in a "failed" state or at the end of file. 你的循环正在等待cin处于“失败”状态或文件末尾。 Hitting enter does neither. 击中输入既不会。 You can end the input by hitting CTRL-Z on windows or CTRL-D on unix/mac. 您可以通过在Windows上按CTRL-Z或在unix / mac上按CTRL-D来结束输入。 These send the "End of file" character to cin. 这些将“文件结束”字符发送给cin。 Alternatively, change your loop condition to "listen" for some specific input. 或者,将循环条件更改为“监听”某些特定输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 C++ 中的提示(按 ENTER 终止)获取用户输入? - How to get user input from a prompt(terminated by press ENTER) in c++? 如何防止用户在 c++ 中输入字符串 - How to prevent the user from inputing a string in c++ 如何通过在C ++和C#中从键盘立即按(不按Enter键)将用户中的数据存储在我的变量中 - How To Store data from the user in my variables by Immediate presses from the keyboard in C++ and C#(without pressing enter) 通过按空格键结束键盘输入数据,而不输入c / c ++键 - Ending input data from keyboard by hitting spacebar, not enter key c/c++ 比较字符串时如何在C ++中的函数调用中输入输入 - How to enter input in function calls in c++ when comparing strings 用户在C ++中按Enter时如何获取输出 - how to get output when user just presses enter in c++ 按Enter按钮时如何使用C ++ Win32 API调用按钮? - How to invoke a button using C++ Win32 API when press the enter button? 仅当我在c ++(Thread)中按Enter键时,代码如何进入下一步 - How the code gets to the next step only when i press the enter key in c++ (Thread) 使用C ++在控制台中从用户接收信息时,如何在当前行中保持“ Enter”? - How can i Keep “Enter” in the current line when receiving information from the user in the console by c++? 将文本文件输入数组时,c ++程序崩溃 - c++ program crash when inputing text file into array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM