Can someone help me on my hw pls!

0 Edgar DuranCoronado · February 19, 2015
So im currently working on a homework assignment and the basis is to
"Design and write a program, using functions, that calculates the area and perimeter of a rectangle whose dimensions (length and width) are provided by a user."

i created a program that allows me to do just that however, when i try to put the inputs for the length or width the inputs will not register . (pic  & code attached)

any help would be greatly appreciated! 

/images/forum/upload/2015-02-19/bf99d590d5e90498e90eaf4477b9aea2.jpg

#include 

float areaofrec( float length, float width);
float periofrec(float length, float width);

int main ()
{
  /* variable definition: */
  int menuSelect, intValue;
  float length, width;
  float result = 0;

   // While a positive number
 while (intValue > 0)
  {
     printf ("Enter a positive Integer to calculate the area or perimeter; \n ");
     printf ("OR a negative Integer to exit the program.: \n");
     scanf(" %d", &intValue);

    if (intValue > 0)
    {
     printf ("Enter 1 to calculate area, 2 to Calculate the perimeter: \n ");
     scanf(" %d", &menuSelect);

     if (menuSelect == 1)
     {
        // Call the area function
        printf("You chose to calculate the area of the rectangle! \n");
        printf("\nPlease enter the length of the rectangle: \n ");
        scanf(" %f ", &length);
        printf("\nPlease enter the width of the rectangle: \n");
        scanf(" %f", &width);
        result = areaofrec(length, width);
        printf("The area of the rectangle is %.0f \n", &result);
     }

     else if (menuSelect == 2)
     {
        // Call the perimeter function
        printf("You chose to calculate the perimeter of the rectangle! \n");
        printf("\nPlease enter the length of the rectangle");
        scanf(" %f ", &length);
        printf("\nPlease enter the width of the rectangle:");
        scanf(" %f", &width);
        result = periofrec(length, width);
        printf("The area of the rectangle is %.0f \n", &result);
     }

     else
       printf("Invalid menu item, only 1 or 2 is accepted\n");
     }


    }

  return 0;
}

/* function returning the area of a number */
float areaofrec( float length,float width)
{
    return length*width;
}

/* function returning the perimeter of a number */
float periofrec( float length, float width)
{
    return (length*2)+(width*2);
}


Post a Reply

Replies

Oldest  Newest  Rating
+1 Edgar DuranCoronado · February 23, 2015
The reason i was running a while loop was in the case that the user was wanting to calculate more than one. Also, the reason i didnt use ints was because it needs a user input. im not familiar enough with longs to use them yet too. 

Rafidul, that piece of code looks great! its simply and can only just a few lines of codes. Ill keep your source code as reference for future programs. however, the issue is in the program im supposed to calculate the area and perimeter as separate functions

The issue with the code I originally provided was that i used &result in the code instead of just result and because it was using the functions to receive the answer, the & was screwing it all up. :D 
0 Dol Lod · February 19, 2015
I'm sorry but I just have to ask. Why are using floats instead of ints,longs or doubles? 

To begin with you're running a while loop on an uninitialized integer. I'm surprised it even compiles. To address this issue, I would initialize intValue to some number greater than 0 based on the current while condition. Additionally, you should find a way to terminate the program or it will remain in an infinite loop. 

For the scanf, I'm not sure what exactly is going on. However, you might want to use fgets as mentioned in http://stackoverflow.com/questions/14565956/how-to-get-newline-character-from-scanf-even-if-its-the-only-input .
0 Rafidul Islam · February 19, 2015
Why not try something like this? 

http://pastebin.com/0VmRgLRj
  • 1

C

107,229 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator