C语言结构体变量的问题
发布网友
发布时间:2023-03-23 10:48
我来回答
共5个回答
热心网友
时间:2023-10-14 07:11
老大,I服了U,错误一大堆,继续努力哈。
#include <stdio.h>
#include "string.h"//
int main()
{
struct Student
{
char name[20];
char sex[10];
char addr[20];
};
Student ab,ac;//
strcpy(ab.name,"ZhangSan");
strcpy(ab.sex,"man");
strcpy(ab.addr,"123 beijing road");
strcpy(ac.name,"LiSi");
strcpy(ac.sex,"woman");
strcpy(ac.addr,"124 beijing road");
char a[20];
printf("请输入名字\n");
scanf("%s",a);//去掉地址符
int iRes=0;
if(strcmp(a,ab.name)==0)iRes=1;
else if(strcmp(a,ac.name)==0)iRes=2;
else iRes=0;
switch(iRes)
{
case 1: printf("name:%s\nsex:%s\naddres:%s\n",ab.name,ab.sex,ab.addr);break;//加引号
case 2: printf("name:%s\nsex:%s\naddres:%s\n",ac.name,ac.sex,ac.addr);break;//加引号
default:printf("not found!");
}
return 0;
}
热心网友
时间:2023-10-14 07:11
问题不大,改好如下:
#include <stdio.h>
#include<cstring>
int main()
{
struct Student
{
char name[20];
char sex[10];
char addr[30];
}ab={"张三","man","123 beijing road"},
ac={"李四","woman","124 beijing road"};
char a[20];
printf("请输入名字\n");
scanf("%s",&a);
if(strcmp(a,"张三")==0)
printf("name:%s\nsex:%s\naddres:%s\n",ab.name,ab.sex,ab.addr);
else
if(strcmp(a,"李四")==0)
printf("name:%s\nsex:%s\naddres:%s\n",ac.name,ac.sex,ac.addr);
else
printf("通讯录中没有此人\n");
return 0;
}
热心网友
时间:2023-10-14 07:12
直接编译,报错:
tmp.c: In function ‘main’:
tmp.c:16: error: case label does not rece to an integer constant
tmp.c:16: error: ‘name’ undeclared (first use in this function)
tmp.c:16: error: (Each undeclared identifier is reported only once
tmp.c:16: error: for each function it appears in.)
tmp.c:16: error: expected ‘)’ before ‘:’ token
tmp.c:16: error: stray ‘\’ in program
tmp.c:16: error: stray ‘\’ in program
tmp.c:16: error: stray ‘\’ in program
tmp.c:16:51: warning: missing terminating " character
tmp.c:16: error: missing terminating " character
tmp.c:17: error: stray ‘\’ in program
tmp.c:17: error: stray ‘\’ in program
tmp.c:17: error: stray ‘\’ in program
tmp.c:17:51: warning: missing terminating " character
tmp.c:17: error: missing terminating " character
tmp.c:20: error: expected ‘;’ before ‘}’ token
tmp.c:20: error: expected declaration or statement at end of input
主要两个错误:
1. switch判断条件错误,不能用字符串做条件,必须是可数类型:整型、字符、布尔和枚举型
2. printf没有引号
为了偷懒,我直接将名字改成一个字符了
#include <stdio.h>
2 int main()
3 {
4 struct Student
5 {
6 char name[20];
7 char sex[10];
8 char addr[20];
9 }ab={"a","man","123 beijing road"},
10 ac={"b","woman","124 beijing road"};
11 char a;
12 printf("请输入名字\n");
13 scanf("%c",&a);
14 switch(a)
15 {
16 case 'a':
17 printf("name:%s\nsex:%s\naddres:%s\n",ab.name,ab.sex,ab.addr);
18 break;
19 case 'b':
20 printf("name:%s\nsex:%s\naddres:%s\n",ac.name,ac.sex,ac.addr);
21 break;
22 }
23 return 0;
24 }
执行结果:
请输入名字
b
name:b
sex:woman
addres:124 beijing road追问你的答案我想得出来,但现在的关键是如何输入中文名时程序能够识别,switch语句括号内表达式类型本来就可以是字符型,你把名字用字母代替当然可以
追答那就只能用字符串比较函数,strcmp,然后判断比较结果,头文件在string.h中,没有其他办法
热心网友
时间:2023-10-14 07:12
struct Student
{
char name[20];
char sex[10];
char addr[20];
}ab={"张三","man","123 beijing road"},
ac={"李四","woman","124 beijing road"};
char a[10];
printf("请输入名字\n");
scanf("%s",&a);
int aa = strcmp("张三",a);
switch(aa)
{
case(0):printf("name:%s\nsex:%s\naddres:%s\n",ab.name,ab.sex,ab.addr);break;
case (1):printf("name:%s\nsex:%s\naddres:%s\n",ac.name,ac.sex,ac.addr);break;
}
热心网友
时间:2023-10-14 07:13
switch-case 不能用字符串
改成
switch(a){
case '1': ...
case '2': ...
}
就可以了