双重输入valtaton A C 程序的麻烦
Trouble with dual input validaton a C++ program
,所以我正在尝试:
- 在do内进行双输入验证时循环
- 检查是否已经进行了此举,以及它是否是逻辑输入。(#1-9)
我最初想到了是否有其他语句,但我不确定如何使用其他语句回到循环的开始。
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 incorrectnn";
break;
}
}
else
{ }
}
while(play != 1 && play != 2 && play != 3 && play != 4 && play != 5 && play != 6 && play != 7 && play != 8 && play != 9);
Dis_board(board);
使用"继续"关键字返回循环开始。
只需删除else
即可。我认为这不是必需的。如果满足while
中的条件,将自动继续进行循环。
您的问题很难理解,但是您有一些条件可以解决此循环:
- 向用户询问输入
- 检查用户输入是否有效(在1-9和之前未使用之前)
- 如果我们有有效的选择,请退出循环
因此,我们需要记录已完成的动作,还检查用户的输入是否在有效的选择范围内,我们可以使用仅在选择有效选择时退出的循环。
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);
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- C++击球平均值程序使用输入/输出文件打印名称,平均值,最高/最低平均值时遇到麻烦
- 我在这个程序上遇到了麻烦.只有1个编译错误
- C++处理文件,在制作程序时遇到麻烦
- 类C 反向程序的麻烦
- 双重输入valtaton A C 程序的麻烦
- 编译phash程序时遇到麻烦
- 在将字符串转换为int数组时,在putty上运行程序会遇到麻烦
- 在我的c++应用程序中包含第三方代码时遇到麻烦