scanf()不';I don’我不在车站工作

scanf()不';I don’我不在车站工作,c,scanf,C,Scanf,我使用repl.it来编写C,但是当我运行它时,系统跳过if语句中的第二个scanf #include <stdio.h> #include <math.h> #include <stdlib.h> int main (void) { char services[40]; loop: printf ("I can help you to do somethings(fibonacci number, pi, x^y and exit)\n");

我使用repl.it来编写C,但是当我运行它时,系统跳过if语句中的第二个scanf

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main (void)
{
  char services[40];
  loop: printf ("I can help you to do somethings(fibonacci number, pi, 
  x^y and exit)\n");
  scanf ("%s", &services);
  if (strncmp(servies, "fibonacci number"))
  {
    float n, first = 0, second = 1, terms = 1;
    printf ("please enter the terms:\n");
    scanf ("%f", &n);
    printf ("fibonacci number                    terms          golden 
    ratio\n");
    while (terms <= n)
    {
      terms = ++terms;
      printf ("%f%35f%10f\n", first, terms, first/second);
      terms = ++terms;
      printf ("%f%35f%10f\n",second, terms, first/second);
      first = first + second;
      second = first + second;
      goto loop;
    }
  }
}
#包括
#包括
#包括
内部主(空)
{
字符服务[40];
循环:printf(“我可以帮你做一些事情(斐波那契数,π,
x^y和退出)\n“;
scanf(“%s”和“服务”);
if(strncmp(服务,“斐波那契数”))
{
浮点数n,第一个=0,第二个=1,项=1;
printf(“请输入下列条款:\n”);
scanf(“%f”、&n);
printf(“斐波那契数
比率;

而(terms您没有阅读警告,或者使用了一个坏掉的C编译器。在修复了打字错误和字符串之后…以及UBs:

some.c: In function ‘main’:
some.c:19:13: warning: operation on ‘terms’ may be undefined [-Wsequence-point]
       terms = ++terms;
       ~~~~~~^~~~~~~~~
some.c:21:13: warning: operation on ‘terms’ may be undefined [-Wsequence-point]
       terms = ++terms;
       ~~~~~~^~~~~~~~~
我只剩下一个警告:

some.c: In function ‘main’:
some.c:9:7: warning: implicit declaration of function ‘strncmp’ [-Wimplicit-function-declaration]
   if (strncmp(services, "fibonacci number"))
       ^~~~~~~
实际上,使用了
strncmp
的隐式定义。是否包含

实际上,缺少第三个参数,即要比较的最大长度,而垃圾输入-垃圾输出就是您得到的

但是,您不需要
strncmp
,因为
strcmp
在这里就足够了。请注意,当字符串匹配时,它返回0,这是一个错误的值

因此:

但是现在,当您运行该程序时,您会注意到它也不起作用-当您在提示符中键入
斐波那契数时,不会出现任何内容。这是因为
%s
读取一个空格分隔的单词;因此
服务
现在将只包含
斐波那契”
!若要解决此问题,请使用
%[^\n]
要匹配非换行符,并明确指定最大长度,请执行以下操作:

scanf("%39[^\n]", services);

然后它就开始工作了……对于这一部分,正如您现在注意到的,
goto loop
位于错误的位置……

scanf(“%s”,和services);
-->
scanf(“%s”,services)
你应该为
strncmp
行得到一个编译错误,注意编译器输出
terms=++terms;
导致未定义的行为,我猜你的意思是
terms=terms+1;
我进入
循环:
我的眼睛突然燃烧起来。很抱歉,我看不懂其余部分。你应该使用
strcmp()
。您需要阅读函数的文档,当字符串匹配时,它不会返回
true
,而是返回
0
,因此您必须编写
if(strcmp(string1,string2)==0)
,但在我修复它之后,while语句就不起作用了
if (strcmp(services, "fibonacci number") == 0)
scanf("%39[^\n]", services);