-2

I am trying to create a function by which I can check if the data entered match a triangle or not. I managed to do the function, but I am having a trouble in calling it in main. I want the output to be either true or false. Thanks.

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

bool triangle(float a, float b, float c);

int main(void)
{
// This is where I miss the point and need help with. A little explanation would be great. Thanks.
    printf("%d", triangle(3, -7, 8));
}

bool triangle(float a, float b, float c)
{
    if (a <= 0 || b <= 0 || c <= 0)
    {
        return false;
    }
    
    if ((a + b <= c) || (a + c <= b) || (c + b <= a))
    {
        return false;
    }

    return true;


}
Jona
  • 1,218
  • 1
  • 10
  • 20
Bryce
  • 71
  • 8
  • What is not working ? What do you get as an output ? What do you mean by "I miss the point" ? – Jona Jul 02 '20 at 10:09
  • 1
    "I am having a trouble" is not a problem description. What happens when you compile/run that code? Why is that wrong? What should happen instead? – underscore_d Jul 02 '20 at 10:09
  • Did you perhaps want this? https://stackoverflow.com/questions/17307275/what-is-the-printf-format-specifier-for-bool – underscore_d Jul 02 '20 at 10:15
  • 1
    @underscore_d Well, tbh, OP actually states the desired behavior and even though it would be good to explicitly state the actual behavior, it's pretty obvious in this case. – klutt Jul 02 '20 at 10:24
  • If i'm not wrong about the data types, it should be `_Bool`. Isn't it? – Shubham Jul 02 '20 at 10:26
  • @Lucas That's the keyword. stdbool.h defines the macro bool, and I think cs50.h includes that header. – klutt Jul 02 '20 at 10:32
  • Oh, thanks for introducing me to `stdbool.h`. @klutt – Shubham Jul 02 '20 at 11:34

4 Answers4

1

Use printf when ever your condition is validated to print true or false

#include <stdbool.h>
#include <stdio.h>

bool triangle(float a, float b, float c);

int main(void)
{
// This is where I miss the point and need help with. A little explanation would be great. Thanks.
    if(triangle(3, -7, 8))
    printf("True");
    else printf("False");
}

bool triangle(float a, float b, float c)
{
    if (a <= 0 || b <= 0 || c <= 0)
    {
        return false;
    }
    
    if ((a + b <= c) || (a + c <= b) || (c + b <= a))
    {
        return false;
    }

    return true;


}
1

Whenever you use bool it's a good practice to add an extra include (since bool is not primitive type in C):

#include <stdbool.h>

(Or) you can use _Bool type, which doesn't need any additional headers.

You can do something like this:

#include <stdio.h>

_Bool triangle(float a, float b, float c);

int main(void)
{
    printf("%s", triangle(3, -7, 8) ? "true" : "false");
}

_Bool triangle(float a, float b, float c)
{
    if (a <= 0 || b <= 0 || c <= 0)
    {
        return 0;
    }
    
    if ((a + b <= c) || (a + c <= b) || (c + b <= a))
    {
        return 0;
    }

    return 1;


}
Sai Sreenivas
  • 1,690
  • 1
  • 7
  • 16
  • Now the answer doesn't explain why it uses `_Bool` instead, but it doesn't need to do that. – underscore_d Jul 02 '20 at 10:19
  • 1
    @underscore_d added! – Sai Sreenivas Jul 02 '20 at 10:22
  • 2
    `C99` added `_Bool` as a new built-in data type. If you don't know. @underscore_d – Shubham Jul 02 '20 at 10:29
  • `bool` is defined in `cs50.h` – Jona Jul 02 '20 at 10:57
  • @Lucas I know that, but the pre-edit post just dropped the `#include ` and changed `bool` to `_Bool` without explanation; that was my point. – underscore_d Jul 02 '20 at 11:25
  • 1
    @Jona you know, I'm sure that this will be defined in `cs50.h` but that is not a standard header file. Actually I didn't know about that until I searched for `CS50`, which is just an online course on `edX`, so it's better to mention about anything new which you are including in your code. – Shubham Jul 02 '20 at 11:33
  • 1
    Have you checked, what's inside that `cs50.h` header file? Perhaps, they might be using `#include ` inside that file. @Jona – Shubham Jul 02 '20 at 11:36
  • @Lucas As `bool` was neither the issue reported by the OP, neither an underlying issue. I don't understand why it's the first thing mentioned in the answer. I would find it more relevant as a later remark. My point was NOT to focus on this `bool`. – Jona Jul 02 '20 at 11:47
  • @Jona Sorry for the miscommunication. Actually ```cs50.h``` is not a standard header. So I just wanted to add that point but I didn't mention it in the correct way. Now I've edited it. Thanks – Sai Sreenivas Jul 02 '20 at 12:10
1

You can use a condition operator to evaluate the return of this function. if triangle returns true (1) - "true" will be printed. Otherwise, "false" will be printed.

printf("%s", triangle(3, -7, 8) ? "true" : "false");
manish ma
  • 1,706
  • 1
  • 14
  • 19
0

You can try this:

int main(void)
{
// This is where I miss the point and need help with. A little explanation would be great. Thanks.
   bool iden = triangle(3, -7, 8);
   if ( iden == true ) printf( "true" );
   else printf("false");
}

If you use

int main(void)
{
// This is where I miss the point and need help with. A little explanation would be great. Thanks.
    printf("%d", triangle(3, -7, 8));
}

You will get 0, because of it return false.

So,if it return true, you will get 1

Itati
  • 193
  • 11