Thread: calculate a rectangle's area

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    15

    calculate a rectangle's area

    I am writing a program to calculate a rectangle's area.

    Eg.
    Enter top left point: 1 1 (User input)
    Enter bottom right point: 2 -1 (User input)
    Top Left x = 1.000000 y: 1.000000
    Bottom Right x = 2.000000 y: -1.000000
    Area = 2.000000 (Program output)



    It keeps on prompting me my variable r is being used without being initialized, when I think I already did so.


    Code:
    typedef struct { 
    	double x; 
    	double y; 
    } Point; 
    
    
    typedef struct { 
    	Point topLeft; /* top left point of rectangle */ 
    	Point botRight; /* bottom right point of rectangle */ 
    } Rectangle; 
    
    
    Point p;
    Rectangle r;
    
    
    printf("Enter top left point : ");
    scanf("\n%f",r.topLeft);
    printf("\n%f",r.topLeft);
    
    
    printf("Enter bottom right point : ");
    scanf("\n%f", r.botRight);
    printf("\n%f",r.botRight);
    
    
    computeArea(&r);
    printf("\n%rectangle",r);
    
    
    
    double computeArea(Rectangle *r)
    { 
    	double h=0,w=0;
    
    
    	w=fabs(r->botRight.x - r->topLeft.x);
    	h=fabs(r->topLeft.y - r->botRight.y);
    
    
    	return (w*h);
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please post the complete error messages, exactly as they appear in your development environment.

    And again I think you should review the documentation for the scanf() function.

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    15
    It gives me this error message:

    calculate a rectangle's area-error-q6-png

    This scanf is to retrieve integer data the user keyed in. Other than scanf to retrieve what user keyed in, may I what else I can use? Eg. for retrieving char, we can use getchar(), what about for integer case?

  4. #4
    Registered User
    Join Date
    Mar 2014
    Posts
    5
    You need to pass a pointer to scanf, not the variable itself, then code compiles no problem for me (Code:Blocks on Win7). However, no matter what I input, printf always writes 0.00000. So it seems there's more than wrong.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Look closely at one of your scanf() calls.

    Code:
    scanf("\n%f",r.topLeft);
    There are several things wrong with this scanf(). First that extra character in your specifier string ( '\n' ) is going to cause problems, get rid of it.

    Next as I said earlier you need to find and read the documentation for scanf(). The format specifiers must exactly match the variable, your's don't match. What is the format specifier for a double?

    Next topLeft is a structure. What variable are you actually trying to get with this scanf()?

    Lastly you need to find and read the documentation for scanf(). This function expects an address of a variable. Are you doing that? Answer = NO!.

    Jim

  6. #6
    Registered User
    Join Date
    Mar 2014
    Posts
    14
    What Jim is saying at the end of his post is that your scanf is using r.topleft. Note that topleft is a structure inside another structure. Your scanf needs to point to an actual member/vaiable of said struct. (for example r.topleft.x). The compiler does not know which of the two members/variables of topleft to store the data into.

    But like people have already pointed out there are quite a few flaws in the code. Alot of them in your use of scanf.
    Last edited by Kotik; 03-15-2014 at 04:00 PM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > printf("\n%rectangle",r);
    Nor is %rectangle a format for printing with either.

    You need to use %f for each base member of the structure.

    > return (w*h);
    You return a result, but your call in main does nothing with it.
    Perhaps you want to store the result, and then print that result?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Mar 2014
    Posts
    15
    Do you know how to pass back using *r ? As using a direct printf in the function, the w*h is working and calculating it correctly, but when it is printed out in the main function, it seems that it is not tabulated and passed back the correct value of w*h. I need to return *r and print out *r. I tried by "return (*r) or return (&r), but it prompts me error. It stated "return : cannot convert from 'Rectangle' to 'double'.


    Code:
    printf("Enter top left point : ");
    scanf("%lf",&r.topLeft.x);
    scanf("%lf",&r.topLeft.y);
    fflush(stdin); 
    printf("Enter bottom right point : ");
    scanf("%lf",&r.botRight.x);
    scanf("%lf",&r.botRight.y);
    computeArea(&r);
    printf("Rectangle Area : %lf",r);
    
    
    double computeArea(Rectangle *r)
    { 
        double h=0,w=0,z=0;
        w=fabs(r->topLeft.x - r->botRight.x);
        printf("w : %lf\n",w);
        h=fabs(r->botRight.y - r->topLeft.y);
        printf("h : %lf\n",h);
        z=w*h;
        printf("w*h : %lf\n",z);
        return (z);
    }
    
    calculate a rectangle's area-q6-2-png
    Last edited by programming123; 03-16-2014 at 03:22 AM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It passes back the correct value.

    The problem is, you do nothing with it.

    Here
    area = computeArea(&r);

    Now declare area as a suitable variable, and then print area.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rectangle
    By juice in forum C Programming
    Replies: 10
    Last Post: 10-29-2011, 10:46 PM
  2. Tab control: how to calculate a tab display area???
    By SundayDeveloper in forum Windows Programming
    Replies: 4
    Last Post: 09-06-2007, 03:19 PM
  3. Calculate Point in Area
    By ilmarculin in forum C Programming
    Replies: 16
    Last Post: 04-07-2005, 07:03 AM
  4. Total Surface Area of a Rectangle (Classes help)
    By TrazPFloyd in forum C++ Programming
    Replies: 3
    Last Post: 09-06-2002, 10:58 AM
  5. Help With Rectangle,
    By incognito in forum Windows Programming
    Replies: 3
    Last Post: 03-30-2002, 12:43 PM

Tags for this Thread