【发布时间】:2017-07-12 08:46:57
【问题描述】:
#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 标准,以便其他人在编译我编写的代码时不会有任何问题。
【问题讨论】:
-
Computer[j].ComputerName == "NAME"不起作用。您需要strcmp来比较 C 中的字符串。