如何实现“ ENTER”。案例转换成switch语句 [英] How to implement an "ENTER" case into a switch statement

查看:102
本文介绍了如何实现“ ENTER”。案例转换成switch语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个课堂项目上,我必须在C ++中为咖啡店创建一个订购系统。如果适用,我正在Visual Studio中工作。

I am working on a class project where I have to create an ordering system for a coffee shop in C++. If it is applicable, I'm working in Visual Studio.

在项目大纲中,老师说有一个简单的整数输入可以浏览菜单;但是,他指定如果输入了NOTHING(假设我已经看到所谓的热输入),它将计算收据并重置程序。

In the project outline, the teacher said that there is a simple integer input to navigate the menu; however, he specifies that if NOTHING is inputted (I'm assuming what I've seen called a "hot enter") that it calculates the receipt and the program resets.

我已经尝试了cin.get()并检查缓冲区是否为'\n',并且工作正常,但是我当前的实现似乎只能捕获一个热输入,并且无法进入switch的情况。

I have tried cin.get() and checking if the buffer is '\n', and this works fine, but my current implementation seems to only be able to capture a hot enter, and fails to roll into the switch case.

为了从用户那里获取输入,我目前已经尝试过:

For getting input from the user, I've currently tried this:

        // Get menu input
        if (cin.get() == '\n') {    // Check if user hot entered, assign value if so
            input = 0; 
        } else {                    // If not, do it normally
            input = cin.get();
        }

但是,这不太正确,并且无法捕获输入的整数以供使用在开关盒中。我不确定这种实施方式在推理中是否合理,还是不确定是否有更简单的途径来进行紧急进入。

However this does not work quite right, and fails to capture inputted integers for use in the switch case. I'm unsure if this sort of implementation is sound in reasoning, or whether there is a much simpler route to have a case for a hot enter.

我不知道收到任何错误,因此我想我对这些功能的工作方式有误,或者我的实现存在逻辑缺陷。

I don't receive any errors, so I imagine there is something wrong with my understanding of how these functions work, or my implementation is flawed in its logic.

谢谢。

推荐答案

您使用了 cin.get()两次。 input = cin.get(); 中的第二个 cin.get()是多余的。

You used cin.get() twice. The second cin.get() in input = cin.get(); is redundant.

// Get menu input
    input = cin.get();
    if (input == '\n') {    // Check if user hot entered, assign value if so
        input = 0; 
    } 
//else {      // If not, do it normally
//         input = cin.get();
//     }

这篇关于如何实现“ ENTER”。案例转换成switch语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆