-4

I am a student trying to make a simple program to get the area of a triangle, square, rectangle, and circle using functions using C language. Unfortunately, my program won't run even though I double checked the syntax and debugged. Listed below is my code for reference:

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

float triangle(float b, float h)
{
    float triangle;
    triangle=0.5*b*h;
    return triangle;
}

float square(float s)
{
    float square;
    square=s*s;
    return square;
}

float rectangle(float l, float w)
{
    float rectangle;
    rectangle=l*w;
    return rectangle;
}

float circle(float r)
{
    float circle;
    circle=3.14*r*r;
    return circle;
}

main()
{
    float base, height, side, length, width, radius;
    
    printf("Triangle Area:\nEnter Base and Height:");
    scanf("%f%f", &base, &height);
    printf("Area of the Triangle= %f", triangle(base, height));
    
    printf("\nSquare Area:\nEnter Side:");
    scanf("%f", &side);
    printf("Area of the Square= %f", square(side));
        
    printf("\nRectangle Area:\nEnter Length and Width:");
    scanf("%f%f", &length, &width);
    printf("Area of the Rectangle= %f", rectangle(length, width));
    
    printf("\nCircle Area:\nEnter Radius:");
    scanf("%f", &radius);
    printf("Area of the Circle= %f", circle(radius));
    
return 0;
}

Edit: Sorry for not getting back to you guys on time. I followed one of your suggestions to put int main(void){} instead of just main(){}.

I believe there is a problem with my compiler because when I try to run the program it would just show the compilation results and that there is nothing wrong with my syntax. I decided to not to input 355.0 / 113.0 to replace 3.14 for pi. I am using WindowsOS platform for as requested.

I cannot attach a photo because I do not have enough reputation but this is what appears in the compiler:

Compilation results...

  • Errors: 0
  • Warnings: 0
  • Output Filename: (Personal Information)
  • Output Size: 157.9794921875 KiB
  • Compilation Time: 0.73s

    Should I change something in the code or try something else? Thank you!
  • Your code looks OK. Please show an example of input and expected vs. actual output. – Jabberwocky Mar 29 '21 at 04:23
  • 1
    What exactly do you mean by "won't run"? In other news, you have to write `int main`; if your book doesn't show that syntax, it's too old. – n. m. could be an AI Mar 29 '21 at 04:23
  • What is your platform? How do you compile? Are there any compiler errors? How do you run the program? When you try tu run the program do you get any error messages? Please [edit] and clarify. – Jabberwocky Mar 29 '21 at 04:26
  • Runs fine for me. Like Jabberwocky said, you'll have to show us the inputs you tried that didn't work for us to help. FYI, you will have issues if you enter anything that `scanf` can't parse as a float (e.g. "hello"). You should check the return value of `scanf` to see if it got valid input and do something to handle when it doesn't. – Outrageous Bacon Mar 29 '21 at 04:27
  • 1
    Use `355.0 / 113.0` as a far better (7-digit) approximation to π. You should end outputs (other than prompts) with a newline, rather than starting outputs with a newline. – Jonathan Leffler Mar 29 '21 at 04:45
  • How do you __run__ the program? From your description I suspect you just compile the program but you never even tried to __run__ it. – Jabberwocky Mar 29 '21 at 08:22

2 Answers2

1

You just need to put int (return type) before main() as part of the function definition. The rest seems to be good

int main(void)
{
    float base, height, side, length, width, radius;
    
    printf("Triangle Area:\nEnter Base and Height:");
    scanf("%f%f", &base, &height);
    printf("Area of the Triangle= %f", triangle(base, height));
    
    printf("\nSquare Area:\nEnter Side:");
    scanf("%f", &side);
    printf("Area of the Square= %f", square(side));
        
    printf("\nRectangle Area:\nEnter Length and Width:");
    scanf("%f%f", &length, &width);
    printf("Area of the Rectangle= %f", rectangle(length, width));
    
    printf("\nCircle Area:\nEnter Radius:");
    scanf("%f", &radius);
    printf("Area of the Circle= %f", circle(radius));
    
    return 0;
}
Antonin GAVREL
  • 9,682
  • 8
  • 54
  • 81
tax evader
  • 2,082
  • 1
  • 7
  • 9
0

You need to just add a type to main function. Ex: int main(void) { … }.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140