C++ 程序检查以查看用户编号是否存储在c++;
我编写此代码是为了检查用户输入的正整数是否存储在程序数组中,然后将结果显示给用户。我对C++非常陌生,而且遇到了一些困难。谁能帮帮我吗C++ 程序检查以查看用户编号是否存储在c++;,c++,arrays,user-defined-functions,C++,Arrays,User Defined Functions,我编写此代码是为了检查用户输入的正整数是否存储在程序数组中,然后将结果显示给用户。我对C++非常陌生,而且遇到了一些困难。谁能帮帮我吗 #include <iostream> //needed for access to cin and cout using namespace std; int main (void) { int LS (int [], int); int ll=10; int l[ll]= {30, 2, 13, 9, 25, 17,
#include <iostream> //needed for access to cin and cout
using namespace std;
int main (void)
{
int LS (int [], int);
int ll=10;
int l[ll]= {30, 2, 13, 9, 25, 17, 89, 32, 73, 69};
int i=0;
int it=0;
int result=0;
cout << " Welcome User, this program allows you to enter a number"<<endl
<< " and see if it is stored in our database."<<endl;
cout << " Please enter number : ";
cin >> it;
result = LS (l[ll], it);
if (result==0)
{
cout << " The number" << it << " you entered was not found in our database. " << endl;
}
if (result==1)
{
cout << " The number" << it << " you entered has been found in our database. " << endl;
}
return 0;
}
int LS (int l[int ll], int it) //function to search for "it"
{
int i=0, f=0;
if (l[i]==it)
{
f=1;
i++;
}
if (i==ll)
{
return f;
}
}
#包括//访问cin和cout所需
使用名称空间std;
内部主(空)
{
int-LS(int[],int);
int ll=10;
int l[ll]={30,2,13,9,25,17,89,32,73,69};
int i=0;
int it=0;
int结果=0;
cout您缺少一个循环来遍历数组中的所有值
#include <iostream> //needed for access to cin and cout
using namespace std;
int main (void)
{
int LS (int [], int);
int ll=10;
int l[ll]= {30, 2, 13, 9, 25, 17, 89, 32, 73, 69};
int i=0;
int it=0;
int result=0;
cout << " Welcome User, this program allows you to enter a number"<<endl<< " and see if it is stored in our database."<<endl;
cout << " Please enter number : ";
cin >> it;
result = LS (l, it);
if (result==0)
{
cout << " The number" << it << " you entered was not found in our database. " << endl;
}
if (result==1)
{
cout << " The number" << it << " you entered has been found in our database. " << endl;
}
return 0;
}
int LS (int l[10], int it) //function to search for "it"
{
int i=0, f=0;
for(i=0;i<10;++i) {
if (l[i]==it)
{
f=1;
return f;
}
return f;
}
}
#包括//访问cin和cout所需
使用名称空间std;
内部主(空)
{
int-LS(int[],int);
int ll=10;
int l[ll]={30,2,13,9,25,17,89,32,73,69};
int i=0;
int it=0;
int结果=0;
cout进行许多更改以获得更好的代码:
- 使用布尔值返回真/假语句
- 使用更明确的函数/变量名
- 正确执行for循环
-
#define ARRAY_SIZE 10
bool findItem( int item, int* items, size_t count )
{
for ( size_t i = 0; i != count; ++i )
{
if ( items[i] == item )
return true;
}
return false;
}
int main (void)
{
int l[ARRAY_SIZE] = {30, 2, 13, 9, 25, 17, 89, 32, 73, 69};
cout << " Welcome User, this program allows you to enter a number"<<endl<< " and see if it is stored in our database."<<endl;
cout << " Please enter number : ";
int toFind;
cin >> toFind;
if ( !findItem( toFind, l, ARRAY_SIZE ) )
{
cout << " The number" << toFind << " you entered was not found in our database. " << endl;
}
else
{
cout << " The number" << toFind << " you entered has been found in our database. " << endl;
}
return 0;
}
#定义数组大小10
布尔findItem(整数项,整数*项,大小\u t计数)
{
对于(大小i=0;i!=count;++i)
{
如果(项目[i]==项目)
返回true;
}
返回false;
}
内部主(空)
{
intl[ARRAY_SIZE]={30,2,13,9,25,17,89,32,73,69};
我在这里发现了一些问题
< P > 1)我们需要在所有函数之外提供函数声明。因此int LS(int [],int)应该在主体之外。函数DECUREN是C++函数的编译器,它将函数LS取2个参数a)一个整数数组b)一个整数< /p>
2) 函数无法自动确定数组中的元素数。因此,我们需要将数组中的元素数发送给函数LS。因此,函数声明应更改如下
int LS (int [], int, int);
第二个参数是数组中的元素数
3) 数组中只放置了10个元素,因此不需要在main()中将数组大小保持为11
4) 最后,您的函数缺少在数组中循环的逻辑
所以,找到下面的程序,这是你的修改版本
#include <iostream> //needed for access to cin and cout
using namespace std;
int LS(int [], int, int);
int main (void)
{
int ll=10;
int l[l0]= {30, 2, 13, 9, 25, 17, 89, 32, 73, 69};
int i=0;
int it=0;
int result=0;
cout << " Welcome User, this program allows you to enter a number"<<endl
<< " and see if it is stored in our database."<<endl;
cout << " Please enter number : ";
cin >> it;
result = LS (l, 10, it);
if (result==0)
{
cout << " The number" << it << " you entered was not found in our database. " << endl;
}
if (result==1)
{
cout << " The number" << it << " you entered has been found in our database. " << endl;
}
return 0;
}
int LS(int L[], int n, int it)
{
for(int i = 0; i < n; i++)
{
if (l[i]==it)
{
return 1;
}
}
return 0;
}
#包括//访问cin和cout所需
使用名称空间std;
int LS(int[],int,int);
内部主(空)
{
int ll=10;
intl[l0]={30,2,13,9,25,17,89,32,73,69};
int i=0;
int it=0;
int结果=0;
cout首先,你应该知道你的生活极其复杂。只需使用std::find
,它可以很好地处理数组,然后就可以使用它了!下面是如何使用普通的标准C++11编写程序:
#include <iostream> // for std::cout and std::cin
#include <algorithm> // for std::find
#include <iterator> // for std::begin and std::end
int main()
{
int const numbers[] = {30, 2, 13, 9, 25, 17, 89, 32, 73, 69 };
std::cout << " Welcome User, this program allows you to enter a number\n"
" and see if it is stored in our database.\n";
std::cout << " Please enter number : ";
int input;
std::cin >> input;
if (std::cin)
{
if (std::find(std::begin(numbers), std::end(numbers), input) == std::end(numbers))
{
std::cout << " The number " << input << " you entered was not found in our database.\n";
}
else
{
std::cout << " The number " << input << " you entered has been found in our database.\n";
}
}
else
{
// invalid input...
}
}
这是使用GCC特定的编译器扩展。在便携标准C++中,数组大小必须在编译时知道,而<>代码> L/<代码>只在运行时知道。
幸运的是,您甚至不必指定大小。这是C++中原始数组为数不多的优点之一:
int l[] = {30, 2, 13, 9, 25, 17, 89, 32, 73, 69};
下一个问题:
int LS (int l[int ll], int it) //function to search for "it"
撇开专有编译器扩展不谈,这可能是:
int LS (int l[], int size, int it)
当您将数组传递给这样的函数时,任何大小信息都会丢失,因此需要一个额外的大小参数。调用该函数的过程如下所示:
result = LS (l, sizeof(l) / sizeof(int), it);
最后,在LS
函数中,您不是在数组上迭代,而是比较单个元素。以下是对原始代码的可能修改:
int LS (int l[], int size, int it)
{
int i=0, f=0;
while (i < size)
{
if (l[i]==it)
{
f=1;
}
i++;
}
return f;
}
int-LS(int-l[],int-size,int-it)
{
int i=0,f=0;
而(i
它仍然有很多问题,但这只是一个开始
故事的寓意:
- 使用
中的函数。它们也可用于数组
< >代码> STD::向量< /> >和<代码> STD::数组每当您可以。您几乎不应该看到原始数组在现代C++代码中。
- 原始数组为数不多的好特性之一是编译器在声明时自动对元素进行计数
#包括//访问cin和cout所需
使用名称空间std;
内部主(空)
{
int-LS(int[],int);
int ll=10;
int l[ll]={30,2,13,9,25,17,89,32,73,69};
int i=0;
int it=0;
int结果=0;
非常感谢你解决了我的问题。我想我只是没有想清楚。他的数组有10个元素。
result = LS (l, sizeof(l) / sizeof(int), it);
int LS (int l[], int size, int it)
{
int i=0, f=0;
while (i < size)
{
if (l[i]==it)
{
f=1;
}
i++;
}
return f;
}
#include <iostream> //needed for access to cin and cout
using namespace std;
int main (void)
{
int LS (int [], int);
int ll=10;
int l[ll]= {30, 2, 13, 9, 25, 17, 89, 32, 73, 69};
int i=0;
int it=0;
int result=0;
cout << " Welcome User, this program allows you to enter a number"<<endl
<< " and see if it is stored in our database."<<endl;
cout << " Please enter number : ";
cin >> it;
result = LS (l[ll], it);
if (result==0){
cout << " The number" << it << " you entered was not found in our database. " << endl;
}
if (result==1)
{
cout << " The number" << it << " you entered has been found in our database. " << endl;
}
return 0;
}
int LS (int l[int ll], int it) //function to search for "it"{
int i=0;
while (l[i]!=it and i<11){
i++;
}
if (i==ll)
return 0;
return 1;
}