#include <stdio.h>
#include <stdlib.h>
struct PCSpec{
int ComputerID;
char ComputerName[50];
float Price;
};
void PrintAll(int count,struct PCSpec Computer[100]);
int InputData(int count,struct PCSpec Computer[100]);
void Search(int count,struct PCSpec Computer[100]);
void SearchID(int count,struct PCSpec Computer[100]);
void SearchNAME(int count,struct PCSpec Computer[100]);
void MainMenu(int count,struct PCSpec Computer[100]);
void Option1Menu();
void Option2Menu();
void PrintSelective(struct PCSpec Computer[100]);
main()
{
int count;
struct PCSpec Computer[100];
printf("Please enter the Number Of Computer (Max is 100) ");
scanf("%d",&count);
system("cls");
Option1Menu();
MainMenu(count,Computer);
}
int InputData(int count,struct PCSpec Computer[100])
{
int i;
for(i=0;i<count;i++)
{
printf("Enter the ComputerID = ");
scanf("%d", &Computer[i].ComputerID);
printf("Enter the ComputerName, Maximum 50 Characters = ");
scanf(" %s",Computer[i].ComputerName);
}
printf("Data was SUCCESSFULLY recorded!");
}
void MainMenu(int count,struct PCSpec Computer[100])
{
while (1)
{
int option;
printf("\nEnter your choice : ");
scanf("%d", &option);
switch (option)
{
case 1:
system("cls");
InputData(count,Computer);
system("cls");
Option1Menu();
break;
case 2:
system("cls");
PrintAll(count,Computer);
Option1Menu();
break;
case 3:
system("cls");
Search(count,Computer);
case 4:
exit(0);
}
}
}
void Search(int count,struct PCSpec Computer[100])
{
int Option;
Option2Menu();
while(1)
{
printf("\nEnter your choice : ");
scanf("%d",&Option);
switch(Option)
{
case 1:
system("cls");
SearchID(count,Computer);
Option2Menu();
break;
case 2:
system("cls");
SearchNAME(count,Computer);
Option2Menu();
break;
case 3:
system("cls");
MainMenu(count,Computer);
case 4:
exit(1);
}
}
}
对于长代码,很抱歉,以下部分是我遇到问题的部分。我能够成功搜索,但我无法显示搜索到的结构。我最初的计划是搜索ComputerID,如果找到了,我希望它也能显示ComputerName。
如果找到ID,我想获得的输出。 电脑ID = 21 计算机名=桌面
void SearchID(int count,struct PCSpec Computer[100])
{
int i,id;
printf("Please Enter Computer ID ");
scanf("%d",&id);
for(i=0;i<count;i++)
{
if(Computer[i].ComputerID == id)
{
printf("%d is found at %d",id,i);
//PrintSelective(Computer);
break;
}
}
if( i == count )
printf("Unable to locate ComputerID\n");
}
对于本节,我的意图与上一节相同,但这次我想搜索ComputerName,发现它也会打印出ComputerID。
现在我遇到的问题是,我输入的输入无法找到。
void SearchNAME(int count,struct PCSpec Computer[100])
{
int j;
char NAME;
printf("Warning CASE Senstitive!\n");
printf("Please Enter Computer Name ");
scanf("%s",&NAME);
for(j=0;j<count;j++)
{
if(Computer[j].ComputerName == "NAME")
{
printf("%s is found at %d",NAME,j);
//PrintSelective(Computer);
break;
}
}
if( j == count )
printf("Unable to locate ComputerID\n");
}
其余几乎就是宣言
void PrintAll(int count,struct PCSpec Computer[100])
{
int j;
for(j=0;j<count;j++)
{
printf("Computer Package %d\n", j);
printf("Computer ID = \t\t%d\n",Computer[j].ComputerID);
printf("Computer Name = \t%s\n",Computer[j].ComputerName);
}
}
void PrintSelective(struct PCSpec Computer[100])
{
int i;
printf("Computer Package %d\n", i);
printf("Computer ID = \t\t%d\n",Computer[i].ComputerID);
printf("Computer Name = \t%s\n",Computer[i].ComputerName);
}
void Option1Menu()
{
printf("\n1] Create a Record\n");
printf("2] Display Records\n");
printf("3] Search a Record\n");
printf("4] Exit");
}
void Option2Menu()
{
printf("\n1] Search using ComputerID\n");
printf("2] Search using ComputerNAME\n");
printf("3] Back to Main Menu\n");
printf("4] Exit the Program\n");
}
很抱歉我以前的帖子我的问题不清楚,因为我还是C的新手,所有对我的编码的反馈都表示赞赏!另一件事是,你们使用了哪个编译器?或者C的标准,以便在编写我编写的代码时,其他人不会有任何问题。
答案 0 :(得分:1)
三件事:
使用scanf("%s",&NAME)
,您将字符串读入单个字符。非空字符串始终至少需要两个字符:至少一个字符串内容加上一个字符串终结符。一个49个字符的字符串需要一个50 char
的数组。
"NAME"
是文字字符串,而不是变量NAME
使用Computer[j].ComputerName == "NAME"
比较两个指针。使用Computer[j].ComputerName == NAME
,您将比较指向单个char
值的指针。如果将NAME
更改为数组,则再次比较两个指针。要比较字符串,您需要使用strcmp
。
答案 1 :(得分:1)
您需要将i
传递给PrintSelective
:
void PrintSelective(struct PCSpec Computer[100], int i)
{
printf("Computer Package %d\n", i);
printf("Computer ID = \t\t%d\n",Computer[i].ComputerID);
printf("Computer Name = \t%s\n",Computer[i].ComputerName);
}
然后将其命名为:
PrintSelective(Computer, i);
PrintAll
也应该致电PrintSelective
。
或者,我不会在PrintSelective
中打印索引,在这种情况下,请写为:
void PrintSelective(struct PCSpec *pComputer)
{
printf("Computer ID = \t\t%d\n",pComputer->ComputerID);
printf("Computer Name = \t%s\n",pComputer->ComputerName);
}
并致电:
PrintSelective(&Computer[i]);