for循环函数在输入时崩溃
For loop function crashes when entered
我必须先输入学生人数,然后是他们的名字,然后是分数。但是当我输入标记时,程序崩溃了。我不知道为什么当我进入最后一个循环函数时它使我的程序崩溃。可能是我嵌套太多了?
#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();
}
}
您应该使用 %lf
说明符进行双重输入。
scanf("%lf",&marks[i]);
也scanf("%c",&options);
因为options的类型是char。
使用错误的 格式说明符会导致未定义的行为。
在主函数中,您正在使用一些未初始化的变量初始化数组。
请按顺序排列。
int main(void)
{
int students;
students = noOfStud();
char names[students][NAME_LEN];
double marks[students];
listStudents(students, names);
options(students, marks);
return 0;
}
我必须先输入学生人数,然后是他们的名字,然后是分数。但是当我输入标记时,程序崩溃了。我不知道为什么当我进入最后一个循环函数时它使我的程序崩溃。可能是我嵌套太多了?
#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();
}
}
您应该使用 %lf
说明符进行双重输入。
scanf("%lf",&marks[i]);
也scanf("%c",&options);
因为options的类型是char。
使用错误的 格式说明符会导致未定义的行为。
在主函数中,您正在使用一些未初始化的变量初始化数组。
请按顺序排列。
int main(void)
{
int students;
students = noOfStud();
char names[students][NAME_LEN];
double marks[students];
listStudents(students, names);
options(students, marks);
return 0;
}