与用户用C相互作用(scanf()的)

问题描述 投票:-2回答:1

我需要

  1. 问他用户是否想用阶乘函数或函数斐波纳契
  2. 问他的价值。

如何看起来:

printf("choose option:\n1 - factorial\n2 - fibonacci sequence\n");
scanf("%d", &a);

if (a==1) {
  printf("enter a number:\n");
  scanf("%d", &x);
  return factorial(x); 
}

if (a==2) {
  printf("enter a number of sequence:\n");
  scanf("%d", &y);
  return fibonacci(y); 
}

问题:第一部分作品,但第二次却没有。 Error: 'exit status 120'

怎么了?

c scanf
1个回答
-3
投票
/*
For first,  variable 'a' should be declared as  integer:
*/
int a = 0;
/*
  Further, when you read keystrokes, you get ascii code of a character.
  Remember:'1' != 1
   Your 'if' statement should be different:
*/  
if (a == '1') {... }   /* note single quote characters around '1' and '2' */
else if (a == '2') {... }
else
{
   /* if user press something that is not '1' or '2' */
     printf("Wrong choice!\n");
     exit(1);
}
© www.soinside.com 2019 - 2024. All rights reserved.