0

include

include

include

include

include

include

int letters = 0;
int words = 0;
int sentences = 0;

int main(void) { string text = get_string("Text: ");

for (int i = 0, len = strlen(text); i < len; i++)
    {
    if (isalpha(text[i]))
       {
        letters++;
       }
    if (text[i] == ' ' && text[i + 1] != ' ')
       {
        words++;
       }
    if ((text[i] == '.') || (text[i] == '!') || (text[i] == '?'))
       {
        sentences++;
       }

    }

float L = (letters / (float) words) * 100;
float S = (sentences / (float) words) * 100;
int index = round(0.0588 * L - 0.296 * S - 15.8);

if (index < 1)

    {
        printf("Before Grade 1\n");

    }

else if (index > 16)

    {
    printf("Grade 16+\n");

    }

else

    {
        printf("Grade %i\n", index);

    }

}

  • Why do you think that there is a problem with the placement of the brackets? – Cliff B May 29 '20 at 16:36
  • i can't get past my first if statement below my for loop. As it is this prints out Before Grade 1 for all inputs but i don't understand why. – wilburch May 29 '20 at 21:43
0

A common mistake here:if ((i = 0 ..... you're setting your 'i' variable to 0, so your code is running endlesly. Also remember to add the newline in your 'printf' answers.

  • okay thank you. I changed that element and the output is still wrong it will only output my first if statement after my for loop. Any clues? – wilburch May 29 '20 at 21:44
  • I check your code changing the couple of things I sugested before and works fine. It even passes 'check50'. Could you post the line you changed? – Tritum May 30 '20 at 6:08
  • I reposted my code with changes up top i still am only getting some parts to work. I'm sorry I'm very new to this. Thank you for your help. – wilburch May 30 '20 at 10:50
  • Don't beat yourself up. Your logic is very good. In fact, in your previous version you did have it nailed. The only problem was in the begining of the line, you had 'i = 0' which is an asignment, instead of 'i == 0' which is a comparison. Your previous version was better in my opinion, because it counted the very first word. If you want to keep this later version, I'd sugesst to start the word count with 1, as when counting the spaces between words, you'll always miss one. – Tritum May 30 '20 at 11:28
  • Thank you so much i see the way it works now. I reverted to the old code and made that change you suggested and it is working now. Thank you again!! – wilburch May 30 '20 at 11:38
0

Try printing out all of the intermediate values and index, and see what they are. Are the numbers that are being calculated correct?

That, of course, in addition to fixing the == vs. = in your if statement.

  • why would I not want equality signs in my if statement? – wilburch May 30 '20 at 10:51
  • a single = sign is an assignment, a double == sign is a test for equality. The latter is what you want to use in a test to see if two things are equal. – Cliff B May 30 '20 at 16:09

You must log in to answer this question.

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