0

On pset2 Readability and for the life of me I can't figure out why my variables are not updating. Whenever I try to print the "letters", "words" and "sentences" variables to screen to debug, it seems that none of my code to update those variables when combing through the string is working.

Is there something obvious I'm missing?

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


int main(void)
{

    string text = get_string("Text: ");

    //Define variables WORDS, LETTERS and SENTENCES from inputted text
    int letters = 0;
    int words = 1;
    int sentences = 0;


    for (int i = 0; i > strlen(text); i++)
    {
        //If character is determined to be alphabetical, increase letters by 1
        if(isalpha(text[i]))
        {
            letters++;
        }

        //If character is determined to be a space, increase words by 1
        if(text[i] == ' ')
        {
            words++;
        }

        //If character is determined to be . ? or ! increase sentences by 1
        if(text[i] == '.' || text[i] == '?' || text[i] == '!')
        {
            sentences++;
        }
    }

    /*
    //Find L: Divide WORDS by 100, then multiply by LETTERS
    float L = (words / 100) * letters;

    //Find S: Divide WORDS by 100, then multiply SENTENCES
    float S = (words / 100) * letters;

    //Determine Coleman-Lau index of inputted string with this formula
    float index = 0.0588 * L - 0.296 * S - 15.8;


    //Rounds the index to the nearest whole number
    index = round(index);
    */

    printf("%i, %i, %i", letters, words, sentences);

    //If index > 16, print "Grade 16+"
    /*
    if(index > 16)
    {
        printf("Grade 16+ \n");
    }
    //Else if index < 1, print "Before Grade 1"
    else if(index < 1)
    {
        printf("Before Grade 1 \n");
    }
    //Print the string "Grade: " followed by the index
    else
    {
        printf("Grade %f \n", index);
    }
    */


}
1

Do you see anything wrong here?

for (int i = 0; i > strlen(text); i++)

Will this for loop ever run? Is the test valid?

| improve this answer | |
  • facepalm thank you, I knew it was something obvious! – sociallyinept Feb 8 at 23:24

You must log in to answer this question.

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