Home > Enterprise >  CS50 PSET2 Readability 2022
CS50 PSET2 Readability 2022

Time:10-28

Okay! I'm really new at programing, so I don't really know whats going on... my program compiles just fine, but it only says 'Before Grade 1', and I saw with debug50 that float L and float S are not doing the math that they are supposed to do, and I have NO clue what is wrong This is my code

`

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

int count_sentences(string);
int count_words(string);
int count_letters(string);

int letters;
int sentences;
int words;

int main(void)
{
// prompt user for text
 string text = get_string("Text: ");

// calcutate the reading level
float L =  100 * (float) letters / words;
float S = 100 * (float) sentences / words;
int index = round(0.0588 * L - 0.296 * S - 15.8);
//output the result
if (index < 1)
{
 printf("Before Grade 1\n");
}
else if (index > 16)
{
    printf ("Grade 16 \n");
}
else
{
    printf("Grade %i\n", index);
}
}

//string is an array of characters, just go through each letter and check if it is a char or not, and add to the counter

int letters = 0;
int count_letters(string text)
{
 for (int i = 0; i < strlen(text); i  )
 {
    if (isalpha(text[i]) != 0)
    {
        letters  ;
    }
 }
 return letters;
}

//calculation for words is made by counting the number of spaces   1
int words = 1;
int count_words(string text)
{
    for (int i = 1; i < strlen(text); i  )
    {
      if (isspace (text[i]) != 0)
  {
       words  ;
  }
    }
    return words;
}

// sentences end with ./!/?, so just count them
int sentences = 0;
int count_sentences(string text)
{
for (int i = 0; i < strlen(text); i  )
{
    if (text[i] == '.' || text[i] == '!' || text[i] == '?')
    {
        sentences  ;
    }

}
return sentences;
}

` Thanks!

CodePudding user response:

I actually answered a similar type of coding question a while back. Basically, what is missing is the derivation of the letter, word, and sentence count because in your current program, you do not call the counting functions even though they are defined. Just defining functions is not enough in C. Somewhere, they need to be called. In looking at your code, the most likely place would be right after your code prompts and receives a text string from the user.

    // prompt user for text
    string text = get_string("Text: ");
    
    letters = count_letters(text);      /* The functions need to be called to derive letters, words, and sentences */
    words = count_words(text);
    sentences = count_sentences(text);

There probably are other bits to test and refine, but you might want to start with that.

  • Related