【发布时间】:2018-11-26 07:28:14
【问题描述】:
如果为 temp 输入了 n,我正在尝试终止 for 循环并结束程序。 CaseCheck 函数返回 1 如果 temp = 'Y' 和 0 在所有其他情况下,此函数被测试正常工作。
现在,无论何时more = 0,程序都会尝试以相同的c值再次运行for循环,而如果more = 1,它将运行下一个c值。
int main()
{
char grades[100];
float v[100];
float w[100];
int more = 1;
char temp;
while (more = 1)
{
for (int c = 0; c < 100; c++)
{
printf("\n\nThis is Assignment number %d\n\nPlease enter the available mark for this assignment\n", c+1);
scanf("%f", &v[c]);
printf("\nPlease enter the awarded mark for this assignment");
scanf("%f", &w[c]);
grades[c] = GradeFromRawMarks(v[c],w[c]);
printf("Is there another assignment? Enter Y/N");
scanf("%s", &temp);
more = CaseCheck(temp,'Y');
}
}
return 0;
}
【问题讨论】:
-
more = 1-->more == 1。并打开你的编译器警告。 -
尝试调试。 more=1 是一个作业!
-
scanf("%s", &temp);获取了错误的类型(并传递了错误的值)。应该是scanf(" %c", &temp);。注意%c之前的额外空格。 -
请注意,将 more 设置为 0 不会超出您的
for循环。 -
... 因此,您可能会通过在
char变量中输入 2 个字节(一个字母,一个字符串终止符)来破坏某些内容。