Home > Software design >  problem set 2 CS50, creating a counter to count only letters on readability, very new to coding
problem set 2 CS50, creating a counter to count only letters on readability, very new to coding

Time:09-09

Really struggling to solve this error, I think it is to do with declaring isalpha incorrectly but thats all I can think of, any advice would be greatly appreciated!

Error

readability.c:5:18: error expected ')' 
int isalpha(char c);
                 ^
readability.c:5:5: note: to match this '('
int isalpha(char c);
    ^    
/usr/include/ctype.h:190:31 note: expanded from macro 'isalpha' 

Code

`int isalpha(char c);

int main(void);

{   int length = 0;
    string text = get_string("text: ");
    for (int i = 0;  text[i] != '\0'; i  )
    {
        if (isalpha(text[i]))
        {
            length  ;
        }
    }
}`
     

CodePudding user response:

It seems that isalpha is defined as a macro in ctype.h:

# define isalpha(c) __isctype((c), _ISalpha)

That's why you cannot write the prototype of the function in your code.

It would have been the wrong prototype anyhow, the correct one would be int isalpha(int);. The function takes the character as an int, not a char.

To solve your problem: Remove the line int isalpha(char c);. The error message hints that you already have #include <ctype.h> above the shown code snippet.

There is also a trailing ; at the line int main(void);, which you have to remove.

https://godbolt.org/z/q1cWKj1ns

CodePudding user response:

Agree; error message indicates line 5 so code example is incomplete. Still, isalpha() is generally implemented as a macro so no prototype is needed. Should be int islpha(int c); anyway.

  • Related