while循环只要求用户输入一次 #包括 #包括 结构 { charfirstname[20]; char LastName[32]; 整数分[20]; }; int main() { inti,n; struct_struct*ptr[100]; printf(“有多少学生?\n”); scanf(“%d”和“&n”); while(iFirstName); printf(“输入姓氏”); scanf(“%s”,ptr[i]->LastName); printf(“输入分数?\n”); scanf(“%s”,ptr[i]->分数); printf(“%s%s%s\n”,ptr[i]->FirstName,ptr[i]->LastName,ptr[i]->Score); i++; } }
嘿,伙计们,当我输入第一个输入时,它只运行一次,而用户输入的数字没有运行,我尝试了for循环,但结果相同。 还在学C,所以如果我误解了什么,我道歉while循环只要求用户输入一次 #包括 #包括 结构 { charfirstname[20]; char LastName[32]; 整数分[20]; }; int main() { inti,n; struct_struct*ptr[100]; printf(“有多少学生?\n”); scanf(“%d”和“&n”); while(iFirstName); printf(“输入姓氏”); scanf(“%s”,ptr[i]->LastName); printf(“输入分数?\n”); scanf(“%s”,ptr[i]->分数); printf(“%s%s%s\n”,ptr[i]->FirstName,ptr[i]->LastName,ptr[i]->Score); i++; } },c,C,嘿,伙计们,当我输入第一个输入时,它只运行一次,而用户输入的数字没有运行,我尝试了for循环,但结果相同。 还在学C,所以如果我误解了什么,我道歉 提前感谢。问题是i未初始化。因此,while(iwhile)循环有问题。您可以将其重写为: #include <stdio.h> #include <stdlib.h> struct the_struct { char FirstName[20]; char LastName[32]; int Score[20];
提前感谢。问题是
i
未初始化。因此,while(iwhile)循环有问题。您可以将其重写为:
#include <stdio.h>
#include <stdlib.h>
struct the_struct
{
char FirstName[20];
char LastName[32];
int Score[20];
};
int main ()
{
int i,n;
struct the_struct *ptr[100];
printf("how many students?\n");
scanf("%d",&n);
while (i<=n);
{
i==0;
ptr[i] = malloc(sizeof(struct the_struct));
printf("Enter First Name \n");
scanf("%s",ptr[i]->FirstName);
printf("Enter Last Name \n");
scanf("%s",ptr[i]->LastName);
printf("Enter Score? \n");
scanf("%s",ptr[i]->Score);
printf("%s %s %s\n",ptr[i]->FirstName,ptr[i]->LastName,ptr[i]->Score);
i++;
}
}
(i=0;i
{
ptr[i]=malloc(sizeof(struct the_struct));
printf(“输入名字”);
scanf(“%s”,ptr[i]->FirstName);
printf(“输入姓氏”);
scanf(“%s”,ptr[i]->LastName);
printf(“输入分数?\n”);
scanf(“%s”,ptr[i]->分数);
printf(“%s%s%s\n”,ptr[i]->FirstName,ptr[i]->LastName,ptr[i]->Score);
}
由于您使用
%s
读取和打印分数
,因此应将其声明为字符分数[20];
而不是int
在之后去掉;
(i当我删除时;当它询问学生人数时,它结束程序,但它允许我输入一个字母作为名字,忽略学生人数。我像你说的那样更改了while循环,结果相同。还尝试了for循环,结果相同。@Ankosh你也需要删除循环后的分号。这很有效!!我正在打印到知道输入是否有效。谢谢!!
for (i = 0; i < n; ++i)
{
ptr[i] = malloc(sizeof(struct the_struct));
printf("Enter First Name \n");
scanf("%s",ptr[i]->FirstName);
printf("Enter Last Name \n");
scanf("%s",ptr[i]->LastName);
printf("Enter Score? \n");
scanf("%s",ptr[i]->Score);
printf("%s %s %s\n",ptr[i]->FirstName,ptr[i]->LastName,ptr[i]->Score);
}