我必须在 c++ 中使用标志吗?
Do I have to use flag in c++?
这是我面临的问题。用户要输入一个数字,如果该数字在数组中,我想将其打印为有效数字,否则我想将其打印为无效数字。
但是,此代码不起作用;它只是打印Not Valid
我查看了其他类似的代码。他们使用了bool
旗帜。
为什么我需要在此程序中使用标志,何时需要使用标志?
#include<iostream>
using namespace std;
int main()
{
const int SIZE=6;
int arry[SIZE]={5658845,4520125,1230493,4856970,2078594,2393741};
int number;
cout <<"Enter the number"<<endl;
for(int i=0; i<SIZE; i++)
{
cin>>number;
if(arry[i]==number)
cout<<"This is valid number"<<endl;
else
cout << "This is not valid number"<<endl;
}
return 0;
}
代码的实现方式是,您在搜索数组时读取用户的输入。 您已经实现了一个猜谜游戏:"第一个数字正好是 X 吗? "第二个数字正好是X吗?"数组中的每个数字以此类推。
这就是你想要的吗?如果没有,如果您希望用户只输入 1 个数字,然后检查该数字是否存在于数组中的任何位置,则需要在进入搜索循环之前读取用户的输入(这是bool
发挥作用的时候(,例如:
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 6;
const int arry[SIZE] = {5658845, 4520125, 1230493, 4856970, 2078594, 2393741};
int number;
cout << "Enter the number" << endl;
cin >> number;
bool found = false;
for(int i = 0; i < SIZE; ++i)
{
if (arry[i] == number)
{
found = true;
break;
}
}
if (found)
cout << "This is valid number" << endl;
else
cout << "This is not valid number" << endl;
return 0;
}
或者,使用std::find()
算法而不是手动搜索:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
const int SIZE = 6;
const int arry[SIZE] = {5658845, 4520125, 1230493, 4856970, 2078594, 2393741};
int number;
cout << "Enter the number" << endl;
cin >> number;
const int *end = arry + SIZE;
if (find(arry, end, number) != end)
cout << "This is valid number" << endl;
else
cout << "This is not valid number" << endl;
return 0;
}
我的代码不起作用
如果要检查值是否在数组中,请检查数组中的所有元素。 但你没有。
我必须在 c++ 中使用标志吗?
标志代表状态。因此,在许多代码中,定义标志并将其用于选项。
喜欢这个
const int SINGLE_THREAD = 0;
const int MULTI_THREAD = 1;
/...
bool select_server_type(int option)
{
//....
}
select_server_type(SINGLE_THREAD);
但是,您不必在所有情况下都使用标志。
在这种情况下,您可以像这样检查它们。
(我知道这是非常不安全的代码。但是在你的关卡中,这段代码将是可以理解的代码(
#include<iostream>
using namespace std;
//check all element in array
bool match(int* arr, int size, int value)
{
for(int i = 0; i < size; ++i)
{
if(arr[i] == value)
return true;
}
return false;
}
int main()
{
const int SIZE=6;
int arry[SIZE]={5658845,4520125,1230493,4856970,2078594,2393741};
int number;
cout <<"Enter the number"<<endl;
cin>>number;
if(match(arry, SIZE, number))
cout<<"This is valid number"<<endl;
else
cout << "This is not valid number"<<endl;
}
只要你想看到是否发生实例或条件,就可以使用标志。您可以按如下方式编写程序。实际上,flag只是一个变量,您可以在其中保存条件以进一步检查。
const int SIZE=6;
int arry[SIZE]={5658845,4520125,1230493,4856970,2078594,2393741};
int number;
cout <<"Enter the number"<<endl;
cin >> number;
int flag = 0;
for(int i=0; i<SIZE; i++{
if(arry[i]==number){
flag=1;
break;
}
}
if(flag==1)
cout<<"This is valid number"<<endl;
else
cout << "This is not valid number"<<endl;
在这里,我们必须检查循环是否为真。因此,我们使用标志,以便我们可以在循环之外获得结果。
在这种情况下,您还可以执行以下操作:
int flag=0;
for(int i=0; i<SIZE; i++{
if(arry[i]==number){
cout<<"This is valid number"<<endl;"
flag=1;
break;
}
}
if(flag!=1)
cout << "This is not valid number"<<endl;
相关文章:
- 即使使用调试编译标志,表达式也是"optimized out"
- 在 CMake 中为每个目标设置编译器/链接器标志
- File.cpp.o:OpenPose 标志 CMakeFiles/.. 的多重定义/main.cpp.o:首先在这里定
- 在轮班操作后使用携带标志
- 如何找出引入AVX标志的内容
- I2C 文件描述符上的 I2C 总线可写/可读标志
- C ++是否有C ++ 17 OSX 10.13.6的标志
- 每次使用带有LOCK_EX标志的LOCK_NB时,相同的程序/进程都会获取锁
- CMake 3.5 中的链接器标志位置
- 如何使用 gnu gcc 标志 -mpc32、-mpc64 和 -mpc80?
- 在 CMake 中添加全局编译标志
- -Wlifetime 标志的目的是什么?
- Visual Studio 19-17 库兼容性根据 GL 标志
- 标志 |= f == s[i],这个语句会做什么?
- Visual Studio 中是否有来自代码块的编译器标志的类似物?
- 如何在 clang 的自动会议中检查支持编译标志
- 使用 boost::p rogram_options 指定多个标志
- 如何告诉 gcc 显示您使用的优化标志列表
- 生成文件中隐式规则中的 -c 标志出错
- 如何将编译器标志限制为仅(我的)本地库?