我必须在 c++ 中使用标志吗?

Do I have to use flag in c++?

本文关键字:标志 c++      更新时间:2023-10-16

这是我面临的问题。用户要输入一个数字,如果该数字在数组中,我想将其打印为有效数字,否则我想将其打印为无效数字。

但是,此代码不起作用;它只是打印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;