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

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

    // to count letters

    for(int i=0;text[i]!='\0';i++)
    { i=text[i];
    printf("letters: %i\n", i);
    }

    // to count words

     for(int j=1;text[j]=='\0';j++)
      { j=text[j];
    printf("words:%i\n", j);
    }

    // to count sentences

    for(int k=0;text[k]=='.' || text[k]=='?' || text[k]=='!';k++)
    { k=text[k];
    printf("sentences: %i\n",k);
    }
    {
        float l=( i/j)*100;
    float s=( k/j)*100;
    float index = (0.0588*l - 0.296*s - 15.8);
    int g=round(index);

if (g >= 1 && g <=16)
        {
            printf("Grade %i\n", g);
        }
    else
    {
        if (g < 1)
        {
            printf("Before Grade 1\n");
        }
        if (g > 16)
        {
            printf("Grade 16+\n");
        }
}
}
}
#include<cs50.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>

int main(void)

{
    string text = get_string("text:\n");

    // to count letters
    float letters = 0;
    float words = 1;
    float sent = 0;
    int i= 0 ;


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

    // to count words
   if( isspace(text[i]))
      { words++;

    }

    // to count sentences
      if (text[i] == '.' || text[i] == '!' || text[i] == '?')
    { sent++;

    }
 }
   //  printf("letters: %f; words: %f; sentences: %f\n", letters, words, sent);
    { float l = letters / words * 100;
    float s = sent / words * 100;
    float index = 0.0588 * l - 0.296 * s - 15.8;
    int g= round(index);

   // printf("index rounded:%i\n",g);

if (g >= 1 && g <=16)
        {
            printf("Grade %i\n",g);
        }
    else
    {
        if (g < 1)
        {
            printf("Before Grade 1\n");
        }
        if (g > 16)
        {
            printf("Grade 16+\n");
        }
}
}
}
1
  • Please edit the question and add the compiler error message(s). Commented Apr 22, 2020 at 10:33

2 Answers 2

1

It's a scope problem: When you declare a variable inside a 'for' loop or a function, it only will be available until the loop/function ends. For example in your for loop: for(int i=0;text[i]!='\0';i++) as 'i' is declared and inicialized at the begining of the for loop, as soon as the loop finishes, it'll be destroyed and you won't be able to access to it anymore.

1
  • Yeah. I altered that. you are right.Thank you so much for looking into and helping me find my error. Commented Apr 27, 2020 at 8:03
1

The variables that you create in the for loops only exist within that for loop. You can use the result of a certain iteration of a for loop by eg: including an if function within the for loop to pick up what you are after .

1
  • Yeah. I altered that. you are right, it does get confusing having these variables as I constantly have to look up what was assigned to which counter. I assigned those counters before the loop and corrected minor errors in bracket placement of loops and it worked. Thank you so much for looking into and helping me find my error. Commented Apr 27, 2020 at 8:03

You must log in to answer this question.

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