C 为什么我的程序不能在数组中搜索?
我的程序允许我添加尽可能多的项目,直到您达到最大值,当您完成所有操作时,它会显示您输入的所有信息。当你去显示一个,当我搜索数组中的第一个点时,它会弹出,但当我搜索我输入的第四个或第五个项目后,它会说它没有找到,但它显然在那里。任何帮助C 为什么我的程序不能在数组中搜索?,c,C,我的程序允许我添加尽可能多的项目,直到您达到最大值,当您完成所有操作时,它会显示您输入的所有信息。当你去显示一个,当我搜索数组中的第一个点时,它会弹出,但当我搜索我输入的第四个或第五个项目后,它会说它没有找到,但它显然在那里。任何帮助 #include <stdio.h> #include <stdlib.h> #define MAX 12 //Structed Items struct item{ char itemname[20]; c
#include <stdio.h>
#include <stdlib.h>
#define MAX 12
//Structed Items
struct item{
char itemname[20];
char itemdes[30];
int itemID;
int itemOH;
double itemUP;
};
// Function Declarations
int getMenu_Choice ();
int process (int choice, int count, struct item inven[]);
int add (int count, struct item inven[]);
int showall(int count, struct item inven[]);
int find(int count, struct item inven[]);
int main (void)
{ // OPENS MAIN
// Declarations
int choice;
struct item inven[MAX];
int count = 0;
// Statements
do//
{
choice = getMenu_Choice ();
count = process (choice, count, inven);
}
while (choice != 0);
return 0;
} // CLOSE MAIN
/*============================getChoice=*/
int getMenu_Choice (void)
{ //OPEN GETCHOICE
// Declarations
int choice;
// Statements
printf("\n\n**********************************");
printf("\n MENU ");
printf("\n\t1.Create A New Item ");
printf("\n\t2.View All Items ");
printf("\n\t3.View One Item ");
printf("\n\t0.Exit ");
printf("\n**********************************");
printf("\nPlease Type Your Choice Using 0-3");
printf("\nThen Hit Enter: ");
scanf("%d", &choice);
return choice;
} //CLOSES GET CHOICE
/*============================process=*/
int process (int choice, int count, struct item inven[])
{// OPEN PROCESS
// Declarations
// Statements
switch(choice)
{
case 1: count = add(count, inven);
break;
case 2: showall(count, inven);
break;
case 3: find(count, inven);
break;
case 0: exit;
break;
deafult: printf("Sorry Option Not Offered");
break;
} // switch
return count;
} // CLOSE PROCESS
/*============================add one=*/
int add(int count, struct item inven[])
{//OPENS CREATE
// Declarations
int i;
i = count;
if (i < MAX)
{
printf("Enter the Item ID:\n");
scanf("%d", &inven[i].itemID);
printf("Enter the Item Name:\n");
scanf("%s", inven[i].itemname);
i++;
}
else {
printf("sorry there is no more room for you to add");
};
return i;
}; // CLOSE CREATE
/*============================showall=*/
int showall(int count, struct item inven[])
{
//Declarations
int i;
// Statements
for(i = 0; i < count; i++)
{
printf("\nItem ID : %d", inven[i].itemID);
printf("\nItem Name : %s", inven[i].itemname);
};
return 0;
};
/*============================find one=*/
int find(int count, struct item inven[])
{
//Declarations
int i;
int search;
int found;
printf("Enter the Item ID to search\n");
scanf("%d", &search);
for (i = 0; i < count; i++)
{
if(inven[i].itemID == search)
{
printf("\nItem ID : %d", inven[i].itemID);
printf("\nItem Name : %s", inven[i].itemname);
break;
}
else {
printf("\nSorry None existent");
break;
}
}
return 0;
};
#包括
#包括
#定义最大值12
//结构化项目
结构项{
char itemname[20];
charitemdes[30];
int itemID;
int itemOH;
双倍递增;
};
//函数声明
int getmenuu选项();
int进程(int选择、int计数、结构项inven[]);
int add(int count,结构项inven[]);
int showall(int count,结构项inven[]);
int find(int count,结构项inven[]);
内部主(空)
{//打开主
//声明
智力选择;
结构项库存【最大值】;
整数计数=0;
//声明
做//
{
choice=getmenuu choice();
计数=过程(选择、计数、inven);
}
while(选项!=0);
返回0;
}//关闭主管道
/*==============================================getChoice=*/
int getMenu_选项(无效)
{//opengetchoice
//声明
智力选择;
//声明
printf(“\n\n****************************************”);
printf(“\n菜单”);
printf(“\n\t1.createanewitem”);
printf(“\n\t2.查看所有项目”);
printf(“\n\t3.查看一项”);
printf(“\n\t0.Exit”);
printf(“\n*************************************”);
printf(“\n请使用0-3键入您的选择”);
printf(“\n然后点击回车:”);
scanf(“%d”,选择(&C);
回报选择;
}//关闭获取选择
/*========================================处理=*/
int进程(int选项、int计数、结构项inven[])
{//开放进程
//声明
//声明
开关(选择)
{
案例1:count=add(count,inven);
打破
案例2:showall(count,inven);
打破
案例3:查找(计数,inven);
打破
案例0:退出;
打破
deafult:printf(“对不起,未提供期权”);
打破
}//开关
返回计数;
}//关闭进程
/*===========================================添加一个=*/
整数添加(整数计数,结构项库存[])
{//打开创建
//声明
int i;
i=计数;
如果(i
此部分错误:
for (i = 0; i < count; i++)
{
if(inven[i].itemID == search)
{
printf("\nItem ID : %d", inven[i].itemID);
printf("\nItem Name : %s", inven[i].itemname);
break;
}
else {
printf("\nSorry None existent");
break;
}
}
调试此类问题的方法是使用调试器逐步检查代码,检查每一步的状态。我以前从未使用过调试器。我假设这是一个简单的修复,不需要这样做,如果您无法通过代码检查看到问题所在,那么无论最终修复是否“简单”,都需要一个调试器。不能对每个小问题都使用Stackoverflow,所以最好现在就学习使用调试器-这将是很好的时间。始终正确缩进代码!这对你会有很大帮助。顺便说一句,那些
}
末尾的code>是不必要的。为什么showall
和find
返回int
?为什么不将返回类型作废?退出代码>→ <代码>退出(0)代码>,死亡:
→ <代码>默认值:
当我搜索我输入的第二个或第三个项目时,它告诉我它不在那里,但是如果你在视图中查看它,它会显示出来。这告诉我你没有正确更新下一个术语的搜索
?
for (i = 0; i < count; i++)
{
if(inven[i].itemID == search)
{
printf("\nItem ID : %d", inven[i].itemID);
printf("\nItem Name : %s", inven[i].itemname);
break;
}
else
{
printf("\nSorry None existent");
break;
}
}
for (i = 0; i < count; i++)
{
if(inven[i].itemID == search) /* break only if condition is true. Otherwise continue looping */
{
printf("\nItem ID : %d", inven[i].itemID);
printf("\nItem Name : %s", inven[i].itemname);
break;
}
}
if(i == count) /* Will be true if the loop executed without executing the break */
{
printf("\nSorry None existent");
}