Cin没有得到正确的输入

时间:2014-06-25 19:15:00

标签: c++ cin

我在使用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;
     }
}

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

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

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

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

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

正如Codec所述,您的if else不正确,在您确认您的选择后,它会将您的选择与2,3和4的值进行比较。因为它现在是1,它将转到其他地方声明并执行其中的任何内容。我的建议是使用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;
}

答案 2 :(得分:-2)

在你的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
      }
}