Reputation: 812
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.
Upvotes: 6
Views: 71941
Reputation: 1
you have to declare the function header before using it in the main, for example:
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: %d\n Word Count: %d\n Whitespace Count: %d\n Letter Count: %d\n Line Count: %d\n", charcount, wordcount, whitespacecount, linecount, lettercount);}
that will fix the conflicting types error. but also you'll have to change " " to ' ' if you are checking on characters.
Upvotes: 0
Reputation: 30136
Declare the following functions before calling them (i.e., above function main
):
int isLetter(char c);
int isWhitespace(char c);
In function main
:
char c
with int c
isLetter(c)
with isLetter((char)c)
isWhitespace(c)
with isWhitespace((char)c)
previous = c
with previous = (char)c
if (c == "\n")
with if ((char)c == '\n')
The reason for int c
, is that function getchar
returns int
in order support the EOF
indicator.
In function isWhitespace
, change the conditional-statement to:
if (c == ' ' || c == '\n' || c == '\r' || c == '\t')
Upvotes: 2
Reputation: 9680
EOF
is an integer value which indicate the end of input. It's a value such that for any character ch
, ch == EOF
is always false. Therefore, you should always compare a value of int
type with EOF
, not char
type. It's working on your machine because the char
type is implemented as signed char
but on machines where char
type is unsigned char
, this won't.
Now coming to the warnings and errors
The scope of a function is from the point of its definition or declaration till the end of the program. You are calling the functions like isLetter
in main
before they have been declared.
"\n"
is a string literal, not a character. So are " "
and " "
. The string literal here evaluates to a pointer to its first element and you are comparing this pointer with a character - a different type. You should, instead, compare with '\n'
, ' '
, '\t'
respectively.
Upvotes: 0
Reputation: 106012
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')
Upvotes: 5
Reputation: 108978
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++;
Upvotes: 2