Search code examples
clogiccs50readability

What's wrong with my code? CS50 Pset2: Readability


1- I don't know what's wrong with my code, this is ## help ## readability CS50 pset2. 2- The problem seems to be in float grade (string grade) function. 3- I am a beginner and it's my second time coding in C so if I missed something important please tell me, or if you have any tips that make my code looks better, thank you!

   //Libraries
    #include <cs50.h>
    #include <ctype.h>
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    
    //Functions
    int count_letters(string letter);
    int count_words(string word);
    int count_sentences(string sentence);
    float grade(string grade);
    
    int main(void)
    {
        string par = get_string("text: ");
    }
    
    
    int count_letters(string letter)
    {
        int letters = 0;
        for (int i = 0, n = strlen(letter); i < n; i++)
            {
                 if (isalpha(letter[i]))
                      letters++;
            } 
        return letters;
    }
    
    
    int count_words(string word)
    {
        int words = 1;
        int spaces = 0;
        int nword;
        nword = strlen(word);
        for (int i = 0; i < nword; i++)
            {
                 if (isspace(word[i]))
                      spaces++;
                      words = spaces + 1;
            }
        return words;
    }
    
    
    int count_sentences(string sentence)
    {
        int sentences = 0;
        int nsentence;
        nsentence = strlen(sentence);
        for (int i = 0; i < nsentence; i++)
            {
                 if (sentence[i] == '.' || sentence[i] == '!' || sentence[i] == '?')
                      sentences++;
            }
        return sentences;
    }
    
    
    float grade(string grade)
    {
        //here comes my problem. variables like letters, words, and sentences are "undeclared identifier"
     float x = 0.0588 * (100 * letters / words) - 0.296 * (100 * sentences / words) - 15.8;
                 if (x < 16 && x >= 0)
                    {
                      printf("Grade %i\n", (int) round(x));
                    }
                 else if (x >= 16)
                    {
                      printf("Grade 16+\n");
                    }
                 else
                    {
                      printf("Before Grade 1");
                    }
    }               

Solution

  • The compiler is telling you exactly what the problem is - you haven't declared letters, words, or sentences in the grade function.

    The declarations of those variables in other functions are local to those functions - they are not visible from within the grade function.

    You have a couple of bigger issues, though. For one thing, none of your functions are being called from anywhere - main reads a string and then immediately exits without doing anything else. For another, grade doesn't have a way of knowing what values should be in letters, words, or sentences.

    What you probably want to do is call your counting functions from within grade like so:

    float grade( string s )
    {
      int letters = count_letters( s );
      int words = count_words( s );
      int sentences = count_sentences( s );
    
      float x = ...;
      ...
    }
    

    and then call grade from main as

    int main( void )
    {
      string par = get_string( "Text: " );
      grade( par );
    }
    

    Alternately, you could call each of the counting functions from main and pass the results as arguments to grade:

    int main( void )
    {
      string par = get_string( "Text: " );
    
      int letters = count_letters( par );
      int words = count_words( par );
      int sentences = count_sentences( par );
    
      grade( letters, words, sentences );
    }
    

    in which case both the declaration and definition of grade should be

    float grade( int, int, int ); // declaration
    
    float grade( int letters, int words, int sentences ) // definition
    {                                                    // since they are declared as arguments,
      float x = ...;                                     // there's no need to declare them in the body of the function
    }
    

    Now, here's a question - do you intend for anyone to use the value of x after grade exits? If so, then you need to add a

    return x;
    

    to the end of grade.

    If not, then change the function's return type from float to void in both the declaration and definition:

    void grade( string ); 
    
    void grade( string s ) { ... } // definition