Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 搜索函数找不到结构中的第一个元素_C - Fatal编程技术网

C 搜索函数找不到结构中的第一个元素

C 搜索函数找不到结构中的第一个元素,c,C,我被要求为类编写一个函数,该类允许用户搜索结构数组。函数要求用户输入ID,如果找到,则应显示结构的其他部分。除数组中的第一个元素([0])外,此操作非常有效。我一直得到-1的回报,或者a未找到,我似乎不知道为什么 int i = 0; for(; i < totalEng; i++)//loop to find the engine ID { if(search == (peng + i)->id) return i; } return - 1; 感谢您的

我被要求为类编写一个函数,该类允许用户搜索结构数组。函数要求用户输入ID,如果找到,则应显示结构的其他部分。除数组中的第一个元素([0])外,此操作非常有效。我一直得到-1的回报,或者a未找到,我似乎不知道为什么

int i = 0;
for(; i < totalEng; i++)//loop to find the engine ID
{
    if(search == (peng + i)->id)
        return i;
}

return - 1;
感谢您的帮助

编辑:这是填充数组的我的函数

void populateEng(engine* peng)
{
//--------------------
(peng + 0)->id   = 111;
(peng + 0)->hp   = 500;
(peng + 0)->size = 4.5;
//---------------------
(peng + 1)->id   = 222;
(peng + 1)->hp   = 120;
(peng + 1)->size = 1.2;
//---------------------
(peng + 2)->id   = 333;
(peng + 2)->hp   = 300;
(peng + 2)->size = 6.5;
//---------------------
(peng + 3)->id   = 444;
(peng + 3)->hp   = 95;
(peng + 3)->size = 1.5;
//---------------------
(peng + 4)->id   = 555;
(peng + 4)->hp   = 200;
(peng + 4)->size = 2.5;
//---------------------
(peng + 5)->id   = 666;
(peng + 5)->hp   = 700;
(peng + 5)->size = 5.0;
//---------------------
}
这是整个搜索功能

int searchEng(engine* peng, int totalEng)
{
char input[5];
char* pinput = NULL;
int i = 0;
int length = 0;
int search = 0;

printf("Enter the ID of the engine you wish to search for: ");
pinput = fgets(input, buffer, stdin);
length = strlen(input);

 for(; i < length - 1; i++)//making sure the user enters only 3-4 numbers
{
    if(length > 5 || length < 3)
    {
    i = 0;
    printf("Invalid input. Please enter up to 4 numbers only: ");
    pinput = fgets(input, buffer, stdin);
    length = strlen(input);
    }
    if(input[i] < '0' || input[i] > '9')
    {
    i = 0;
    printf("Invalid input. Please enter up to 4 numbers only: ");
    pinput = fgets(input, buffer, stdin);
    length = strlen(input);
    }

}

search = atoi(input);//converting the input into an integer to search


for(i = 0; i < totalEng; i++)//loop to find the engine ID
{
    if(search == (peng + i)->id)
        return i;
}

return - 1;


}//end searchEng
edit3:谢谢大家的耐心,我应该先把这些东西都贴出来。我可以做一个新的职位与一切在一个职位,如果这将是首选。还有,我现在正在读那篇文章。再次感谢!我不会再犯同样的错误了

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define buffer 10
#包括
#包括
#包括
#定义缓冲区10

编辑4:谢谢你,蓝精灵。这确实更有意义。我只是按照教授教我们的方式做的。我将把那篇文章发出去,然后用这些方法调试程序。如果我还有问题,我会让你知道。我希望我没有给人留下懒散和寻找廉价答案的印象。只是想确保我没有遗漏任何明显的东西。谢谢

我无法理解您是如何填充结构数组,然后调用搜索函数的。你能显示更多的代码吗?是的。对不起,我不想发布太多代码,让它看起来很邋遢。我对这一切还是新手。我现在将编辑我的文章,提供更多信息。我需要查看你的代码的更多部分添加你的文章,而不是评论。请阅读。
struct engine
{
int id;
int hp;
double size;

}typedef engine;

int main()
{
//declarations:
engine eng[100];
engine* peng = &eng[0];
...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define buffer 10