【问题标题】:Cin not getting the proper inputCin 没有得到正确的输入
【发布时间】:2014-06-25 19:15:00
【问题描述】:

我用 C++ 编写的程序有问题。请注意,我不在笔记本电脑上(接下来的 8 小时我都在工作),所以我无法在此处复制代码,但我在这个问题上花了几个小时,所以我知道出了什么问题,只是不要不知道如何解决它。所以我的代码看起来像这样:

bool valid = false;
int choice;

cout << "Please make a choice between 1 and 4\n"; 
//Here I enumarate 4 choices
cin >> choice;  //first input

//note that I do verify that an integer is entered and that it's between 1 and 4
//my code works so far

valid = true; //if the choise is good

while(valid)
{
     if (choice == 1)
     {
         //here I open a url
         cout << "If you would like to stick with this choice, presse 1 again or make  another choice \n";

         //This is where the code does not work at all
         cin >> choice; //second input

         if (choice == 1) //this line is to get out of the loop but I never reach it
             valid = false;
     }
     if (choice == 2) //contains similar code to the 1st if
     {
     }
     if (choice == 3) //contains similar code to the 1st if
     {
     }
     if (choice == 4) //contains similar code to the 1st if
     {
     }
     else
     {
          cout << "This is not one the choices. Choose again!";
          cin >> choice;
     }
}

当用户选择第一个选项时,我只填写了第一个 if,因为其他 3 个 if 的行为方式相同,只是使用了不同的链接。以下是用户选择第一个选项时程序的行为方式。它可以很好地打开链接,然后要求用户再次按 1 以坚持使用此选项或做出其他选择。它总是在后面进入 else 条件。即使我按 1 退出循环,它也会进入 else。这意味着无论我输入什么输入,它都没有注册为 1 到 4 之间的整数。

我已尝试调试以查看“选择”在第二次输入之后的值,它显示为 49!这没有任何意义,因为我输入了 1。但是,在 else 之后,如果我在 1 和 4 之间做出正确的选择,例如 1,链接将再次打开就好了,程序将再次要求我按 1 坚持与我的选择或做出另一个选择。

我认为缓冲区上还有一些东西。我读了一些关于它以了解如何清除它,但我在第一个 cin 之后尝试了 cin.clear()、cin.ignore()、cin.sync() 但第二个输入总是与我实际输入的不同无论我输入什么,它都会再次进入 else 。这是一个来自地狱的无限循环。

我还是 C++ 新手,我正在尝试一些想法。我尝试自己解决这个问题,但我需要帮助。谢谢。

【问题讨论】:

  • 注意:即使您“验证输入了一个整数并且它在 1 和 4 之间”,您也永远不会检查流状态,这是完全错误的。
  • 流状态?你能详细说明一下吗?你可以告诉我我需要读什么,我会做的。
  • @Davel 尝试输入 'hello' 并调试
  • 49 是字符 '1' 的 ASCII 码,所以 cin &gt;&gt; choice 可能将输入读取为字符而不是整数值。但是,不清楚为什么您只会在第二个&gt;&gt; 上注意到这一点。我尽量不使用cin &gt;&gt; 作为输入,所以我能说的就这么多了。
  • 您确实到达了“我永远达不到”的那一行,但是您随后陷入了其他 if 案例

标签: c++ cin


【解决方案1】:
//Your control block was not correct. 
//Your logic falls into the else block after the second input 
//has been read because it use the new value of choice and run through all the
//if statements. In a case where you have multiple options in that manner use 'else if'` 

void start()
{
    bool valid = false;
    int choice;

    cout << "Please make a choice between 1 and 4\n"; 
    //Here I enumarate 4 choices
    cin >> choice;  //first input

    //note that I do verify that an integer is entered and that it's between 1 and 4
    //my code works so far

    valid = true; //if the choise is good

    while(valid)
    {
         if (choice == 1)
         {
             //here I open a url
             cout << "If you would like to stick with this choice, presse 1 again or make  another       choice \n";

             //This is where the code does not work at all
             cin >> choice; //second input

             if (choice == 1)
             {
                valid = false;
             } //this line is to get out of the loop but I never reach it

         }

         else if (choice == 2) //contains similar code to the 1st if
         {
         }
         else if (choice == 3) //contains similar code to the 1st if
         {
         }
         else if (choice == 4) //contains similar code to the 1st if
         {
         }
         else
         {
              cout << "This is not one the choices. Choose again!";
              cin >> choice;
         }
    }   
}
int main(void)
{
    start();
    return 0;
}

【讨论】:

  • 谢谢,我会在回家后(7 小时后)试试,但我有一个问题?你不觉得它不读这个很奇怪吗:if (choice == 1){ valid = false;}
  • 它读过!第二次提示用户选择具有新值时,您的程序会检查所有其他“if”块并与该值进行比较。因此,如果变量 'choice' 的新值与 'if' 语句中的任何值都不对应,那么它将始终落在 else 块中。
  • 谢谢。这就是解决方案。
【解决方案2】:

正如Codec所说,您的if else不正确,在您确认选择后,它会将您的选择与2、3和4的值进行比较。由于现在是1,它会进入else语句并执行那里的任何事情。我的建议是使用 switch 语句,这样会更容易阅读。

cin >> choice;

switch(choice)
{
    case 1:
    // do whatever you want
    break;
    case 2:
    // do whatever you want
    break;
    // continue with your choice
    default:
    // do whatever you want if the value falls out of your choice
    cout << "This is not the right choice" << endl;
    break;
}

【讨论】:

  • 我想到了。原因是我希望用户确认他的选择或做出另一个选择。我可以把开关变成一个循环。好的,如果if else 有效,我会切换到这个。谢谢。
【解决方案3】:

在你的 while 循环中使用如下:

while(valid)
   {
    if(choice==1)
      {
        //your code
      }
else if(choice==2)
      {
       //your code
      }
else if(choice==3)
      {
       //your code
      }
else if(choice==4)
      {
       //your code
      }
else 
      {
       //your code
      }
}

【讨论】:

  • 我在更正后检查了代码,它工作正常。改正后逻辑上没有错。这是他做的唯一错误,因为他没有得到他期望的结果。顺便说一句,您检查过代码一次吗?
  • 我已经检查过了。他明确指出问题出在cin 之后的选择变量的值上。逻辑可能有缺陷(或者正如他所说,他正在工作并且他写得很快,所以“原始”代码可能没问题),但这并没有改变输入 1 后得到 49 的事实
  • 您能否详细说明一下这个答案?通常不鼓励仅使用代码回答。
  • @prajmus 正是我的观点。即使我在第二次输入后立即输入cout &lt;&lt; choice,它也会显示 49。
  • 我不知道 49,但这个答案是正确的,因为代码应该使用 if...else if...else if。就目前而言,您正在为除 4 之外的任何值点击 else 子句,这不是您想要的。 49 告诉你什么?在else 子句中尝试打印语句。
猜你喜欢
  • 2017-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多