【发布时间】:2014-02-21 17:23:33
【问题描述】:
#define f(x) (x*(x+1)*(2*x+1))/6
void terminate();
main()
{
int n,op;
char c;
printf("Enter n value\n");
scanf("%d",&n);
op=f(n);
printf("%d",op);
printf("want to enter another value: (y / n)?\n");
scanf("%c",&c); // execution stops here itself without taking input.
getch();
if(c=='y')
main();
else
terminate();
getch();
}
void terminate()
{
exit(1);
}
在上面的程序中,我想从用户那里获取输入,直到他输入一个n 值。
为此,我试图反复调用main() 函数。如果它在 C 中是合法的,我想知道为什么程序在 scanf("%c",&c) 处终止,如注释行所示。
有人,请帮忙。
【问题讨论】:
-
OT:至少是
int main(void)。并假设terminate()不打算接受任何参数,它应该是void terminate(void)。 -
@alk - 进行了更改,但问题仍然存在。