학생 번호를 입력 한 다음 이름과 마크를 입력해야합니다. 그러나 마크를 입력하면 프로그램이 충돌합니다. 왜 내가 마지막 루프 기능에 들어가면 내 프로그램이 충돌하는지 모르겠다. 어쩌면 내가 너무 많이 중첩했기 때문에?루프 기능이 입력되면 오류가 발생합니다.
#include <stdio.h>
#include <string.h>
#define NAME_LEN 25
int noOfStud(void);
void listStudents(int noOfStud, char names[][NAME_LEN]);
void options(int noOfStud, double marks[]);
void switchOptions(int noOfStud, double marks[]);
void courseWork1(int noOfStud, double marks[]);
void emptyBuffer(void);
int main(void)
{
int students;
char names[students][NAME_LEN];
double marks[students];
students = noOfStud();
listStudents(students, names);
options(students, marks);
return 0;
}
int noOfStud(void)
{
int students;
printf("Number of students: ");
scanf("%d", &students);
getchar();
return students;
}
void listStudents(int noOfStud, char names[][NAME_LEN])
{
int i;
for(i=0; i<noOfStud; i++)
{
printf("Enter name: ");
scanf("%[^\n]", names[i]);
getchar();
}
}
void options(int noOfStud, double marks[])
{
printf("1. Enter marks for course work 1\n");
printf("2. Enter marks for course work 2\n");
printf("3. Enter makrs for course work 3\n");
printf("4. Display a particular student's marks\n");
printf("5. Supervisor mode\n");
printf("6. Exi program\n");
switchOptions(noOfStud, marks);
}
void switchOptions(int noOfStud, double marks[])
{
char options;
printf("\nPlease enter a number for which choice you want: ");
scanf("%d", &options);
emptyBuffer();
switch(options)
{
case 1:
printf("Thank you for chosing to enter marks for course work 1!\n");
courseWork1(noOfStud,marks);
break;
case 2:
printf("Thank you for chosing to enter marks for course work 2!\n");
break;
case 3:
printf("Thank you for chosing to enter marks for course work 3!\n");
break;
case 4:
printf("work 4");
break;
case 5:
printf("work 5");
break;
case 6:
printf("work 6");
break;
default:
printf("You didn't enter anything");
}
}
void courseWork1(int noOfStud, double marks[])
{
int i;
for(i=0; i<noOfStud; i++)
{
printf("Enter marks: ");
scanf("%d", &marks[i]);
getchar();
}
}
void emptyBuffer(void) /* Empty the keyboard buffer */
{
while(getchar() != '\n')
{
;
}
}
마지막 루프 기능을 입력하기 전까지는 문제가 없습니다. 충돌이 발생합니다. 너 왜 그런 생각이있어? 도와 주셔서 감사합니다.
void courseWork1(int noOfStud, double marks[])
{
int i;
for(i=0; i<noOfStud; i++)
{
printf("Enter marks: ");
scanf("%d", &marks[i]);
getchar();
}
}
http://idownvotedbecau.se/nomcve/ – klutt
'char names [students] [NAME_LEN];은'students = noOfStud();'뒤에 와야합니다. – mch
'scanf ("% d", & options);'->'options'는'char' 타입입니다. 'scanf ("% c", & options);' –