Home > Blockchain >  C - One of my Variables not calculating and or loading correctly
C - One of my Variables not calculating and or loading correctly

Time:10-11

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


//preload variables.
float S, L;
int get_sent(string text);
int get_words(string text);
int get_letter(string text);
int get_words(string text);

int main(void)
{

//get Text to be analyzed from user.
string text = get_string("Text: ");

//get the number of letters within text.
int letters = get_letter(text);
//get the number of words within text.
int words = get_words(text);
//get the number of sentences within the text.
int sent = get_sent(text);


// MATHS
S = sent / words * 100;  <---- RIGHT HERE!!!  when tested S = 0.00000 no matter what.
L = letters / words * 100;
float index = (0.0588 * L) - (0.296 * S) - 15.8;
int grade = round(index);

if (grade < 1)
{
    printf("Before Grade 1\n");
}
if (grade > 1 && grade < 16)
{
    printf("Grade %i\n", grade);
}
if (grade > 16)
{
    printf("Grade 16 \n");
}




//print text back to user, // this after testing.
printf("%s\n", text);
//print how many letters were typed back to user, // this after testing.
printf("%i Letters\n", letters);
//print how many words were typed back to user, // this after testing.
printf("%i Words\n", words);
//print how many sentences were typed back to user, // this after testing.
printf("%i Sentences\n", sent);
//print index as a float for testing, // this after testing.
printf("index = %f\n", index);
// Print grade int after float index was rounded, // this after testing.
printf("grade = %i\n", grade);

printf("S = %f\n", S);
printf("L = %f\n", L);

}

//get the amount of letters used in the string counting only a-z and A-Z, no spaces or special 
chars.
int get_letter(string text)
{

int letter = 0;
//count the letter in the string from user and count only upper and lowercase letters all = 1 
"point" per letter.
for (int i = 0, len = strlen(text); i < len; i  )
{
    //count the letters a-z && A-Z.
    if (isalpha(text[i]))
    {
        //add a "point" to letter.
        letter  ;
    }
}
//return "points" back to main.
return letter;
}

int get_words(string text)
{
int word = 1;

for(int i = 0, len = strlen(text); i < len; i  )
{
    if (isspace(text[i]))
    {
        word  ;
    }
}
return word;
}

int get_sent(string text)
{
int sent = 0;

for (int i = 0, len = strlen(text); i < len; i  )
{
    if (text[i] == 46 || text[i] == 33 || text[i] == 63 )
    {
        sent  ;
    }
}
return sent;
} 

With testing my code S = 0.000 No matter what is entered. return sent returns the correct amount of sentences but when I S = sent / words *100; it doesn't seem to use the return to load sent correctly and I've bashed my face into the keyboard for an hour trying to figure out why. I'm done other then that.

CodePudding user response:

In your code

 S = sent / words * 100;

will perform integer arithmetic, as all involving operands are int. If you need a floating point arithmetic, you need to enforce it. Something like

 S = sent / (float) words * 100;

Same goes for other statements, too.

CodePudding user response:

For starters there is no any need to declare the variables S and L in the file scope

//preload variables.
float S, L;

In general it is a bad idea. Declare variables in the minimum scope where they are used. So you should declare the variables S and L in main.

You two times declare the function get_words.

int get_sent(string text);
int get_words(string text);
int get_letter(string text);
int get_words(string text);

Though it does not produce a compiler error nevertheless it can confuse readers of the code. So remove one declaration.

The function strlen has the return type size_t. in general the signed type int is not large enough to store lengths of strings. So your function should have the unsigned type size_t as the return type.

size_t get_sent(string text);
size_t get_words(string text);
size_t get_letters(string text);

Within the functions' loops calls of the function strlen is redundant and inefficient.

For example the function get_letter that should be renamed as get_letters similarly to the name get_words can look the following way

size_t get_letters(string text)
{
    size_t letters = 0;

    // count the letter in the string from user and count 
    // only upper and lowercase letters all = 1 "point" per letter.

    for ( ; *text != '\0';   text )
    {
        //count the letters a-z && A-Z.
        if ( isalpha( ( unsigned char )*text ) )
        {
            //add a "point" to letter.
           letters  ;
        }
    }

    //return "points" back to main.
    return letters;
}

And in main the function is called like

size_t letters = get_letters(text);

To output the value you need to use the conversion specification %zu instead of %i.

//print how many letters were typed back to user, // this after testing.
printf("%zu Letters\n", letters);

Correspondingly the variables words and sent also should be declared as having the type size_t

//get the number of words within text.
size_t words = get_words(text);
//get the number of sentences within the text.
size_t sent = get_sent(text);

and you need to use the conversion specification %zu instead of %i to output them.

//print how many words were typed back to user, // this after testing.
printf("%zu Words\n", words);
//print how many sentences were typed back to user, // this after testing.
printf("%zu Sentences\n", sent);

The function get_words is incorrect. It does not count words. It counts spaces. For example if the string text contains adjacent spaces then the function returns an incorrect value.

The function can be defined the following way

size_t get_words( string text )
{
    size_t words = o;

    while ( *text != '\0' )
    {
        while ( isspace( ( unsigned char )*text ) )   text;

        if ( *text != '\0' )
        {
              words;
            while ( *text && !isspace( ( unsigned char )*text ) )   text;
        } 
    }

    return words;
}

The function get_sent also should be rewritten. At least it is a bad idea to use magic numbers like in this statement

if (text[i] == 46 || text[i] == 33 || text[i] == 63 )

because it makes the code unclear and un readable.

As for your problem then you are using the integer arithmetic instead of the floating point arithmetic as for example

S = sent / words * 100;
L = letters / words * 100;

You should write

S = ( float )sent / words * 100;
L = ( float )letters / words * 100;
  • Related