0

I replaced semicolon with a coma as sugested erlier. I compile the code but when I enter the text to receive Grade 3 nothing happens, again anny sugestion. Thanks.

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

int main(void) { //Get user input to count text input string s = get_string("Text: ");

int count_letters = 0;
int count_words = 0;
int count_sentences = 0;

//Count number of letters uper and lower case, words and centences
for (int i = 0, n = strlen(s); i < n; i++)
{
    if (isalpha(s[i]))
    {
        count_letters++;
    }
    if (s[i] == '!' || s[i] == '?' || s[i] == '.')
    {
        count_sentences++;
    }
    if ((i = 0 && s[i] != ' ') || (i != n - 1 && s[i] == ' ' && s[i + 1] != ' '))
    {
        count_words++;
    }
}
//This will calculate average number of letters and sentences per 100 words
float L = 100 * (count_letters / count_words);
float S = 100 * (count_sentences / count_words);

//Coleman Liau index this formula will round up to the nearest number
int formula = round(0.0588 * L - 0.296 * S - 15.8);

if (formula < 1)
{
    printf("Grade 1\n");
}
else if (formula >= 16)
{
    printf("Grade 16+");
}
else
{
    printf("Grade %i\n", formula);
}

}

~/pset2/readability/ $ make readability

clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow readability.c -lcrypt -lcs50 -lm -o readability ~/pset2/readability/ $ ./readability Text: Congratulations! Today is your day. You're off to Great Places! You're off and away!

1 Answer 1

1

The first error is usually the trigger for the entire cascade of errors, therefore we will look at the first error:

for (int i = 0, len = strlen(s), i < len; i++)

If we look at the structure of a FOR loop in C, we see that the three different parts it can contain are separated by a semicolon (;), and not by a comma only. You can easily learn the different ways in which it is possible to write a FOR loop, but the initialization, the end-of-cycle condition and the update of the variable are always separated by a semicolon.

6
  • Thanks foor the quick explanation, I have adjusted the semicolon all before and this shows the same and other error notifications [ for (int i = 0; len = strlen(s); i < len; i++) ]
    – Matrix
    May 6, 2021 at 11:04
  • The initialization is wrong, if you have to initialize more than one variable they must be separated by a comma, the rest of the body of the for loop are separated by a semicolon. Your code has more errors but it is absolutely necessary that you learn to interpret it. [ for (int i = 0, len = strlen(s); i < len; i++) ]
    – MARS
    May 6, 2021 at 12:51
  • Hi Mars. I replaced semicolon with a coma as you sugested erlier and it compile with no errors. But when I enter the text to receive Grade 3 nothing happens any susetion. I think it has to do with the float, if I change anything in float I receive multyple errors, why this could heppens. Thanks
    – Matrix
    May 7, 2021 at 13:34
  • You have an infinite loop (I love this phrase), I suggest you check the IF conditions inside the for loop, in one of them you set i = 0 so that the end of cycle condition is never reached
    – MARS
    May 7, 2021 at 20:47
  • I remind you that the assignment operator in C is = and the comparison operator is ==
    – MARS
    May 7, 2021 at 20:50

You must log in to answer this question.

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