c/c++语言开发共享C – 函数的错误冲突类型

我是C的新手。我正在尝试从用户那里获取大量文本并计算单词,字符,行,空格和字母的数量。 这就是我所做的:

#include  #include  int main(void) { char c = getchar(); char previousc; int charcount = 0; int wordcount = 0; int whitespacecount = 0; int linecount = 0; int lettercount = 0; while(c != EOF) { if(isLetter(c) == 1) lettercount++; if(isWhitespace(c) == 1) { whitespacecount++; if(isWhitespace(previousc) == 0) wordcount++; } if(c == "n") linecount++; previousc = c; c = getchar(); charcount++; } printf("Character Count: %dn Word Count: %dn Whitespace Count: %dn Letter Count: %dn Line Count: %dn", charcount, wordcount, whitespacecount, linecount, lettercount); } int isLetter(char c) // 1 for true, 0 for false. { // instead of writing tons of if's if(isalpha(c) > 0) return 1; return 0; } int isWhitespace(char c) // 1 for true, 0 for false. { if(c == "n" || c == " " || c == " ") return 1; return 0; } 

但是我收到了很多错误和警告,我只是输了……

 program2.c: In function 'main': program2.c:20: warning: comparison between pointer and integer program2.c: At top level: program2.c:28: error: conflicting types for 'isLetter' program2.c:28: note: an argument type that has a default promotion can't match an empty parameter name list declaration program2.c:14: error: previous implicit declaration of 'isLetter' was here program2.c:35: error: conflicting types for 'isWhitespace' program2.c:35: note: an argument type that has a default promotion can't match an empty parameter name list declaration program2.c:15: error: previous implicit declaration of 'isWhitespace' was here program2.c: In function 'isWhitespace': program2.c:36: warning: comparison between pointer and integer program2.c:36: warning: comparison between pointer and integer program2.c:36: warning: comparison between pointer and integer 

我搜索了不同的错误,但没有找到解决我的问题的解决方案。

你能帮我一下吗?

谢谢。

    对于

    program2.c:20:warning:指针和整数之间的比较

    更改

      if(c == "n") 

      if(c == 'n') 

    对于

    program2.c:28:错误:’isLetter’的冲突类型
    program2.c:28:注意:具有默认促销的参数类型不能与空参数名称列表声明匹配
    program2.c:14:错误:’isLetter’的先前隐式声明在这里
    program2.c:35:错误:’isWhitespace’的冲突类型
    program2.c:35:注意:具有默认促销的参数类型不能与空参数名称列表声明program2.c匹配:15:错误:’isWhitespace’的先前隐式声明在这里

    为您的function定义原型。

     int isLetter(char c); int isWhitespace(char c); 

    对于

    program2.c:在函数’isWhitespace’中:
    program2.c:36:warning:指针和整数之间的比较
    program2.c:36:warning:指针和整数之间的比较
    program2.c:36:warning:指针和整数之间的比较

    更改

     if(c == "n" || c == " " || c == " ") return 1; 

     if(c == 'n' || c == ' ' || c == 't') 

    从第一个错误/警告开始,修复它,然后在每次更改后逐个编译。 通常,你会发现,在一行上摆脱错误/警告也会在后续行中删除其他行。

    第20行:

      if(c == "n") linecount++; 

    发出警告

     program2.c:20: warning: comparison between pointer and integer 

    c是char(在比较之前内部转换为整数); "n"char的数组[2](在比较之前内部转换为char * )。
    这就是编译器抱怨比较整数和指针的原因。

    你需要将c与一个字符进行比较(两者都将在内部转换为整数)

      if(c == 'n') linecount++; 


    int c的原因是函数getchar返回int以支持EOF指标。


    EOF是一个整数值,表示输入结束。 这是一个值,对于任何字符chch == EOF总是假的。 因此,您应始终将int类型的值与EOF进行比较,而不是char类型。 它在你的机器上运行,因为char类型是作为signed char实现的,但是在char类型是unsigned char机器上,这不会。

    现在发出警告和错误

    你必须在main中使用之前声明函数头,例如:

     int isLetter(char c); int main(void){ char c = getchar(); char previousc; int charcount = 0; int wordcount = 0; int whitespacecount = 0; int linecount = 0; int lettercount = 0; while(c != EOF) { if(isLetter(c) == 1) lettercount++; if(isWhitespace(c) == 1) { whitespacecount++; if(isWhitespace(previousc) == 0) wordcount++; } if(c == "n") linecount++; previousc = c; c = getchar(); charcount++; } printf("Character Count: %dn Word Count: %dn Whitespace Count: %dn Letter Count: %dn Line Count: %dn", charcount, wordcount, whitespacecount, linecount, lettercount);} 

    这将解决冲突类型错误。 但如果要检查字符,还必须将“”更改为“”。

      以上就是c/c++开发分享C – 函数的错误冲突类型相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/560666.html

      (0)
      上一篇 2021年1月28日
      下一篇 2021年1月28日

      精彩推荐