Click here to Skip to main content
15,887,998 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to do the following:

2. (do while)
The formula for the area of any triangle is:

A = √(s(s-a)(s-b)(s-c)) where s= (a+b+c)/2

Write a program that will allow the user to make at least one area calculation, your program will prompt the user if he wishes to continue and it will stop once the user enters the character N or n. Assume that the length units are cm. Your solution must include a user-define function that returns the area of the triangle and its inputs are the three sides.
float areaTriangle(float sideA, float sideB, float sideC);

I am getting quite a lot of errors and im pretty sure i have declared too many variables and i think i messed up on my function definition and the function call is wrong in my opinion. I appreciate the help.

What I have tried:

C++
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

    float areaTriangle(float sideA, float sideB, float sideC);


int main()
{
    float sideA,sideB,sideC;
    char answer;

    printf("Please enter the length of side A: \n");
    scanf("%f", &sideA);

    printf("Please enter the length of side B: \n");
    scanf("%f", &sideB);

    printf("Please enter the length of side C: \n");
    scanf("%f", &sideC);

    printf("Would you like to solve another\n"
           "exercise?\n"
           " Y or N ");
    scanf("%c", &answer);

     do{
        printf("The area of the triangle is: %lf", &areaTriangle);
     }while( char == 'Y');

      do{
        printf("You have selected to quit out of the program.\n"
               "Thank you for using the program.\n"
     }while( char == 'N');


    return 0;
}

     float areaTriangle(float sideA, float sideB, float sideC)
    {
        float sideVariable;

        sideVariable = (sideA + sideB + sideC) / 2;

        areaTriangle = sqrt(sideVariable(sideVariable - sideA)(sideVariable - sideB)(sideVariable - sideC));

    return areaTriangle;
    }


Some of the errors i got are:
45:warning: format '%lf' expects argument of type double, but argument 2 has type float
46 and 51:expected expression before 'char'
60:note declared here
64:called objective 'sideVariable' is not a function or function pointer
67:incompatible types when returning type 'float (*)(float, float, float)' but '..
68:warning: control reaches end of non-void function
Posted
Updated 16-Mar-20 1:05am
v5
Comments
Patrice T 15-Mar-20 15:08pm    
What errors ?
Patrice T 15-Mar-20 15:19pm    
And the lines of errors are ...

The primary error I see is your assumption of how multiplication works. Writing (a)(b) does not mean multiply a by b in the C language. You have to write it as a*b. The asterisk is the multiplication symbol in C and it must be used. In your case, that means :
C
area = (float) sqrt( sideVar * ( sideVar - sideA ) * ( sideVar - sideB ) * ( sideVar - sideC ) );
I shortened names to make it fit on a line. The other thing is sqrt returns a double so you should cast its result with (float).
 
Share this answer
 
v3
The errors are occuring because your written code isnt understandable for the compiler. This is also called: code bugs.

Normally you get a lengthy explanation with some error code. Read the documentation of the error code and rethink the complained code.

This is the every day job for a software developer. When not sitting in boring meetings ;-)

PS: Z 45 needs a type cast to float
 
Share this answer
 
45:warning: format '%lf' expects argument of type double, but argument 2 has type float
AreaTriangle should return a double not a float.

46 and 51:expected expression before 'char'
char is a reserved word, and denotes a type. You should be comparing the variable answer.

60:note declared here
The string is missing the close parenthesis and terminating semi-colon.

64:called objective 'sideVariable' is not a function or function pointer
sideVariable is a number, you cannot use it as a function.

67:incompatible types when returning type 'float (*)(float, float, float)' but '..
68:warning: control reaches end of non-void function
Change the definition (in both places) to
C++
double areaTriangle(float sideA, float sideB, float sideC)
 
Share this answer
 
1. the program currently does the following:
- prompt the user for the side lengths
- prompt the user whether whe wants to solve another problem
- do a loop to repeatedly caluclate and print the result
- do a loop to repeatedly tell the user 'bye'

Apart from the compiler errors. you have to ask yourself: is that what I want the program to do?

2. The first compiler error refers to the printf statement. You should therefore look up the definition of printf, and it's format specifiers, e. g. here: printf - C++ Reference[^] . If you read that, you'll find that %f tells printf to print a float value, whereas the additional 'l' is a length modifier. However, that length modifier only applies to int, not float.

3. Unlike scanf, printf does not expect a pointer to a variable. You should just pass values or a variable holding the value. If you follow the link above, you'll find example code at the bottom of the page.

4. The while statement at the end of the do loop doesn't make any sense: while (char == 'Y') :
char is a type, not a variable or value.

5. The while statement in the second do loop has the same issue. Of course, both loops don't make sense to start with (see 1.)

6. Rick already pointed out your error regarding the multiplications.

7. The local variable areaTriangle you are using to store the result of the calculation was never defined! You have to define variables before you use them. Also it's a very bad idea to name a variable exactly like a function! While the compiler is able to discern the two, it can be very confusing to read, and there's a large risk to use them inappropriately.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900