0

im new to coding and currently working on pset2, readability, and I keep getting this error and can´t find a way to fix it.

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

int count_letters(string a, int b, int c);
int count_words(string a, int b, int c);
int count_sentences (string a, int b, int c);
float index_formula(int letters, int words, int sentences);

int main(void)
{
  string text = get_string("Text: ");
  int length = strlen(text);
  int number_letters = 0;
  int number_words = 0;
  int number_sentences = 0;
  count_letters(text, number_letters, length);
  count_words(text, number_words, length);
  count_sentences(text, number_sentences, length);
  index_formula(count_letters, count_words, count_sentences);
  printf("%f grade\n", index_formula);
}

int count_letters(string a, int b, int c)
{

  for (int i = 0; i < c; i++)
  {
      if (isalpha(a[i]))
      {
          b++;
      }
  }
  return b;
}

int count_words(string a, int b, int c)
{
  for (int i = 0; i < c; i++)
  {
      if ((i == 0 && a[i] != ' ')
      || (i != c - 1 && a[i] == ' ' && a[i + 1] != ' '))
      {
          b++;
      }
  }
  return b;
}

int count_sentences(string a , int b, int c)
{
  for (int i = 0; i < c; i++)
  {
      if (a[i] == '.' || a[i] == '!' || a[i] == '?')
      {
          b++;
      }
  }
  return b;
}

float index_formula(int letters, int words, int sentences)
{
  float L = (letters / words) * 100;
  float S = (sentences / words) * 100;
  float index = (0.0588 * L) (- 0.296 * S) - 15.8;
  return round(index);
}

Im pretty sure im doing it the wrong way, but what im trying to do is (being count_letters; count_words; count_sentences; and index_formula all functions) use the output of each function as an input for index_formula, and then print it, but i keep getting

 error: incompatible pointer to integer conversion passing 'int (string, int, int)' (aka 'int (char *, int, int)') to parameter of type 'int' [-Werror,-Wint-conversion]
    index_formula(count_letters, count_words, count_sentences);

Is there a way to fix it? Sorry if its a really dumb question, but i started programming a few weeks ago, and there is a lot I still dont get. Any help is really appeciated.

2
  • Can you show us your full code.
    – Ojou Nii
    May 20 '20 at 5:24
  • Yes! There it is. Sorry I didnt post the full code at first, thought it was too much. May 20 '20 at 13:11
1

You are not saving the value from your functions. What you're doing is the you call the function then the function counts the sentences and returns the count but your not assigning that return value to any variable, so it's basically doing nothing then you're passing you're functions as the arguments for the index function.

4
  • I tried adding a variable inside the function but keep getting the same error, shouldnt a "return name_variable" at the end of the function work? May 20 '20 at 23:03
  • try doing int x = count_letters(text, number_letters, length); etc... then pass the variables that stored the return value of the function as the parameters of the index_formula function,
    – Ojou Nii
    May 21 '20 at 1:36
  • Thank you, that worked great. So in order to pass the return value of each function I need to make a new variablefor every one of them right? May 21 '20 at 22:44
  • Yes because if you don't your function just runs and nothing changes since when you call a function you create a copy of the variables you passed in, unless you use references as the argument the values won't change
    – Ojou Nii
    May 22 '20 at 6:40

You must log in to answer this question.

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