做与循环和&没有正确循环c ++

时间:2016-09-10 03:20:52

标签: c++ do-while binary-operators

当输入1到7之间的值时,程序应检查条件,如果值为1到7,并且输入的数字数组中的值不是0,但该程序的问题是,如果值[0]或其他数组为0,程序没有循环它继续运行

    do
            {
              cout << "Player 1\n";
              cout << "Please choose your location (1 to 7) => ";
              cin >> h1in ; // player 1 enters a value
              cin.clear();// used to clear the value if a str value is entered
              cin.ignore(1000,'\n');// used to clear the value if a str value is entered
              if ((h1in<1)||(h1in>7))
              {
                  cout << "You entered the wrong input \n\n";
              }
              if(house[h1in-1]==0)
              {
                  cout<<"jde";
              }

            } while((house[h1in-1]!=0)&(h1in<1)||(h1in>7));
if (h1in==1)
         {
           for(int i=1;i<7;i++){

            house[0]--;
            house[i]++;
            if(i==6){
                storehouse[0]++;
                seeds--;
                house[0]--;
            }
            printout();

           }

           }
          else if(h1in==2)
          {
             for(int i=1;i<7;i++){
               house[1]--;
               house[i+1]++;
               if(i==6){
                storehouse[0]++;
                seeds--;
                house[1]--;

               }
               printout();
             }
          }
          else if(h1in==3)
          {
              for(int i=1;i<7;i++)
              {
                  house[2]--;
                  house[i+2]++;
                  if(i==6){
                    storehouse[0]++;
                    seeds--;
                    house[2]--;
                  }
                  printout();
              }
          }
          else if(h1in==4)
          {
              for(int i=1;i<7;i++)
              {
                  house[3]--;
                  house[i+3]++;
                  if(i==6){
                    storehouse[0]++;
                    seeds--;
                    house[3]--;
                  }
                  printout();
              }
          }
           else if(h1in==5)
           {
               for(int i=1;i<7;i++)
               {
                  house[4]--;
                  house[i+4]++;
                  if(i==6){
                    storehouse[0]++;
                    seeds--;
                    house[4]--;
                  }
                  printout();
               }
           }
           else if(h1in==6)
             {
               for(int i=1;i<7;i++)
               {
                  house[5]--;
                  house[i+5]++;
                  if(i==6){
                    storehouse[0]++;
                    seeds--;
                    house[5]--;
                  }
                  printout();
               }
             }
            else if(h1in==7)
            {
                for(int i=1;i<7;i++)
                {
                  house[6]--;
                  house[i+6]++;
                  if(i==6){
                    storehouse[0]++;
                    seeds--;
                    house[6]--;
                  }
                  printout();
                }
            }

1 个答案:

答案 0 :(得分:2)

&是按位and。它在操作数的每个位上预先形成二进制and。您需要&&运算符,它是逻辑and

您还应该添加一些括号来制作最后一行:

while( (house[h1in-1]!=0) && ((h1in<1)||(h1in>7)));