简体   繁体   English

Function 不返回 printf

[英]Function does not return printf

when I run this program it prints the question and it gets the answer from the user, but it does not say if it's correct or false.当我运行这个程序时,它会打印问题并从用户那里得到答案,但它并没有说明它是正确的还是错误的。 Please help!请帮忙!

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>

void
correct (char str[])
{
    char c1 = "Cristoforo";
    char c2 = "cristoforo";
    char c3 = "CRISTOFORO";
    int conf = strcmp (str, c1);
    int conf1 = strcmp (str, c2);
    int conf2 = strcmp (str, c3);
    if (conf1 == 0 || conf == 0 || conf2 == 0)
    {
        printf ("The answer is correct");
    }
    else
    {
        printf ("The answer is wrong");
    }
}

int
main ()
{
  char s[] = "What's the name of the explorere Colombus?\n";
  char r[100];
  printf ("%s", s);
  gets (r);
  void correct (r);
}

void correct(r) is declaration and it means - correct is a function that takes r type in arguments and returns void. void correct(r)是声明,它意味着 - 正确的是 function,它在 arguments 中采用 r 类型并返回 void。 Just remove void to call function.只需删除void即可调用 function。 correct(r) . correct(r) And you need to change char s to char * or const char* .您需要将char更改为char *const char*

    const char *c1 = "Cristoforo";
    const char *c2 = "cristoforo";
    const char *c3 = "CRISTOFORO";

You are getting a seg fault due to incorrect use of strcmp() .由于不正确使用strcmp() ,您会遇到seg错误。 The second parameter is required to be a const char *__s2 .第二个参数必须是const char *__s2 So what would be right is:所以正确的是:

int conf = strcmp(str, "Cristoforo");
int conf1 = strcmp(str, "cristoforo");
int conf2 = strcmp(str, "CRISTOFORO");

and also remove void when calling correct(r)并在调用correct(r)时删除void

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM