在 C ++ 中输入数字时输入字母时出现问题

问题描述 投票:0回答:4

我正在制作一个程序,其中包括制作一个菜单来注册和删除商店中的产品,我只是设计带有开关的菜单,到目前为止一切正常,问题是当我输入数字以外的内容时当数据(字母或符号)时,控制台会变得疯狂;所有文本开始闪烁,它不会让我做任何事情(就像它在循环中一样),我必须关闭它。

有什么办法可以避免这种情况吗?因此,当我输入字母或符号时,它会自动将其检测为无效并向我显示消息,而控制台不会变得疯狂?

顺便说一句,我使用 Visual Studio。

提前致谢:)

#include<iostream>
#include<locale.h>

using namespace std;

int main()
{
   
    int opc;

    cout << "                WELCOME TO THE STORE \"Happy Shopping\" ";
    cout << endl;
    

    cout << "\n1.- Create Order.";
    cout << "\n2.- Delate Order.";
    cout << "\n3.- list of orders created.";
    cout << "\n4.- Exit";
    cout << endl;
    cout << "\n¿what do you want to do?: "; cin >> opc;

    switch (opc)
    {

    case 1:cout << "\nCreate Order"; break;
    case 2:cout << "\nDelate Order"; break;
    case 3: cout << "\nlist of orders created"; break;
    case 4:exit(EXIT_SUCCESS);
    default:
        
        if ((opc != 1) && (opc != 2) && (opc != 3) && (opc != 4))
        {

            system("cls");

                cout << "the option entered is not valid, try again";
                return main();


        }
    }
}
c++ visual-studio visual-studio-2010 switch-statement
4个回答
0
投票

也许不返回 main?

switch (opc)
{

case 1:cout << "\nCreate Order"; break;
case 2:cout << "\nDelate Order"; break;
case 3: cout << "\nlist of orders created"; break;
case 4:exit(EXIT_SUCCESS);
default:
    system("cls");
    cout << "the option entered is not valid, try again";
}

0
投票

如果您将

opc
设为
string
并将输入作为字符串,您可以手动检查该字符串是否为数字,如下所示:

cin >> opc;
if (!isdigit(opc[0]) // since you're dealing with single-digit numbers, this should be fine
{
  cout << "You didn't enter a number!\n";
  // Any other error handling
}

isdigit
是位于
cctype
头文件中的函数,因此您应该在文件开头添加
#include <cctype>

在此之后,您可以使用

opc
stoi()
转换为整数:

int opnum = stoi(opc);
// opnum is now the number, write the rest of the code such that it uses opnum

0
投票

正如已经说过的,你不应该

return main()
。例如,您可以通过测试您的输入来使用循环(这肯定是可能存在的众多解决方案之一)。

此外,您不需要下面的代码部分:

 // you do not need the if as you are already in the case !=1,2,3, or 4
 if ((opc != 1) && (opc != 2) && (opc != 3) && (opc != 4))

我为您提供了一种取自您的代码的尝试,这可能有助于改进您的代码。 可能仍然存在一些错误,但这是一个很好的起点

#include<iostream>
#include<locale.h>

int main()
{

    int opc=0;
    std::string input;

    std::cout << "                WELCOME TO THE STORE \"Happy Shopping\" ";
    std::cout << std::endl;
    // first display
    std::cout << "\n1.- Create Order.";
    std::cout << "\n2.- Delate Order.";
    std::cout << "\n3.- list of orders created.";
    std::cout << "\n4.- Exit";
    std::cout << std::endl;
    std::cout << "\n¿what do you want to do?: "; //cin >> opc;

    std::cin >> input;

    while ( input.length() > 0 )
    {
        // if we enter a string of more than length 1


        try
        {
            opc = std::stoi( input );

        }
        catch (...)
        {
            // mainly if the converted argument is not a number =>std::invalid_argument
            std::cout << "invalid value " << opc << std::endl;
        }
        std::cout << "you entered the value " << opc << std::endl;

        switch (opc)
        {

        case 1:
            std::cout << "\nCreate Order";
            break;
        case 2:
            std::cout << "\nDelate Order";
            break;
        case 3:
            std::cout << "\nlist of orders created";
            break;
        case 4:
            exit(EXIT_SUCCESS);
        default:
            // you do not need the if as you are already in the case !=1,2,3, or 4
            //if ((opc != 1) && (opc != 2) && (opc != 3) && (opc != 4))
            //{
            system("cls");
            // if you type things other than
            std::cout << "\n1.- Create Order.";
            std::cout << "\n2.- Delate Order.";
            std::cout << "\n3.- list of orders created.";
            std::cout << "\n4.- Exit";
            std::cout << std::endl;
            std::cout << "\n¿what do you want to do?: "; //cin >> opc;
        }

        std::cin >> input;

    }
}


0
投票

重写该程序,如果用户在需要输入数字或字母的字段中输入除A、B、C、数字和特殊字符之外的字母,则会向用户弹出错误消息

要选择矩形、圆形还是圆柱体,系统会从菜单表中询问用户

菜单表如下:

选择你的形状

A.长方形

B.圆圈

C.气缸

X。退出

如果用户选择A、B、C,并且在执行程序后,您的系统将询问他/她是否要继续并按Y再次重新运行相同的形状,按M返回主菜单或 X 退出,系统将显示“您的系统已终止”。

© www.soinside.com 2019 - 2024. All rights reserved.