I'm new to C. I'm trying to get a lot of text from the user and count the number of words, characters, lines, whitespaces and letters. This is what I've done:
#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;
}
But I get so many errors and warnings I'm just lost...
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
I googled the different errors but didn't find a solution that solves my problem.
Can you help me a bit?
Thanks.
Conflicting Types for Error - Is a common mistake in programming it occurs due to incompatibility of parameters/arguments type in declaration and definition of the function. Let's understand by example: In this program we are finding the average of two numbers and we create a user define function name average().
implicit declaration of function. So what does the gcc warning 'implicit declaration of function' mean (other compiler report a 'implicit parameter declaration')? Basically it means that the compiler has found a call to function for which he does not have a prototype.
A function prototype is simply the declaration of a function that specifies function's name, parameters and return type. It doesn't contain function body. A function prototype gives information to the compiler that the function may later be used in the program.
Function Declarations The actual body of the function can be defined separately. int max(int, int); Function declaration is required when you define a function in one source file and you call that function in another file. In such case, you should declare the function at the top of the file calling the function.
For
program2.c:20: warning: comparison between pointer and integer
Change
if(c == "\n")
to
if(c == '\n')
For
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
Define prototypes for your functions.
int isLetter(char c);
int isWhitespace(char c);
For
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
Change
if(c == "\n" || c == " " || c == " ") return 1;
to
if(c == '\n' || c == ' ' || c == '\t')
Start with the first error/warning, fix it and then work your way down one by one always compiling after each change. Often, you will find, getting rid of an error/warning on a line also gets rid of others in subsequent lines.
Line 20:
if(c == "\n") linecount++;
gives the warning
program2.c:20: warning: comparison between pointer and integer
c
is a char (internally converted to an integer before the comparison); "\n"
is an array[2] of char
(internally converted to char *
before the comparison).
That's why the compiler complains about comparing an integer and a pointer.
You need to compare c
to a character (both will get internally converted to integers)
if(c == '\n') linecount++;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With