0

I am attempting to make my program but everytime I try, it tells me I have not identified my variable "i" eventhough I identified it as an integer above. Does anyone know what I'm doing wrong and how to fix it? Here is the code I have so far and the error message I keep on getting. Thanks.

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>

int main(void)
{
    string text = get_string("Text: ");
    int letters, words, sentences; 
    letters = 0;
    words = 0;
    sentences = 0;
    int length = strlen(text);
    for (int i = 0; i < length; i++);
    {
        if (isalpha(text[i]))
            letters++;
        if (isspace(text[i]))
            words++; 
        if text[i] == '.' || text[i] == '!' || text[i] == '?'
            sentences++;
}
readability.c:16:26: error: use of undeclared identifier 'i'
        if (isalpha(text[i]))
                         ^
readability.c:18:26: error: use of undeclared identifier 'i'
        if (isspace(text[i]))
                         ^
0

Try adding to isalpha(text[i]) and isspace.

Add != 0 to the end to make it,

if (isalpha(text[i]) != 0) and

if (isspace(text[i]) != 0)

If that doesn’t work, try making a new variable and declare the value as the isalpha statement, and the isspace statement.

Hope I helped!

5
  • Hey sorry I tried what you said but it still does not seem to work. When I tried setting up variables I did string x = isalpha(text [i]); and string y = isspace(text[i]); but it gave me an error message saying "incompatible integer to pointer conversion initializing 'string' (aka 'char *') with an expression of type 'int'". I think it is not working because i is an int and x and y are strings. Do you know what I could do to fix this? – ac743 Aug 11 '20 at 0:38
  • You should declare the statement as an int, because it returns 0 if true, and anything other than 0 if not. – azh412 Aug 11 '20 at 1:45
  • And inside the if statement, if (isalpha(text[i]) != 0) and if (isspace(text[i]) != 0) – azh412 Aug 11 '20 at 1:45
  • Ok thanks the program seems to be working now. I didn't need to use != 0 though. – ac743 Aug 11 '20 at 2:26
  • Oh, ok! Mark the answer as solved if I helped you! – azh412 Aug 11 '20 at 17:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .