繁体   English   中英

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

[英]Problem typing a letter when expecting a number in C ++

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

有没有办法避免这种情况? 因此,当我输入一个字母或符号时,它会自动将其检测为无效并向我显示消息,而不会让控制台发疯?

顺便说一句,我使用 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();


        }
    }
}

如果将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是 function 位于cctype header 文件中,因此您应该在文件开头添加#include <cctype>

在此之后,您可以使用stoi()opc转换为 integer :

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

也许不返回主要?

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";
}

如前所述,您不应该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;

    }
}

暂无
暂无

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

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