Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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_Loops_While Loop_Structure - Fatal编程技术网

C 在用户输入某些信息后,如何搜索结构并显示这些信息?

C 在用户输入某些信息后,如何搜索结构并显示这些信息?,c,loops,while-loop,structure,C,Loops,While Loop,Structure,对于我的作业,我要创建一个允许用户输入学生信息(ID、DOB和电话号码)的结构。我做这个很简单,没有问题。现在我需要使用学生ID搜索输入信息,以显示学生对应的DOB和电话号码,这就是我遇到的问题。如果您发现我的程序有任何其他问题,请让我知道什么是错误的,为什么我应该改变,这样我就可以从错误中吸取教训 多谢各位 #include <stdio.h> #include <stdlib.h> struct infoStruct { int studentID;

对于我的作业,我要创建一个允许用户输入学生信息(ID、DOB和电话号码)的结构。我做这个很简单,没有问题。现在我需要使用学生ID搜索输入信息,以显示学生对应的DOB和电话号码,这就是我遇到的问题。如果您发现我的程序有任何其他问题,请让我知道什么是错误的,为什么我应该改变,这样我就可以从错误中吸取教训

多谢各位

#include <stdio.h>
#include <stdlib.h>

struct infoStruct 
{
    int studentID;
    int year;
    int month;
    int day;
    int phone;
    int end;
};

int main (void)
{
int students = 0;   
int infoArray [students];
struct infoStruct info;
    int studentID;
    int year;
    int month;
    int day;
    int phone;
    int end;



while (info.end != -1) {
students = students + 1;
printf("Enter student information (ID, day, month, year, phone)\n");
printf("Enter -1 following the phone number to end the process to continue enter 0\n");
scanf("%d %d %d %d %d %d", &info.studentID, &info.day, &info.month, &info.year, &info.phone, &info.end);
}
if (info.end = -1){
printf("You entered %d student(s)\n", students);
}
//Student Search
printf("Please enter the student ID of the student your looking for\n.");
scanf("%d", info.studentID);
printf(" DOB: %d %d %d, Phone: %d", info.month, info.day, info.year, info.phone);

}
#包括
#包括
结构信息结构
{
国际学生;
国际年;
整月;
国际日;
国际电话;
内端;
};
内部主(空)
{
int学生=0;
int infoArray[学生];
结构信息结构信息;
国际学生;
国际年;
整月;
国际日;
国际电话;
内端;
while(info.end!=-1){
学生=学生+1;
printf(“输入学生信息(ID、日期、月份、年份、电话)\n”);
printf(“在电话号码后输入-1以结束过程,继续输入0\n”);
scanf(“%d%d%d%d%d%d%d”、&info.studentID、&info.day、&info.month、&info.year、&info.phone、&info.end);
}
如果(info.end=-1){
printf(“您输入了%d名学生,\n”,学生);
}
//学生搜索
printf(“请输入您要查找的学生的学生ID\n.”;
scanf(“%d”,信息学生ID);
printf(“日期:%d%d%d,电话:%d”,信息月,信息日,信息年,信息电话);
}

info.end在while之前未初始化(info.end!=-1)。初始化所有变量(studentID…)和结构

如果(info.end=-1)是一个赋值

Use:if(info.end==-1)我更喜欢使用if(-1==info.end)(如果您使用了Use:only=而不是==您将得到一个错误)。(尤达把戏^^)

为了保存每个学生,你必须使用一个struct数组(因为你在不断地删除以前的学生信息)


这是你的家庭作业,我不会为你做这项工作;)

我将把大部分代码留给您,因为这是家庭作业,但以下是您需要更改的内容以使其正常工作

首先,如果你想存储多个学生的信息,就需要一个数组

static int MAX_STUDENTS = 50;
struct infoStruct info[MAX_STUDENTS];
然后将每个学生扫描到结构的单独部分

scanf("%d %d %d %d %d %d", &info[students-1].studentID, &info[students-1].day, &info[students-1].month, &info[students-1].year, &info[students-1].phone, &info[students-1].end);
然后需要确保正确检查结束条件(检查最新信息[x]。结束)
在尝试添加更多空间之前,检查阵列中是否还有空间也是明智的

完成这些操作后,您将正确存储学员

至于搜索,您需要扫描id以搜索到一个单独的int。 然后循环遍历数组(info[x]),并根据搜索ID搜索每个元素的studentID。当有匹配项时,将其打印出来

编辑:
也值得考虑将电话号码存储为“字符串”而不是int,很多电话号码都以“0”开头,但int会删除0。
(因此电话号码012345将变成12345)

关于您的编辑:如果他只想显示号码,他可以使用(示例)printf(“%08u\n”,info.phone);但我同意。。。在这里使用无符号整数并不是一个好主意…@Joze fair point print formatting可以用一个正常的数字来修复它。我想说字符串更灵活,因为您可以存储其他字符(例如“区号电话号码”),但您的解决方案对于简单的情况(如OP's@Daboyzuk我通过添加一个数组稍微改变了我的程序,但我仍然不确定应该如何搜索它。你能给我一根骨头吗?再多给我一点工作。我已经为这个项目工作了几天,现在我试图自己解决它,但我似乎无法解决这个问题。正如你所知,我对编程相当陌生。编辑:我还应该让你们知道我所有的课程都是在线的,所以联系我的教授寻求帮助有点困难,所以我向你们求助。@user2172993你们必须使用一个结构数组(如Daboyzuk消息中所示…@user2172993)。你的搜索应该是这样的<代码>int sIndex=0;当循环结束时,(sIndex。。。如果sIndex等于students,则ID不在数组中。。。如果sIndex少于学生,那么它已经找到了。然后可以打印信息[sIndex]。这是从我的手机里打出来的,所以请原谅任何打字错误!