2012-12-19 151 views
2

双输入,所以我想:麻烦与validaton C++程序

  1. 做一做里面的双输入验证while循环
  2. 检查是否此举尚未作出,是否有一个逻辑输入。 (#1-9)

我原本以为如果其他语句,但我不知道如何使用else语句返回到循环的开始。

do 
{ 
    cout << "Interesting move, What is your next choice?: "; 
    cin >> play; 
    Pused[1] = play; 
    if(play != Pused[0] && play != cantuse[0] && play != cantuse[1]) 
    { 
    switch(play) 
    { 
     default:cout << "Your choice is incorrect\n\n"; 
     break; 
    } 
    } 
    else 
    { } 
}  
while(play != 1 && play != 2 && play != 3 && play != 4 && play != 5 && play != 6 && play != 7 && play != 8 && play != 9); 
Dis_board(board); 
+4

请你的代码减少到最低限度,如果你希望人们读它 –

回答

0

使用“continue”关键字返回循环开始。

+0

它是一个DO WHILE和,因为它是真实的,num是1-9退出....它不检查看看1-9是否已经输入 – user1914650

0

刚删除else。我认为这不是必需的。如果条件满足while,自动循环将继续。

+0

它需要其他的,因为如果是说如果它没有进入他们做功能,否则.....我需要其他返回到if语句之上。 ......如果可能的话......或者如果可能的话,发给Do语句...... – user1914650

+0

如果你想在别的地方做一些操作,那么只有其他的地方是必需的。如果你的游戏值超过9,那么循环将继续,否则它会出来。此外,您可以使用while(!(play> 9))作为条件检查,而不是写入很大的while循环条件。 – Nipun

0

你的问题是有点难以理解,但你有几个条件,在这个循环来解决:

  1. 询问用户输入
  2. 检查用户输入有效(间1-9而不是之前)
  3. 退出循环使用了,如果我们有一个有效的选择

所以我们需要记录动作已经做了什么,并检查用户的输入是有效的选择中,我们可以使用仅在选择有效选择时退出的循环。

int choice; 
bool used[9] = { false }; // Set all values to false 
std::cout << "Interesting move, what is your next choice?: "; 
do { 
    std::cin >> choice; 
    // Here we check if the choice is within the range we care about 
    // and not used, note if the first condition isn't true then 
    // the second condition won't be evaluated, so if choice is 11 
    // we won't check used[10] because the && will short circuit, this 
    // lets us avoid array out of bounds. We also need to 
    // offset the choice by 1 for the array because arrays in C++ 
    // are indexed from 0 so used runs from used[0] to used[8] 
    if((choice >= 1 && choice <= 9) && !used[choice - 1]) { 
     // If we're here we have a valid choice, mark it as used 
     // and leave the loop 
     used[choice - 1] = true; 
     break; // Exit the loop regardless of the while condition 
    } 

    // If we've made it here it means the above if failed and we have 
    // an invalid choice. Restart the loop! 
    std::cout << "\nInvalid choice! Input: "; 
} while (true);