如何检查cin是否与数组中的一组数字匹配
How to check if cin matches a set of numbers in an array?
此代码从1-6生成5个随机数。然后,它将数字存储到一个包含5个数字的数组中。然后,它会提示用户输入这五个数字。如何检查"cin"是否与数组中的数字匹配?
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;
int magicNumbers;
int code;
int array[5];
void enterCode()
{
cout << "nnnnEnter your given code: ";
cin >> code;
if(code == array[])
{
cout << "Yes!";
}
else if(code != array[])
{
cout << "No!";
}
}
int main()
{
srand(time(NULL));
for(int x = 0; x < 5; x++)
{
magicNumbers = 1+rand()%6;
array[x] = magicNumbers;
cout << magicNumbers << endl;
}
cout << "nThe Array" << endl;
cout << "-----------------" << endl;
cout << array[0] << array[1] << array[2] << array[3] << array[4];
Sleep(2000);
enterCode();
return 0;
}
您可以通过创建第二个数组来分别询问用户代码的每个数字,并用用户的输入填充。然后将每个数组一次比较一项。
如果你仍然想一次要求所有代码,你需要将代码的每个数字分割成一个数组:-可以使用字符串流将数字转换为字符串,然后逐字符拆分-你可以直接拆分数字,例如使用这里描述的技术:C:如何将多位数拆分为单独的变量?
相关文章:
- 在 c++ 中拥有一组结构的正确方法是什么?
- 有哪些有效的方法可以消除一组 100 万个字符串>重复数据?
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 如何检查cin是否与数组中的一组数字匹配
- C :优雅地迭代一组数字
- 测试结果乘加减除法可能为一组数字
- 将一组数字相加或相减以达到一个值
- 需要帮助找到一组数字的数量最多和最小数量
- 如何使用伪随机数生成器生成一组特定的数字
- 查找一组数据中的最小数字
- 对一组数字进行排序的最快数据结构(和排序算法)是什么
- 查找最窄区间的算法,其中 m 将覆盖一组数字
- C++,要求用户输入一组数字求和(未知的输入数量),但是数字不会加起来
- 从一组数字中找出最接近的三个数字值
- C++ 输出一组数字的总和和乘积
- 求一组数字的GCD
- 如何将一组动作应用于一组数字
- 类似于MS Excel中的图表功能,给定一组数字,生成数学方程来描述这组数字
- 从一组集合{{中选择一组数字{1,2,3}.},{.},{.}}使得所有元素都是唯一的
- 在c++或Python中,用最少暴力的方法来查看一组数字中的多少组数字与给定的数字相加