【问题标题】:Input with cin is not working in switch case带有 cin 的输入在 switch case 中不起作用
【发布时间】:2017-09-05 06:51:40
【问题描述】:

我正在创建一个程序,它会询问用户回复以及您想要打印回复并显示多少次。我在程序中使用了一个while循环和switch case。 但是当我在std::cin 的帮助下将输入存储在变量a 中时,switch case 不会接收到相同的输入。任何帮助表示赞赏。

#include <iostream>

using namespace std;

int a;
int input;
int i=1;
void display()
{
    cout << "Select a choice for reply" << endl;
    cout << "1.Thank You" << endl;
    cout << "2.Welcome" << endl;
    cout << "3.Okay" << endl;
}

int main()
{
    display();
    cout << "Enter Choice" << endl;
    cin >> a;
    input='a';
    switch (input)
    {
        case '1': {
            int x;
            cout << "Enter no. of times  you want to print reply line" << endl;
            cin >> x;
            while (i <= x)
            {
                cout << "Thank you" << endl;
            }

            break;
        }
        case '2': {
            int x;
            cout << "Enter no. of times  you want to print line" << endl;
            cin >> x;
            while (i <= x)
            {
                cout << "Welcome" << endl;
            }

            break;
        }
        case '3': {
            int x;
            cout << "Enter no. of times  you want to print line" << endl;
            cin >> x;
            while (i <= x)
            {
                cout << "okay" << endl;
            }

            break;
        }
        default: {
            cout << "wrong choice" << endl;
        }

        cout << "Thank you for replying" << endl;
    }

    return 0;
}

【问题讨论】:

  • case '1' - 嗯。对你有用的机会很小。我不认为您尝试过case 1(即不要使用字符文字)。而且由于在随后的while 循环中ix 都没有改变,即使你解决了第一个问题,它也注定会旋转到无穷大。我强烈建议good book on C++
  • 请正确格式化您的代码,就像您的 C++ 教科书中的示例一样。
  • 您将input 声明为integer,然后分配了input='a'。之后你输入switch(input)
  • 编译所有警告和调试信息(例如g++ -Wall -Wextra -gGCC...)然后使用调试器 (gdb)
  • 我认为您将数字 ('9') 与数字 (9) 混淆了。

标签: c++ switch-statement cin


【解决方案1】:

核心问题是:

input = 'a'

还有:

case '1':

你可能想要的是:

input = a

case 1:

毕竟编译器应该发出警告。 但是为什么要复制价值呢? 做吧

switch(a)

其实你的代码有很多问题,不过我让其他人详细说明。

【讨论】:

    【解决方案2】:

    你的问题是input='a';,输入值为97,写input = (int)'a';也是一样

    为什么要声明“a”变量?只需使用输入 cin &gt;&gt; input;

    【讨论】:

      【解决方案3】:

      您的代码中有几个问题:
      1) 你尝试给一个整数一个字符:input = 'a';
      要做到这一点,你应该直接使用变量:input = a;(不带')

      2)在您的开关情况下,您与“1”、“2”(字符类型)进行比较,但input 是整数类型。试试这个:
      case 1:case 2:(不带 ')

      3)你不要在你的while循环中增加/减少i或x,它们永远不会停止。尝试类似:i++x-- 在循环中的某处。

      一些备注:
      1) 您可以使用不带大括号 ({ }) 的 switch case:

      int a = 0;
      cin >> 1;
      switch(a)
      {
          case 1:
              break;
          case 2:
              int tmp = 0;
              cout<<"tmp: "<<tmp<<endl;
              break;
          default:
              break;
      }
      

      2) 尽量避免使用全局变量,它们在大多数情况下是不必要的,也是不好的做法。

      3)你不需要你的变量a,你可以直接使用input,比如:

      cout << "Enter Choice" << endl;
      cin >> input;
      switch (input)
      

      4) 最后但并非最不重要的一点是,我建议您阅读一些不错的 c++ 书籍,这真的很值得。 (Good books)

      【讨论】:

        【解决方案4】:
        #include <iostream>
        using namespace std;
        int a;
        int input;
        int i=1;
        void display()
        {
            cout<<"Select a choice for reply"<<endl;
            cout<<"1.Thank You"<<endl;
            cout<<"2.Welcome"<<endl;
            cout<<"3.Okay"<<endl;
        }
        
        int main()
        {
            display();
            cout<<"Enter Choice"<<endl;
            cin>>a;
            input=a;
            switch(input)
            {
                case 1 :
                {
                    int x;
                    cout<<"Enter no. of times  you want to print reply 
                    line<<endl;
                    cin>>x;
                    while(i<=x)
                    {
                        cout<<"Thank you"<<endl;
                        x--;
                    }
                    break;
                }
                case 2 :
                {
                    int x;
                    cout<<"Enter no. of times  you want to print line" <<endl;
                    cin>>x;
                    while(i<=x)
                    {
                        cout<<"Welcome"<<endl;
                        x--;
                    }
                    break;
                }
                case 3 :
                {
                    int x;
                    cout<<"Enter no. of times  you want to print line"<<endl;
                    cin>>x;
                    while(i<=x)
                    {
                        cout<<"okay"<<endl;
                        x--;
                    }
                    break;
                }
                default:
                {
                    cout<<"wrong choice"<<endl;
                }
                cout<<"Thank you for replying"<<endl;
            }
            return 0;
        }
        

        【讨论】:

        • 你的问题是input='a';,输入值是97,写成input = (int)'a'也是一样的;另外也不需要取两个不同的变量这是一个输入,一个就足够了。但是由于您声明并使用了这两个变量,我没有从您的程序中删除它。当您编写 case st。在 switch 中,请始终记住整数的语法是 (case 1 :)其中 1 可以替换为您提供的任何数字作为字符的选择和语法(case 'a':)。同样在 while 循环中,您忘记给出终止条件( x--;)。
        猜你喜欢
        • 1970-01-01
        • 2019-03-06
        • 2016-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-22
        • 1970-01-01
        • 2014-03-23
        相关资源
        最近更新 更多