首先,程序要求您输入学生姓名。当我想使用%s
在底部显示学生的姓名时,xcode总是告诉我替换为%c
。您能否给我解决方案如何显示输入的学生姓名,而不是%c
?谢谢。
#include <stdio.h>
void showthelastvalue () {
char name1, name2, name3, name4;
int value1a, value1b, value1c, value1d;
int value2a, value2b, value2c, value2d;
int value3a, value3b, value3c, value3d;
int value4a, value4b, value4c, value4d;
printf("\nenter student name-1 : ");
scanf(" %s", name1);
printf("enter student name-2 : ");
scanf(" %s", name2);
printf("enter student name-3 : ");
scanf(" %s", name3);
printf("enter student name-4 : ");
scanf(" %s", name4);
printf("\nEnter student grade-1 %s\n", name1);
printf("grade ke 1 : ");
scanf("%d", &grade1a);
printf("grade ke 2 : ");
scanf("%d", &grade1b);
printf("grade ke 3 : ");
scanf("%d", &grade1c);
printf("grade ke 4 : ");
scanf("%d", &grade1d);
printf("\nEnter student grade- 2 %s\n", name2);
printf("grade ke 1 : ");
scanf("%d", &grade2a);
printf("grade ke 2 : ");
scanf("%d", &grade2b);
printf("grade ke 3 : ");
scanf("%d", &grade2c);
printf("grade ke 4 : ");
scanf("%d", &grade2d);
printf("\nEnter student grade- 3 %s\n", name3);
printf("grade ke 1 : ");
scanf("%d", &grade3a);
printf("grade ke 2 : ");
scanf("%d", &grade3b);
printf("grade ke 3 : ");
scanf("%d", &grade3c);
printf("grade ke 4 : ");
scanf("%d", &grade3d);
printf("\nEnter student grade- 4 %s\n", name4);
printf("grade ke 1 : ");
scanf("%d", &grade4a);
printf("grade ke 2 : ");
scanf("%d", &grade4b);
printf("grade ke 3 : ");
scanf("%d", &grade4c);
printf("grade ke 4 : ");
scanf("%d", &grade4d);
printf("\nThe grade of 4 students: \n");
printf(" %s %d %d %d %d\n", name1, grade1a, grade1b, grade1c, grade1d);
printf(" %s %d %d %d %d\n", name2, grade1a, grade1b, grade1c, grade1d);
printf(" %s %d %d %d %d\n", name3, grade1a, grade1b, grade1c, grade1d);
printf(" %s %d %d %d %d\n", name4, grade1a, grade1b, grade1c, grade1d);
averange1 = (grade1a + grade1b + grade1c + grade1d) / 4;
averange2 = (grade2a + grade2b + grade2c + grade2d) / 4;
averange3 = (grade3a + grade3b + grade3c + grade3d) / 4;
averange4 = (grade4a + grade4b + grade4c + grade4d) / 4;
printf("Last grade of 4 studentsgra :\n");
printf("Last grade from student1 %s = %d\n", name1, averange1);
printf("Last grade from student2 %s = %d\n", name2, averange2);
printf("Last grade from student3 %s = %d\n", name3, averange3);
printf("Last grade from student4 %s = %d\n", name4, averange4);
}
int main() {
int choose;
printf("Welcome!\n");
do {
printf("Choose anda :\n");
printf("1. Show the last grade\n");
printf("2. Show the grade\n");
printf("3. Show the Table\n");
printf("4. Exit\n");
printf("\nWhat will you choose ? ");
scanf("%d", &choose);
switch (choose) {
case 1:
showthelastgrade ();
break;
case 4:
printf("Thank you /001\n");
break;
}
}
while (choose != 5);
return 0;
}
答案 0 :(得分:2)
您正在将名称读入单个char
,这就是xcode为您提供该消息的原因。您需要使用char
s。
最简单的方法是将声明更改为:
char name1[N], name2[N], name3[N], name4[N];
其中N
是名称的最大长度。
然后,您可以将所有scanf
更改为:
scanf(" %s", name1); // & removed
as name1
等现在指向char
数组的开头。
请注意,这本身就是冒险的事情,因为这意味着可以通过输入太长的名称来溢出缓冲区。
最好使用fgets
,因为它允许您指定缓冲区的最大长度:
fgets(name1, N, stdin);
任何超过N-1
个字符的内容都将被丢弃,而不是溢出缓冲区。
答案 1 :(得分:0)
要打印char
,请使用%c
。
要打印字符串或char数组,请使用%s
您可以使用char char name[30]
数组或char指针char *name
,然后使用%s
进行打印。
答案 2 :(得分:0)
我没有调查你的程序的逻辑,但做了以下修改,它应该工作正常。
#include <stdio.h>
//The maximum allowed name size, you can change it to whatever you want
#define MAX 10
void showthelastvalue () {
char name1[MAX], name2[MAX], name3[MAX], name4[MAX];
//You need character arrays here, simple char would store only a single byte
int value1a, value1b, value1c, value1d;
int value2a, value2b, value2c, value2d;
int value3a, value3b, value3c, value3d;
int value4a, value4b, value4c, value4d;
printf("insert student name 1 : ");
gets(name1); //scanf(" %s", &name1);
printf("insert student name 2 : ");
gets(name2); //scanf(" %s", &name2);
printf("insert student name 3 : ");
gets(name3); //scanf(" %s", &name3);
printf("insert student name 4 : ");
gets(name4); //scanf(" %s", &name4);
printf("Insert student value-1 %c\n", name1);
printf("value ke 1 : ");
scanf("%d", &value1a);
printf("value ke 2 : ");
scanf("%d", &value1b);
printf("value ke 3 : ");
scanf("%d", &value1c);
printf("value ke 4 : ");
scanf("%d", &value1d);
printf("Insert student value- 2 %c\n", name2);
printf("value ke 1 : ");
scanf("%d", &value2a);
printf("value ke 2 : ");
scanf("%d", &value2b);
printf("value ke 3 : ");
scanf("%d", &value2c);
printf("value ke 4 : ");
scanf("%d", &value2d);
printf("Insert student value- 3 %c\n", name3);
printf("value ke 1 : ");
scanf("%d", &value3a);
printf("value ke 2 : ");
scanf("%d", &value3b);
printf("value ke 3 : ");
scanf("%d", &value3c);
printf("value ke 4 : ");
scanf("%d", &value3d);
printf("Insert student value- 4 %c\n", name4);
printf("value ke 1 : ");
scanf("%d", &value4a);
printf("value ke 2 : ");
scanf("%d", &value4b);
printf("value ke 3 : ");
scanf("%d", &value4c);
printf("value ke 4 : ");
scanf("%d", &value4d);
printf("Jadi value dari 4 mahasiswa sbb : \n");
printf(" %s %d %d %d %d", name1, value1a, value1b, value1c, value1d);
printf(" %s %d %d %d %d", name2, value1a, value1b, value1c, value1d);
printf(" %s %d %d %d %d", name3, value1a, value1b, value1c, value1d);
printf(" %s %d %d %d %d", name4, value1a, value1b, value1c, value1d);
}
int main() {
int choose;
printf("Welcome!\n");
do {
printf("Choose anda :\n");
printf("1. Show the last value\n");
printf("2. Show Grade\n");
printf("3. Show the Table\n");
printf("4. Exit\n");
printf("What you will choose ? ");
scanf("%d", &choose);
switch (choose) {
case 1:
showthelastvalue();
break;
case 4:
printf("Thank you /001\n");
break;
}
}
while (choose != 5);
return 0;
}
对你做错了什么的一些解释:你已经将变量定义为&#39; char&#39;这意味着你的程序期望一个字节。另外,使用&#39; gets()&#39;函数而不是使用scanf来存储字符串。 &#39;%S&#39;仅适用于printf中的字符数组。
答案 3 :(得分:0)
您正在使用char name1,name2,name3,name4
用
char name1[N],name2[N],name3[N],name4[N]
然后使用%s
代替%c
。
然后使用gets()
函数,而不是使用scanf()
函数。
然后尝试一下,我希望它会完美运行。