gpt4 book ai didi

C - 函数的错误类型冲突

转载 作者:太空狗 更新时间:2023-10-29 17:09:20 25 4
gpt4 key购买 nike

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

#include <ctype.h>
#include <stdio.h>
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: %d\n Word Count: %d\n Whitespace Count: %d\n Letter Count: %d\n Line Count: %d\n", 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: comparison between pointer and integer

改变

 if(c == "\n")  

 if(c == '\n')  

为了

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

为您的函数定义原型(prototype)。

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

为了

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

改变

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

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

关于C - 函数的错误类型冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22436975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com