성적 / 학생 번호 (KAS) / 학생 이름을 배열에 삽입하는 코드를 작성 중입니다.
그리고 알려지지 않은 수의 입력이 결국 인쇄됩니다.
문제 : 내 프로젝트의 문제는 인쇄 결과에 인쇄에 결함이있는 요소가 있다는 것입니다.
나는 입력을 한 후 정확한 시간을 삽입 한 모든 요소를 확인하려고 노력했으며 ( printf(...)
) 모든 것이 괜찮아 보였습니다.
그러나 여전히 출력에서 그들 중 일부는 잘못되었습니다.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int grade[100] , KAS[100] ,x,spots = 0; // KAS = student number
char name[spots][14], answer;
printf("Please insert a grade : ");
scanf("%d", &grade[spots]);
getchar();
printf("Please add a KAS : ");
scanf("%d",&KAS[spots]);
getchar();
printf("Please enter a name : ");
scanf("%s",&name[spots]);
getchar();
printf("\nDo you want to add another value? y/n : ");
scanf("%c",&answer);
puts("");
getchar();
while(answer == 'y')
{
spots++;
printf("Please insert another grade : ");
scanf("%d", &grade[spots]);
getchar();
printf("Please add another KAS : ");
scanf("%d",&KAS[spots]);
getchar();
printf("Please enter another name : ");
scanf("%s",&name[spots]);
getchar();
printf("\nDo you want to add another value? y/n : ");
scanf("%c\n",&answer);
puts("");
getchar();
if(answer == 'n')
{
break;
}
}
puts("*****************************");
for(x = 0; x < spots; x++)
{
puts("");
printf("%d. Student's great : %d\n",x,grade[x]);
printf("%d. Student's KAS : %d\n",x,KAS[x]);
printf("%d. Student's name : %s\n",x,name[x]);
}
puts("\n*****************************\n");
}
char name[spots][14]; //as spots is 0 , it would be name[0][14]
/* you would end up access invalid memory and cause UB */
이것을 다음으로 변경해야합니다.
char name[100][14];
또한 루프 안팎의 두 명령문에서 입력을받는 동안
scanf("%s",&name[spots]);
^ you don't need to use &
또한 for
루프 x <= spots
에서 루프 조건으로 사용 합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다