【问题标题】:Problem typing a letter when expecting a number in C ++在 C++ 中输入数字时出现问题
【发布时间】:2021-05-15 22:06:03
【问题描述】:

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

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

顺便说一下,我用的是 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();


        }
    }
}

【问题讨论】:

  • 顺便说一句,return main(); 不是一个好主意。我认为您正在尝试实现递归,但最好不要尝试使用main()。请改用循环。

标签: c++ visual-studio visual-studio-2010 switch-statement


【解决方案1】:

如果您将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 &lt;cctype&gt;

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

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

【讨论】:

    【解决方案2】:

    也许不返回 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";
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-17
        相关资源
        最近更新 更多