{ char c[20];int d=0; printf("请输入姓名:\n"); scanf("%s",c); stu *p=head->next; while(p!=NULL) { if(strcmp(p->name,c)==0) { printf("该学生信息已找到!\n"); printf("学号:%d,名字:%s,性别:%s,年龄:%d,籍贯:%s,系别:%s,专业:%s,班级:%d,学籍变化:%s\n", p->id,p->name,p->gender,p->age,p->native_place,p->department,p->specialty,p->cla,p->xjbh); d=d+1; } p=p->next; } if(d=0) { printf("\n该学生信息不存在!\n"); } }这段代码哪里错了
时间: 2023-12-03 09:45:46 浏览: 43
在判断是否存在该学生信息时,应该使用`if(d==0)`而不是`if(d=0)`,因为`=`是赋值操作符,而`==`才是判断相等的操作符。因此,正确的代码应该是:
```
char c[20];
int d = 0;
printf("请输入姓名:\n");
scanf("%s", c);
stu *p = head->next;
while (p != NULL) {
if (strcmp(p->name, c) == 0) {
printf("该学生信息已找到!\n");
printf("学号:%d,名字:%s,性别:%s,年龄:%d,籍贯:%s,系别:%s,专业:%s,班级:%d,学籍变化:%s\n",
p->id, p->name, p->gender, p->age, p->native_place, p->department, p->specialty, p->cla, p->xjbh);
d = d + 1;
}
p = p->next;
}
if (d == 0) {
printf("\n该学生信息不存在!\n");
}
```
相关问题
#include <stdio.h> #include <string.h> int main() { struct person{ char name[20]; long int num; char sex; char pro[20]; union { int class_num; char duty[20]; }special; }; int n; printf("请输入人数:"); scanf("%d",&n); struct person per[n]; int i; for(i=0;i<n;i++) { printf("请输入人员信息 \n姓名:"); scanf("%s",per[i].name); printf("号码:"); scanf("%ld",&per[i].num); printf("性别:"); scanf("%c",&per[i].sex); printf("职业:"); scanf("%s",per[i].pro); if(strcmp(per[i].pro , "学生")==0) { printf("班级:"); scanf("%d",&per[i].special.class_num);} else { printf("职务:"); scanf("%s",per[i].special.duty);} } printf("人员信息如下\n"); for(i=0;i<n;i++) { printf("姓名:%s\n",per[i].name); printf("号码:%ld\n",per[i].num); printf("性别:%c\n",per[i].sex); printf("职业:%s\n",per[i].pro); if(strcmp(per[i].pro,"学生")==0){ printf("班级:%s",per[i].special.class_num);} else printf("职务:%s",per[i].special.duty); } }这个程序会同时输出性别和职业,怎么改正
可以在读取性别的scanf前加一个getchar(),来清除输入缓冲区中的回车符。另外,在输出班级和职务时,应该使用% d或% s来输出整型和字符串类型,而不是使用%s输出整型。
修改后的程序代码如下:
```c
#include <stdio.h>
#include <string.h>
int main() {
struct person {
char name[20];
long int num;
char sex;
char pro[20];
union {
int class_num;
char duty[20];
} special;
};
int n;
printf("请输入人数:");
scanf("%d", &n);
struct person per[n];
int i;
for (i = 0; i < n; i++) {
printf("请输入人员信息 \n姓名:");
scanf("%s", per[i].name);
printf("号码:");
scanf("%ld", &per[i].num);
getchar();
printf("性别:");
scanf("%c", &per[i].sex);
printf("职业:");
scanf("%s", per[i].pro);
if (strcmp(per[i].pro, "学生") == 0) {
printf("班级:");
scanf("%d", &per[i].special.class_num);
} else {
printf("职务:");
scanf("%s", per[i].special.duty);
}
}
printf("人员信息如下\n");
for (i = 0; i < n; i++) {
printf("姓名:%s\n", per[i].name);
printf("号码:%ld\n", per[i].num);
printf("性别:%c\n", per[i].sex);
printf("职业:%s\n", per[i].pro);
if (strcmp(per[i].pro, "学生") == 0) {
printf("班级:%d\n", per[i].special.class_num);
} else {
printf("职务:%s\n", per[i].special.duty);
}
}
}
```
#include <stdio.h> struct student { int num; char name[20]; char gender; int age; float score; }; int main() { struct student s; s.num = 1001; scanf("%s %c %d %f", s.name, &s.gender, &s.age, &s.score); printf("学号:%d\n姓名:%s\n性别:%c\n年龄:%d\n成绩:%.2f\n", s.num, s.name, s.gender, s.age, s.score); return 0; }找错
此代码的问题在于缺少了对结构体类型的定义。需要在代码开头加上以下代码:
```c
struct student {
int num;
char name[20];
char gender;
int age;
float score;
};
```
完整的代码如下:
```c
#include <stdio.h>
struct student {
int num;
char name[20];
char gender;
int age;
float score;
};
int main() {
struct student s;
s.num = 1001;
scanf("%s %c %d %f", s.name, &s.gender, &s.age, &s.score);
printf("学号:%d\n姓名:%s\n性别:%c\n年龄:%d\n成绩:%.2f\n", s.num, s.name, s.gender, s.age, s.score);
return 0;
}
```
运行程序时,输入相应的信息即可输出学生信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)