-2

I am taking CS50x Harvard on edX, and I'm currently working on problem set 2, readability. I've compiled my code, and it should work to determine the reading level like it's supposed to. However, every time I run the program, no matter what I put in it gives back the answer before grade 1.

Where is the error?

Here's the code.

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

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

    // Start words on 1 because theres always 1 less space then words
    int letters = 0;
    int words = 1;
    int sentences = 0;

    // Count characters
    for (int i = 0; i < strlen(text); i++)
    {
        // Check for letters
        if (isalpha(text[i]))
        {
            letters++;
        }
    // Check for spaces
        else if (text[i] == ' ')
        {
            words++;
        }
        else if (text[i] == '.' || text[i] == '?' || text[i] == '!')
        {
            sentences++;
        }
    }

    float L = (float)letters / (float)words * 100;
    float S = (float)words / (float)sentences * 100;


    int index = (int)round(0.0588 * L - 0.296 * S - 15.8);
    if (index > 16)
    {
        printf ("Grade 16+\n");
    }
    else if (index < 1)
    {
        printf ("Before Grade 1\n");
    }
    else
    {
        printf ("Grade %i\n", index);
    }

}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • FYI, there are dozens of universities and colleges. Each may have a different numbering system for their courses. Some may have a CS50 class that doesn't match the one you are taking. I highly recommend adding clarification as to which CS50 class and it's subject matter. – Thomas Matthews Aug 17 '22 at 16:48
  • 3
    @ThomasMatthews CS50 is a Harvard self study class. It's known enough on SO to have a tag for it, as you can see on this post. – blackbrandt Aug 17 '22 at 16:49
  • You should use `using std::string;` and `using std::cout;` to your program, preferably near the top. – Thomas Matthews Aug 17 '22 at 16:50
  • Simply adding a sample string and the grade you expect it to get would help. At the moment it's obvious that the code could give a result of 'before grade 1'. What we need is a case where you think that is wrong (plus what you think the grade should be and why). – john Aug 17 '22 at 16:50
  • @ OP, this looks like an excellent time to learn how to use a debugger. Is `index` the value you expect it to be? – blackbrandt Aug 17 '22 at 16:50
  • Your definition of *word* is incorrect. You are counting the number of spaces. Text can have multiple spaces between words. – Thomas Matthews Aug 17 '22 at 16:51
  • Okay well this is a paragraph that I know should recieve grade 8 and it says before 1, sorry Im very new so Im just trying to learn I dont know alot, Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, "and what is the use of a book," thought Alice "without pictures or conversation?" – Not a Guru Jordan Aug 17 '22 at 16:52
  • @blackbrandt: it was marked as C++ when I was reading it and adding the comments. – Thomas Matthews Aug 17 '22 at 16:52
  • Sorry its supposed to be c – Not a Guru Jordan Aug 17 '22 at 16:53
  • 1
    `string text` is not C. – john Aug 17 '22 at 16:54
  • how is it not c they use it in the course as c – Not a Guru Jordan Aug 17 '22 at 16:56
  • @ThomasMatthews how do I fix this Im really stuck – Not a Guru Jordan Aug 17 '22 at 16:56
  • @NotaGuruJordan Well here's the problem, by the formulae you have written down in your program the result of 'before 1' is correct. I don't know what the formulas should be but presumably you have made some mistake in translating them into C. Can you provide a reference to the formulae? – john Aug 17 '22 at 16:58
  • @NotaGuruJordan OK, I guess some magic in makes it legal C. It is not legal C if you remove that non-standard header. – john Aug 17 '22 at 17:00
  • @john yes, I've never gone thru cs50 stuff myself, but from previous posts about it on SO, I recall that cs50.h contains a `typedef char* string` or something similar, and I assume the associated `get_string` function does the necessary memory allocation for you. – yano Aug 17 '22 at 17:08
  • @blackbrandt Okay ill try the debugger I dont know how to use it tho will have to learn – Not a Guru Jordan Aug 17 '22 at 17:08
  • @john yes the formula is 0.0588 * L - 0.296 * S - 15.8, L is the avg number of letters in 100 words and S is the avg number of sentences per 100 words – Not a Guru Jordan Aug 17 '22 at 17:10
  • @john its called the Coleman-Liau Index – Not a Guru Jordan Aug 17 '22 at 17:11
  • There is a whole Stack Exchange site dedicated to [Harvard CS50](https://en.wikipedia.org/wiki/CS50): *[CS50](https://cs50.stackexchange.com/tour)*. But do check what is on-topic, etc. before posting – Peter Mortensen Aug 20 '22 at 09:55

1 Answers1

0

You got your formulae for L and S incorrect

Here's the correct formulae

float L = ((float)letters*100) / (float)words;
float S = ((float)sentences*100) / (float)words;

L is the average number of letters per 100 words and S is the average number of sentences per 100 words, reference here.

If you had only printed out the intermediate values of L and S and compared them to your expectations then you would have seen the error immediately. Learning how to debug your own code is a vital skill for a programmer.

john
  • 85,011
  • 4
  • 57
  • 81