简体   繁体   English

在C ++中,如果按下返回键,如何使cin“取消”?

[英]In C++, how can I make cin “cancel” if the return key is pressed?

I'm trying to learn C++ by writing a simple console application. 我试图通过编写一个简单的控制台应用程序来学习C ++。 The user navigates the main menu by entering a number stored in a variable which a switch statement then uses to determine what to do. 用户通过输入存储在变量中的数字来导航主菜单,switch语句随后使用该数字来确定要执行的操作。 It's pretty simple. 很简单 :) :)

The issue that's bugging me is that when the program reaches the cin statement, pressing return without entering a number doesn't "exit" the statement but just bumps it down to the next line. 让我感到困扰的问题是,当程序到达cin语句时,在不输入数字的情况下按return不会“退出”该语句,而只是将其跳至下一行。 I guess this makes sense, but how can I make it so pressing return with no previous input just "exits" or "cancels" the cin statement? 我想这是有道理的,但是我如何才能做到,按回车键而没有先前输入,只是“退出”或“取消” cin语句?

Below is a shortened idea of what my application sort of looks like: 下面是我的应用程序外观的简短构想:

int main()
{
    int mainMenuSelector;

    while(mainMenuSelector != 4){
        cout << "--- MAIN MENU -----------------" << endl;
        cout << "[1] First Option" << endl;
        cout << "[2] Second Option" << endl;
        cout << "[3] Third Option" << endl;
        cout << "[4] Exit Application" << endl;
        cout << "-------------------------------" << endl;
        cout << "Selection: ";

        cin >> mainMenuSelector;
        // This is the statement I want to move along from
        // if the user presses the return key without entering any input.

        switch(mainMenuSelector){
            case 1:
                doSomething();
                break;
            case 1:
                doSomething();
                break;
            case 2:
                doSomething();
                break;
            case 3:
                doSomething();
                break;
        }
    }
    return 0;
}
std::string input;
while (std::getline(std::cin, input) && !input.empty()) { /* do stuff here */ }

You might want to go further and verify that the input is valid, doesn't just have a bunch of spaces, etc... 您可能想进一步验证输入是否有效,不仅有很多空格,等等。

Pressing enter with no input results in an empty string value. 在没有输入的情况下按Enter键将导致一个空字符串值。 You can do this (try it and adapt it to your code): 您可以执行此操作(尝试并使其适应您的代码):

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

int main() {
    string s;
    getline(cin, s);
    while(s != "") { // if the person hits enter, s == "" and leave the loop
        cout << s << endl;
        getline(cin, s);
    }
    return 0;
}

If you're specifically looking for options which use the stream operators (rather than parsing the input yourself), you might consider std::stringstream. 如果您特别在寻找使用流运算符的选项(而不是自己解析输入),则可以考虑使用std :: stringstream。 For example: 例如:

#include <string>
#include <iostream>
#include <sstream>

using namespace std;

void ExampleCaptureInput()
{
    int value;
    string s;
    getline(cin, s);
    if (s != "")
    {
        stringstream sstream(s);
        sstream >> value;
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM