1

The question states: 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); 

The following is the code I have at the moment but can't finish because of the errors.

#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);

    do {
        printf("Would you like to solve another\n"
               "exercise?\n"
               " Y or N ");
        scanf(" %c", &answer);
        printf("The area of the triangle is: %f", &areaTriangle(sideA, sideB, sideC));
    } while (char == 'Y');

    do {
        printf("Would you like to solve another\n"
               "exercise?\n"
               " Y or N ");
        scanf(" %c", &answer);
        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;
}

I believe I messed up in my function definition and the function call. Also, I am not sure whether I should have all the variables that I included.

The list of errors I am getting:

45:error: lvalue required as unary '&' operand
46 and 51:expected expression before 'char'
60:note declared here
64:error: lvalue required as left operand of assignment
68:error: expected expression before 'float'
69:warning: control reaches end of non-void function
chqrlie
  • 131,814
  • 10
  • 121
  • 189

1 Answers1

1

lvalue required as unary '&' operand

You shouldn't have & before the call to areaTriangle(). It returns a float, and that's what the argument corresponding to %f needs to be.

expected expression before 'char'

char is a type specifier. while (char == 'Y') should be comparing a variable, not a type, e.g. while (answer == 'Y')

lvalue required as left operand of assignment

You can't assign to the name of the function. You should declare a new variable:

float result = sqrt(sideVariable*(sideVariable - sideA)*(sideVariable - sideB)
                    *(sideVariable - sideC));

expected expression before 'float'

You shouldn't have the type declaration in the return statement. It should be return result;

You shouldn't be using do-while to test the response to the question, just use if. You can put a loop around the entire body to repeat it all.

When reading a single character with scanf(), you should put a space before %c to skip over any whitespace left in the input buffer. See The program doesn't stop on scanf("%c", &ch) line, why? for details.

Full revised code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

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

int main()
{
    while (1) {
        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("The area of the triangle is: %f", areaTriangle(sideA,sideB,sideC));

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

        if (toupper(answer) != 'Y') {
            printf("You have selected to quit out of the program.\n"
                   "Thank you for using the program.\n");
            break;
        }
    }

    return 0;
}

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

    sideVariable = (sideA + sideB + sideC) / 2;
    float result = sqrt(sideVariable*(sideVariable - sideA)*(sideVariable - sideB)
                        *(sideVariable - sideC));
    return result;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Wow thanks for all the corrections and the reason as to why I kept getting the error. The reason I am using a do-while statement to test the response is because I have to follow the method my professor is telling me to use with each exercise. In this particular exercise I have to incorporate the do-while in order to get the full credit. That is where i am still confused on what I should include inside the do-while. – StaySafein2020 Mar 15 '20 at 20:49
  • @StaySafein2020 — replace the `while (1)` in the answer with `do`. Put the `} while (toupper(answer) != 'N');` after the `scanf(" %c", &answer);`. You can print the "You have selected to quit …" message after the loop. – Jonathan Leffler Mar 15 '20 at 20:52
  • You might want to add a trailing newline in `"The area of the triangle is: %f"`. Also `upper(answer)` should be `toupper((unsigned char)answer)` – chqrlie Mar 15 '20 at 20:57
  • Thank you all for the help. I was finally able to correct the errors and finish and turn it in. I appreciate you guys answering so fast and being really helpful. – StaySafein2020 Mar 15 '20 at 21:11