scanf 乱码
while (1)
{
system("cls");
printf("1:Input person information\n");
printf("2:Display person information\n");
printf("3:Exit\n");
int input;
scanf("%d", &input);
system("cls");
while (input == 1)
{
printf("Please input:\nname\ngender\nheight\nweight\n");
//scanf("%s %s %lf %lf", &name[numPerson], &gender[numPerson], &height[numPerson], &weight[numPerson]);
scanf("%s,%s,%lf,%lf", &arr[numPerson].name,&arr[numPerson].gender,&arr[numPerson].height,&arr[numPerson].weight);
/*scanf("%s", &name[numPerson]);
scanf("%s",&gender[numPerson]);
scanf("%lf",&height[numPerson]);
scanf("%lf",&weight[numPerson]);*/
numPerson++;
system("cls");
printf("Keep on input information?\n 1:Yes\n 2:No\n");
scanf("%d",&choose);
if (choose == 2)
{
break;
}
}
if (input == 2)
{
printf("Display input name,gender,height and weight information\n");
for (int i = 0; i < numPerson;i++)
{
//printf("Name:%s\nGender:%s\nHeight:%lf\nWeight:%lf\n", name[i], gender[i], height[i], weight[i]);
printf("Name:%s\nGender:%s\nHeight:%lf\nWeight:%lf\n", arr[i].name, arr[i].gender, arr[i].height, arr[i].weight);
putchar('\n');
}
system("pause");
}
else if (input == 3)
break;
}
改为scanf("%s %s %lf %lf", &arr[numPerson].name,&arr[numPerson].gender,&arr[numPerson].height,&arr[numPerson].weight);显示正确
为什么此处scanf用逗号会吧所有输入都给到name这一部分?