0

enter image description here

Hi, I'm having trouble with this. Is there something wrong with my code line or should i just redo everything?

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

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

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

        for (int i = 0; 1 < strlen(text); i++)
        {
            if (isalpha(text[i]))
            {
                letters++;
            }

            else if (text[i] == ' ')
            {
                words++;
            }
            else if (text[i] == '.' || text[i] == '?' || text[i] == '!')
            {
                sentences++;
            }
        }


    float L =  (float) letters /  (float) words * 100;
    float S =  (float) sentences /  (float) words * 100;
    float calculation = (0.0588 * L- 0.296 * S -15.8);
    int index = round(calculation);
    if (index < 1)
     {
         printf("Before Grade 1\n");
     }
     else if (index > 16)
     {
         printf("Grade 16+\n");
     }
     else
     {
         printf("Grade %.of\n", index);
     }
     return 0;
}
3
  • Can you please edit the question and replace the photo with cut and pasted code? It's difficult to read the text in photos and nobody wants to transcribe code for testing or review. Also, this doesn't appear to have the line of code creating the seg fault. Do you know how to find the line with the seg fault?
    – Cliff B
    Commented May 6, 2022 at 5:29
  • Hi, so sorry! i just edited the thing and reuploaded it again
    – Phạm Tú
    Commented May 6, 2022 at 5:52
  • i debug50 it, and the string text line is where the bug is located at. I do not know how or change to what. Everywhere i looked it said that the code i wrote for string text its's right
    – Phạm Tú
    Commented May 6, 2022 at 5:54

1 Answer 1

1

This is why it's important that you cut and paste the code, rather than someone transcribing it from an image. Likely would have missed it otherwise. ;-)

The seg fault occurs on line 17:

    for (int i = 0; 1 < strlen(text); i++)

Look closely at this line. There's a typo. Specifically, the test clause has a "1" (the number one) where it should have an i.

You might want to read this post too.
Do YOU know how to find a seg fault?? Advice to new programmers

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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