0

This goes to week 2: Functions mini lecture. We create a custom function for verifying whether we have a valid triangle by assessing it's 3 sides.

I wanted to elaborate on the code. Since, this isn't a graded assignment, but rather soley for practice, I don't find any reason why I shouldn't share my results. ERRORS

// Includes
#include <stdio.h>
#include <cs50.h>
#include <stdbool.h> // to call bool flag variable

// declare functions
bool valid_triangle(float a, float b, float c);

int main(void)
{
    // prompt the user for inputs (s1 for side 1, etc..)
    float s1, s2, s3;
    printf("Enter triangle side 1 :\n");
    scanf("%f, &s1");

    printf("Enter triangle side 2 :\n");
    scanf("%f, &s2");

    printf("Enter triangle side 3 :\n");
    scanf("%f, &s3");


    // calling the function and using a 'flag variable' to evaluate
    bool flag = valid_triangle(s1,s2,s3);
        if(flag == 0)
            printf("A triangle must have positive sides\n and both sides must sum to the other side \n");
        if(flag == 1)
            printf("Those are valid sides of a triangle\n");
}


// define bool_triangle function
bool valid_triangle(float a, float b, float c)
{
    // check all sides are positive
    if (a <= 0 || b <= 0 || c <= 0)
    {
        return false;
    }

    // check that sum of any two sides is greater than third
    if ((a + b <= c) || (a + c <= b) || (b + c <= a))
    {
        return false;
    }

    // if we passed both tests, we're good:
    return true;
}
  • My error is:
$ make triangle
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    triangle.c  -lcrypt -lcs50 -lm -o triangle
triangle.c:13:13: error: more '%' conversions than data arguments
      [-Werror,-Wformat]
    scanf("%f, &s1");
           ~^
triangle.c:16:13: error: more '%' conversions than data arguments
      [-Werror,-Wformat]
    scanf("%f, &s2");
           ~^
triangle.c:19:13: error: more '%' conversions than data arguments
      [-Werror,-Wformat]
    scanf("%f, &s3");
           ~^
triangle.c:23:32: error: variable 's1' is uninitialized when used here
      [-Werror,-Wuninitialized]
    bool flag = valid_triangle(s1,s2,s3);
                               ^~
triangle.c:11:13: note: initialize the variable 's1' to silence this warning
    float s1, s2, s3;
            ^
             = 0.0
triangle.c:23:35: error: variable 's2' is uninitialized when used here
      [-Werror,-Wuninitialized]
    bool flag = valid_triangle(s1,s2,s3);
                                  ^~
triangle.c:11:17: note: initialize the variable 's2' to silence this warning
    float s1, s2, s3;
                ^
                 = 0.0
triangle.c:23:38: error: variable 's3' is uninitialized when used here
      [-Werror,-Wuninitialized]
    bool flag = valid_triangle(s1,s2,s3);
                                     ^~
triangle.c:11:21: note: initialize the variable 's3' to silence this warning
    float s1, s2, s3;
                    ^
                     = 0.0
6 errors generated.
<builtin>: recipe for target 'triangle' failed
make: *** [triangle] Error 1
~/pset1/week2/ $ make triangle
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    triangle.c  -lcrypt -lcs50 -lm -o triangle
triangle.c:13:13: error: more '%' conversions than data arguments
      [-Werror,-Wformat]
    scanf("%f, &s1");
           ~^
triangle.c:16:13: error: more '%' conversions than data arguments
      [-Werror,-Wformat]
    scanf("%f, &s2");
           ~^
triangle.c:19:13: error: more '%' conversions than data arguments
      [-Werror,-Wformat]
    scanf("%f, &s3");
           ~^
triangle.c:23:32: error: variable 's1' is uninitialized when used here
      [-Werror,-Wuninitialized]
    bool flag = valid_triangle(s1,s2,s3);
                               ^~
triangle.c:11:13: note: initialize the variable 's1' to silence this warning
    float s1, s2, s3;
            ^
             = 0.0
triangle.c:23:35: error: variable 's2' is uninitialized when used here
      [-Werror,-Wuninitialized]
    bool flag = valid_triangle(s1,s2,s3);
                                  ^~
triangle.c:11:17: note: initialize the variable 's2' to silence this warning
    float s1, s2, s3;
                ^
                 = 0.0
triangle.c:23:38: error: variable 's3' is uninitialized when used here
      [-Werror,-Wuninitialized]
    bool flag = valid_triangle(s1,s2,s3);
                                     ^~
triangle.c:11:21: note: initialize the variable 's3' to silence this warning
    float s1, s2, s3;
                    ^
                     = 0.0
6 errors generated.
<builtin>: recipe for target 'triangle' failed
make: *** [triangle] Error 1
~/pset1/week2/ $ debug50 make triangle
debug50: make: No such file
~/pset1/week2/ $ check50 make triangle
usage: check50 [-h] [-d] [--offline] [-l] [--log]
               [-o {ansi,json,html} [{ansi,json,html} ...]]
               [--target TARGET [TARGET ...]] [--output-file FILE] [-v] [-V]
               [--logout]
               slug
check50: error: unrecognized arguments: triangle
~/pset1/week2/ $ check50 triangle
Invalid slug
~/pset1/week2/ $ check50 ./triangle
Invalid slug
~/pset1/week2/ $ debug50 ./triangle                                         
Looks like you've changed your code. Recompile and then re-run debug50!
~/pset1/week2/ $ debug50 make triangle
debug50: make: No such file
~/pset1/week2/ $ make triangle
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    triangle.c  -lcrypt -lcs50 -lm -o triangle
triangle.c:13:13: error: more '%' conversions than data arguments
      [-Werror,-Wformat]
    scanf("%f, &s1");
           ~^
triangle.c:16:13: error: more '%' conversions than data arguments
      [-Werror,-Wformat]
    scanf("%f, &s2");
           ~^
triangle.c:19:13: error: more '%' conversions than data arguments
      [-Werror,-Wformat]
    scanf("%f, &s3");
           ~^
triangle.c:23:32: error: variable 's1' is uninitialized when used here
      [-Werror,-Wuninitialized]
    bool flag = valid_triangle(s1,s2,s3);
                               ^~
triangle.c:11:13: note: initialize the variable 's1' to silence this warning
    float s1, s2, s3;
            ^
             = 0.0
triangle.c:23:35: error: variable 's2' is uninitialized when used here
      [-Werror,-Wuninitialized]
    bool flag = valid_triangle(s1,s2,s3);
                                  ^~
triangle.c:11:17: note: initialize the variable 's2' to silence this warning
    float s1, s2, s3;
                ^
                 = 0.0
triangle.c:23:38: error: variable 's3' is uninitialized when used here
      [-Werror,-Wuninitialized]
    bool flag = valid_triangle(s1,s2,s3);
                                     ^~
triangle.c:11:21: note: initialize the variable 's3' to silence this warning
    float s1, s2, s3;
                    ^
                     = 0.0
6 errors generated.
<builtin>: recipe for target 'triangle' failed
make: *** [triangle] Error 1
~/pset1/week2/ $ debug50 ./triangle                                         
Looks like you've changed your code. Recompile and then re-run debug50!
~/pset1/week2/ $ make triangle
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    triangle.c  -lcrypt -lcs50 -lm -o triangle
triangle.c:14:13: error: more '%' conversions than data arguments
      [-Werror,-Wformat]
    scanf("%f, &s1");
           ~^
triangle.c:17:13: error: more '%' conversions than data arguments
      [-Werror,-Wformat]
    scanf("%f, &s2");
           ~^
triangle.c:20:13: error: more '%' conversions than data arguments
      [-Werror,-Wformat]
    scanf("%f, &s3");
           ~^
triangle.c:24:32: error: variable 's1' is uninitialized when used here
      [-Werror,-Wuninitialized]
    bool flag = valid_triangle(s1,s2,s3);
                               ^~
triangle.c:12:13: note: initialize the variable 's1' to silence this warning
    float s1, s2, s3;
            ^
             = 0.0
triangle.c:24:35: error: variable 's2' is uninitialized when used here
      [-Werror,-Wuninitialized]
    bool flag = valid_triangle(s1,s2,s3);
                                  ^~
triangle.c:12:17: note: initialize the variable 's2' to silence this warning
    float s1, s2, s3;
                ^
                 = 0.0
triangle.c:24:38: error: variable 's3' is uninitialized when used here
      [-Werror,-Wuninitialized]
    bool flag = valid_triangle(s1,s2,s3);
                                     ^~
triangle.c:12:21: note: initialize the variable 's3' to silence this warning
    float s1, s2, s3;
                    ^
                     = 0.0
6 errors generated.
<builtin>: recipe for target 'triangle' failed
make: *** [triangle] Error 1
~/pset1/week2/ $ make triangle
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    triangle.c  -lcrypt -lcs50 -lm -o triangle
triangle.c:14:13: error: more '%' conversions than data arguments
      [-Werror,-Wformat]
    scanf("%f, &s1");
           ~^
triangle.c:17:13: error: more '%' conversions than data arguments
      [-Werror,-Wformat]
    scanf("%f, &s2");
           ~^
triangle.c:20:13: error: more '%' conversions than data arguments
      [-Werror,-Wformat]
    scanf("%f, &s3");
           ~^
triangle.c:24:32: error: variable 's1' is uninitialized when used here
      [-Werror,-Wuninitialized]
    bool flag = valid_triangle(s1,s2,s3);
                               ^~
triangle.c:12:13: note: initialize the variable 's1' to silence this warning
    float s1, s2, s3;
            ^
             = 0.0
triangle.c:24:35: error: variable 's2' is uninitialized when used here
      [-Werror,-Wuninitialized]
    bool flag = valid_triangle(s1,s2,s3);
                                  ^~
triangle.c:12:17: note: initialize the variable 's2' to silence this warning
    float s1, s2, s3;
                ^
                 = 0.0
triangle.c:24:38: error: variable 's3' is uninitialized when used here
      [-Werror,-Wuninitialized]
    bool flag = valid_triangle(s1,s2,s3);
                                     ^~
triangle.c:12:21: note: initialize the variable 's3' to silence this warning
~/pset1/week2/ $ make triangle
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    triangle.c  -lcrypt -lcs50 -lm -o triangle
triangle.c:14:13: error: more '%' conversions than data arguments
      [-Werror,-Wformat]
    scanf("%f, &s1");
           ~^
triangle.c:17:13: error: more '%' conversions than data arguments
      [-Werror,-Wformat]
    scanf("%f, &s2");
           ~^
triangle.c:20:13: error: more '%' conversions than data arguments
      [-Werror,-Wformat]
    scanf("%f, &s3");
           ~^
triangle.c:24:32: error: variable 's1' is uninitialized when used here
      [-Werror,-Wuninitialized]
    bool flag = valid_triangle(s1,s2,s3);
                               ^~
triangle.c:12:13: note: initialize the variable 's1' to silence this warning
    float s1, s2, s3;
            ^
             = 0.0
triangle.c:24:35: error: variable 's2' is uninitialized when used here
      [-Werror,-Wuninitialized]
    bool flag = valid_triangle(s1,s2,s3);
                                  ^~
triangle.c:12:17: note: initialize the variable 's2' to silence this warning
    float s1, s2, s3;
                ^
                 = 0.0
triangle.c:24:38: error: variable 's3' is uninitialized when used here
      [-Werror,-Wuninitialized]
    bool flag = valid_triangle(s1,s2,s3);
                                     ^~
triangle.c:12:21: note: initialize the variable 's3' to silence this warning
    float s1, s2, s3;
                    ^
                     = 0.0
6 errors generated.
<builtin>: recipe for target 'triangle' failed
make: *** [triangle] Error 1

1 Answer 1

0

In your scanf lines, only the format code should be in the double quotes. So the line should be

scanf("%f", &s1);

(and likewise for reading the other two sides).

1
  • You solved it! Thank you! Commented Jun 30, 2020 at 15:53

You must log in to answer this question.

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