Search code examples
arraysreadability

error - [-Werror,-Wunused-value] - don't know what it means


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

int letters = 0;
int words = 0;
int sentences = 0;

int main(void)
{
    string text = get_string("Text: ");
    printf("\n");
    
    for(int j = 0; j < strlen(text); j++)
    {
        if((text[j] >= 'a' &&  text[j] <= 'z') || (text[j] >= 'A' && text[j] <= 'Z'))
        {
            letters++;
        }
        
        if(text[j] == ' ')
        {
            words++;
        }
        
        if(text[j] == '.' || text[j] == '!' || text[j] == '?')
        {
            sentences++;
        }
    }
    printf("Letters: %i\n", letters);
    printf("Words: %i\n", words);
    printf("Sentences: %i\n", sentences);
    
    float result = 0.0588 * ("%i / %i", letters, words) - 0.269 * ("%i / %i", words, sentences) - 15.8;
    printf("%f\n", result);











~/pset1/readability/ $ make readability
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    readability.c  -lcrypt -lcs50 -lm -o readability
readability.c:36:30: error: expression result unused [-Werror,-Wunused-value]
    float result = 0.0588 * ("%i / %i", letters, words) - 0.269 * ("%i / %i", words, sentences) - 15.8;
                             ^~~~~~~~~
readability.c:36:41: error: expression result unused [-Werror,-Wunused-value]
    float result = 0.0588 * ("%i / %i", letters, words) - 0.269 * ("%i / %i", words, sentences) - 15.8;
                                        ^~~~~~~
readability.c:36:68: error: expression result unused [-Werror,-Wunused-value]
    float result = 0.0588 * ("%i / %i", letters, words) - 0.269 * ("%i / %i", words, sentences) - 15.8;
                                                                   ^~~~~~~~~
readability.c:36:79: error: expression result unused [-Werror,-Wunused-value]
    float result = 0.0588 * ("%i / %i", letters, words) - 0.269 * ("%i / %i", words, sentences) - 15.8;
                                                                              ^~~~~
4 errors generated.
<builtin>: recipe for target 'readability' failed
make: *** [readability] Error 1

Can someone please help me? I am seriously confused about what I am doing wrong. I am trying to use the Coleman-Liau index formula (index = 0.0588 * L - 0.296 * S - 15.8 - L is the average number of letters per 100 words in the text, and S is the average number of sentences per 100 words in the text.)


Solution

  • ("%i / %i", letters, words)
    

    This doesn't work the way you think it does, you are confused by the arguments printf function takes. If you need division, just use / alone: letters / words.

    Since these are integers, and you probably want a ratio with fractional part, it is also a good idea to cast one of those variables to float or double: letters / (double) words