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

int count_sentences(string text);

int main(void)
{
    string text = get_string("Text: ");
    int count_sentences(string text);
    {
         int sentences = 1;
         for (int j = 0; j < strlen(text); j++)
         {
              if (text[j] == '.' || text[j] == '?' || text[j] == '!')
              {
                 sentences++;
              }
         }
         return sentences;
    }
    printf("Sentences: %i\n", count_sentences);
}

I am doing the cs50 course through edx. I am stuck on a problem set in week two that has us write a program to assign a reading grade level to a user given text. I have figured out how to count the letters and words portion of the problem. However, the sentence count portion is giving me a hard time. Above is the test piece I have drafted before I implement it in my final code to make sure it functions the way I want. When I try to make the code it gives me this error:

test.c:24:31: error: format specifies type 'int' but the argument has type 'int (*)(string)' (aka 'int (*)(char *)') [-Werror,-Wformat]
   24 |     printf("Sentences: %i\n", count_sentences);
      |                        ~~     ^~~~~~~~~~~~~~~
1 error generated.
make: *** [<builtin>: test] Error 1

I cannot figure out what I am doing wrong. Any advice would be greatly appreciated.

8
  • 2
    Your count_sentences is a function, and should be defined outside of main (for canonical C). When you want to call it, you need to write it's name and pass the parameters it expects in parentheses. Commented Oct 28, 2024 at 20:24
  • 1
    Surely CS50 isn't teaching you to use nested functions, which are non-standard? Commented Oct 28, 2024 at 20:24
  • 1
    You need to use () to call the function: count_sentences() Commented Oct 28, 2024 at 20:26
  • 1
    @Barmar That is also an issue, that might fall under 'not reproducible or caused by typos.' Commented Oct 28, 2024 at 20:30
  • 2
    Thank you all for your feedback, the nesting of the function was my own error that I did not even realize I was doing. It's not something that is being taught in the course. After looking at your responses, I did some tweaking and got it to work how I wanted. So, thank you all again. Commented Oct 28, 2024 at 21:26

1 Answer 1

3

You have multiple issues.

  • You are trying to nest the count_sentences function inside main. Don't do this. Some compilers might allow it as a specific extension, but there's no need at this stage of your learning career to rely on those.
  • You've only declared that function because of the semicolon. The block following int count_sentences(string text); is always executed. That means that return makes your print unreachable.
  • You haven't called count_sentences in your call to printf. Even if you do, you'd have to provide a string (alias for char *) argument to make it a correct call.
  • It's comparatively minor, but you have extraneous includes.

If we correct these issues:

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

int count_sentences(string text);

int main(void)
{
    string text = get_string("Text: ");
    printf("Sentences: %i\n", count_sentences(text));
}

int count_sentences(string text) 
{
    int sentences = 1;
    for (int j = 0; j < strlen(text); j++)
    {
        if (text[j] == '.' || text[j] == '?' || text[j] == '!')
        {
            sentences++;
        }
    }
    return sentences;
}

There remains the algorithmic issue of whether you're actually counting sentences properly. Your code may need to be a bit more sophisticated to detect things like an input string "???" which would tell us there are four sentences.

Note that when the compiler error tells you the argument is of type int (*)(char *) be aware that this is a function pointer type. In this case a pointer to a function which takes a char * and returns an int. That's exactly what count_sentences is. When that bare function name is passed to a function it decays to a pointer to the function.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.