Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Problem with c code, trying to find area of a rectangle?

Here's what I've got..

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

double getrectanglearea(double h, double w)

{

return h*w;

}

double getrectangleperimeter(double h, double w)

{

return 2.0*h+2.0*w;

}

int main(void)

{

double perimeter=0.0, area=0.0;

double h=5.0;

double w=5.0;

area = getrectanglearea(h, w);

perimeter = getrectangleperimeter(h, w);

printf("Rectangle with width %lf and height %lf: area %lf perimeter %lf\n", w, h, area, perimeter);

return EXIT_SUCCESS

}

It won't print anything. Not sure what's up.

3 Answers

Relevance
  • ?
    Lv 6
    9 years ago

    Looks okay to me, except that you have missed a semicolon on:

    return EXIT_SUCCESS; // Now it works fine.

    Make sure everything compiles.

    EDIT:

    Actually, a better method over system("pause"); is to use getchar();

    the system("pause") method is only valid for windows systems, and nothing else.

    It's actually a poor choice to keep a console window open. Usually any system()

    command is a poor choice, unless you are a low-level developer, there are often

    better, more useful ways of doing it. Some low level stuff like kernels must use system commands though which make direct calls to the OS.

    EDIT2:

    The format specifier is actually correct. The C-language specifies that you use %lf for doubles and %f for floats. Although in printf you can get away with using %f, because the float in printf will get promoted to a double in the method of variable argument functions. Still providing a %lf states that you are using a double and not a float. Just a little heads up.

    http://stackoverflow.com/questions/210590/why-does...

  • Anonymous
    9 years ago

    This question get asked a lot when they first use visual c++ ide alot. If your program compiled properly, still the program might run and disappear before you get to see the results. You might need to use system("pause") to pause the program at the end of your program.

  • Anonymous
    9 years ago

    - You're using the wrong format specifier for printf; %f is used for doubles.

    - Your third return statement isn't terminated with a semicolon.

    - The ``math.h'' header file isn't used by your program.

Still have questions? Get your answers by asking now.