Question

I wrote a program which computes the area and perimeter of a square, circle, or rectangle. As you will see in my main function, there is a for loop in which the user is supposed to be able repeat the...

I wrote a program which computes the area and perimeter of a square, circle, or rectangle. As you will see in my main function, there is a for loop in which the user is supposed to be able repeat the program until they enter "q" to quit. However, my program runs through one time, the prompt appears again, but then it terminates before the user is allowed to respond to the prompt again. I'm not able to debug why this is happening,

/*This program computes the area and perimeter of a square, rectangle, or circle*/

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

#define PI 3.14159

//Struct types defining components needed to represent each shape
typedef struct {
   double area, circumference, radius;
} circle_t;

typedef struct {
   double area, perimeter, width, height;
} rectangle_t;

typedef struct {
   double area, perimeter, side;
} square_t;


//Union type that can be interpreted in a different way for each shape
typedef union {
   circle_t circle;
   rectangle_t rectangle;
   square_t square;
} figure_data_t;


//Struct type containing union: select the figure you want, and the necessary components of that figure
typedef struct {
   char shape;
   figure_data_t fig;
} figure_t;

//Function declarations
figure_t get_figure_dimensions(void);
figure_t compute_area(figure_t object);
figure_t compute_perim(figure_t object);
void print_figure(figure_t object);

//Main Function
int main(int argc, char* argv[])
{
   figure_t onefig;

   printf("Area and Perimeter Computation Program\n");

   for (onefig = get_figure_dimensions();
       onefig.shape != 'Q';
       onefig = get_figure_dimensions())
   {
       onefig = compute_area(onefig);
       onefig = compute_perim(onefig);
       print_figure(onefig);
   }

   return 0;
}

//////////////////

//Function Definitions//
/*Prompts for and stores the dimension data necessary to compute a figure's area and perimeter.
Figure returned contains a "Q" in the shape component when signaling end of data.*/
figure_t get_figure_dimensions(void)
{
   figure_t object;
   printf("Enter a letter to indicate the object shape or Q to quit.\n");
   printf("C (circle), R (rectangle), or S (square): ");
   object.shape = getchar();

   switch (object.shape)
   {
   case 'C':
   case 'c':
       printf("Enter radius: ");
       scanf("%lf", &object.fig.circle.radius);
       break;
   case 'R':
   case 'r':
       printf("Enter Height: ");
       scanf("%lf", &object.fig.rectangle.height);
       printf("Enter width: ");
       scanf("%lf", &object.fig.rectangle.width);
       break;
   case 'S':
   case 's':
       printf("Enter length of a side: ");
       scanf("%lf", &object.fig.square.side);
       break;
   default: //Error is treated as a QUIT
       object.shape = 'Q';
   }
   return object;
}


/*Computes the area of a figure given relevant dimensions. Returns figure with area component filled.
Pre: value of shape component is one of these letters: CcRrSs
   necessary dimension components have values*/
figure_t compute_area(figure_t object)
{
   switch (object.shape)
   {
   case 'C':
   case 'c':
       object.fig.circle.area = PI * pow(object.fig.circle.radius, 2);
       break;
   case 'R':
   case 'r':
       object.fig.circle.area = object.fig.rectangle.height * object.fig.rectangle.width;
       break;
   case 'S':
   case 's':
       object.fig.square.area = pow(object.fig.square.side, 2);
       break;
   default:
       printf("Error in shape code detected in compute_area\n");
   }
   return object;
}


//The code for the functions below is an assignment
figure_t compute_perim(figure_t object)
{
   switch (object.shape)
   {
   case 'C':
   case 'c':
       object.fig.circle.circumference = 2 * PI * object.fig.circle.radius;
       break;
   case 'R':
   case 'r':
       object.fig.rectangle.perimeter = (2 *object.fig.rectangle.height) + (2 * object.fig.rectangle.width);
       break;
   case 'S':
   case 's':
       object.fig.square.perimeter = 4 * object.fig.square.side;
       break;
   default:
       printf("Error in shape code detected in compute_perim\n");
   }
   return object;
}
void print_figure(figure_t object)
{
   switch (object.shape)
   {
   case 'C':
   case 'c':
       printf("For a circle with a radius of %.2f, the area is %.2f and the circumference is %.2f\n\n",
           object.fig.circle.radius, object.fig.circle.area, object.fig.circle.circumference);
       break;
   case 'R':
   case 'r':
       printf("For a rectangle with a width of %.2f and a height of %.2f,\n",
           object.fig.rectangle.width, object.fig.rectangle.height);
       printf("the area is %.2f and the perimeter is %.2f\n\n",
           object.fig.rectangle.area, object.fig.rectangle.perimeter);
       break;
   case 'S':
   case 's':
       printf("For a square with a side length of %.2f, the area is %.2f and the perimeter is %.2f\n\n",
           object.fig.square.side, object.fig.square.area, object.fig.square.perimeter);
       break;
   default:
       printf("Error in shape code detected in print_figure\n\n");
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question.


I made 3 changes and its working as expected. ! Attached screenshot. Changes are commented in the code.


Thank You !!
========================================================================================

/*This program computes the area and perimeter of a square, rectangle, or circle*/

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

#define PI 3.14159

//Struct types defining components needed to represent each shape
typedef struct {
double area, circumference, radius;
} circle_t;

typedef struct {
double area, perimeter, width, height;
} rectangle_t;

typedef struct {
double area, perimeter, side;
} square_t;


//Union type that can be interpreted in a different way for each shape
typedef union {
circle_t circle;
rectangle_t rectangle;
square_t square;
} figure_data_t;


//Struct type containing union: select the figure you want, and the necessary components of that figure
typedef struct {
char shape;
figure_data_t fig;
} figure_t;

//Function declarations
figure_t get_figure_dimensions(void);
figure_t compute_area(figure_t object);
figure_t compute_perim(figure_t object);
void print_figure(figure_t object);

//Main Function
int main(int argc, char* argv[])
{
figure_t onefig;

printf("Area and Perimeter Computation Program\n");
   while(1){ // updated this line
       onefig = get_figure_dimensions();
       if(onefig.shape == 'Q')break;   // updated this line
onefig = compute_area(onefig);
onefig = compute_perim(onefig);
print_figure(onefig);
}

return 0;
}

//////////////////

//Function Definitions//
/*Prompts for and stores the dimension data necessary to compute a figure's area and perimeter.
Figure returned contains a "Q" in the shape component when signaling end of data.*/
figure_t get_figure_dimensions(void)
{
figure_t object;
printf("Enter a letter to indicate the object shape or Q to quit.\n");
printf("C (circle), R (rectangle), or S (square): ");
scanf(" %c",&object.shape); // updated this line

switch (object.shape)
{
case 'C':
case 'c':
printf("Enter radius: ");
scanf("%lf", &object.fig.circle.radius);
break;
case 'R':
case 'r':
printf("Enter Height: ");
scanf("%lf", &object.fig.rectangle.height);
printf("Enter width: ");
scanf("%lf", &object.fig.rectangle.width);
break;
case 'S':
case 's':
printf("Enter length of a side: ");
scanf("%lf", &object.fig.square.side);
break;
default: //Error is treated as a QUIT
object.shape = 'Q';
}
return object;
}


/*Computes the area of a figure given relevant dimensions. Returns figure with area component filled.
Pre: value of shape component is one of these letters: CcRrSs
necessary dimension components have values*/
figure_t compute_area(figure_t object)
{
switch (object.shape)
{
case 'C':
case 'c':
object.fig.circle.area = PI * pow(object.fig.circle.radius, 2);
break;
case 'R':
case 'r':
object.fig.circle.area = object.fig.rectangle.height * object.fig.rectangle.width;
break;
case 'S':
case 's':
object.fig.square.area = pow(object.fig.square.side, 2);
break;
default:
printf("Error in shape code detected in compute_area\n");
}
return object;
}


//The code for the functions below is an assignment
figure_t compute_perim(figure_t object)
{
switch (object.shape)
{
case 'C':
case 'c':
object.fig.circle.circumference = 2 * PI * object.fig.circle.radius;
break;
case 'R':
case 'r':
object.fig.rectangle.perimeter = (2 *object.fig.rectangle.height) + (2 * object.fig.rectangle.width);
break;
case 'S':
case 's':
object.fig.square.perimeter = 4 * object.fig.square.side;
break;
default:
printf("Error in shape code detected in compute_perim\n");
}
return object;
}
void print_figure(figure_t object)
{
switch (object.shape)
{
case 'C':
case 'c':
printf("For a circle with a radius of %.2f, the area is %.2f and the circumference is %.2f\n\n",
object.fig.circle.radius, object.fig.circle.area, object.fig.circle.circumference);
break;
case 'R':
case 'r':
printf("For a rectangle with a width of %.2f and a height of %.2f,\n",
object.fig.rectangle.width, object.fig.rectangle.height);
printf("the area is %.2f and the perimeter is %.2f\n\n",
object.fig.rectangle.area, object.fig.rectangle.perimeter);
break;
case 'S':
case 's':
printf("For a square with a side length of %.2f, the area is %.2f and the perimeter is %.2f\n\n",
object.fig.square.side, object.fig.square.area, object.fig.square.perimeter);
break;
default:
printf("Error in shape code detected in print_figure\n\n");
}
}

========================================================================================

Shape.c figure t object; printf(Enter a letter to indicate the object shape or Q to quit.In) printf(C (circle), R (rectangl

Add a comment
Know the answer?
Add Answer to:
I wrote a program which computes the area and perimeter of a square, circle, or rectangle. As you will see in my main function, there is a for loop in which the user is supposed to be able repeat the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Why when i enter A, and the second scanf founction is not work, there are no...

    Why when i enter A, and the second scanf founction is not work, there are no value for cis. int main() char in, Cls; int r-0; printf("A: Circle nB: Square\nC: Triangle\n"); printf("Enter Which Shape to use: \n"); scanf("%c", &in); switch(in) [ case A' printf("Enter P for solve for perimeter and A for solve for Area\n"); printf("Choice: "); scanf(%c", &cis); / /cis is choice printf("Enter circle radius: "); scanf("%lf", &r); printf("Circle perimeterr is:", perimeter('A', 2, 0, e)); return 0;

  • I'm having trouble getting my program to print shapes For example: Which shape (L-line, T-triangle, R-rectangle):...

    I'm having trouble getting my program to print shapes For example: Which shape (L-line, T-triangle, R-rectangle): L Enter an integer length between 1 and 25: 13 ************* Which shape (L-line, T-triangle, R-rectangle): t Enter an integer base length between 3 and 25: 5 * ** *** **** ***** Which shape (L-line, T-triangle, R-rectangle): r Enter an integer width and height between 2 and 25: 4 5 **** **** **** **** **** Here's my code: #include <stdio.h> #include <ctype.h> const int...

  • Create another program that will prompt a user for input file. The user will choose from...

    Create another program that will prompt a user for input file. The user will choose from 4 choices: 1. Display all positive balance accounts. 2. Display all negative balance accounts. 3. Display all zero balance accounts. 4. End program. C PROGRAMMING my code so far #include<stdlib.h> #include<stdio.h> int main(void) { int request; int account; FILE *rptr; char name[20]; double balance; FILE *wptr;    int accountNumber;    if((wptr = fopen("clients.dat","w")) == NULL) { puts("File could not be opened"); } else {...

  • in C++ use Inheritance for the following Shape Circle Sphere Cylinder Rectangle Square Cube You must define one class for each shape. And, use public inheritance when deriving from base cla...

    in C++ use Inheritance for the following Shape Circle Sphere Cylinder Rectangle Square Cube You must define one class for each shape. And, use public inheritance when deriving from base classes. Circle int r; void area(); void perimeter(); void volume(); //No volume for circle Sphere int r; void area();//Sphere surface area= 4 × pi × radius2 void perimeter(); //No perimeter for Sphere void volume();//Sphere volume= 4/3 × pi × radius2 Cylinder int r, height; void area();// Cylinder surface area=perimeter of...

  • So I have a question in regards to my program. I'm learning to program in C...

    So I have a question in regards to my program. I'm learning to program in C and I was curious about the use of functions. We don't get into them in this class but I wanted to see how they work. Here is my program and I would like to create a function for each of my menu options. I created a function to display and read the menu and the option that is entered, but I would like to...

  • Write a C program that does the following: Displays a menu (similar to what you see...

    Write a C program that does the following: Displays a menu (similar to what you see in a Bank ATM machine) that prompts the user to enter a single character S or D or Q and then prints a shape of Square,Diamond (with selected height and selected symbol), or Quits if user entered Q.Apart from these 2 other shapes, add a new shape of your choice for any related character. Program then prompts the user to enter a number and...

  • Create a program that calculates the area of various shapes. Console Specifications Create an abstract class...

    Create a program that calculates the area of various shapes. Console Specifications Create an abstract class named Shape. This class should contain virtual member function named get_area() that returns a double type. Create a class named Circle that inherits the Shape class and contains these constructors and member functions: Circle(double radius) double get_radius() void set_radius(double radius) double get_area() Create a class named Square that inherits the Shape class and contains these constructors and member functions: Square(double width) double get_width() void...

  • Hardware Inventory – Write a database to keep track of tools, their cost and number. Your...

    Hardware Inventory – Write a database to keep track of tools, their cost and number. Your program should initialize hardware.dat to 100 empty records, let the user input a record number, tool name, cost and number of that tool. Your program should let you delete and edit records in the database. The next run of the program must start with the data from the last session. This is my program so far, when I run the program I keep getting...

  • I am trying to write a Geometry.java program but Dr.Java is giving me errors and I...

    I am trying to write a Geometry.java program but Dr.Java is giving me errors and I dont know what I am doing wrong. import java.util.Scanner; /** This program demonstrates static methods */ public class Geometry { public static void main(String[] args) { int choice; // The user's choice double value = 0; // The method's return value char letter; // The user's Y or N decision double radius; // The radius of the circle double length; // The length of...

  • #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here....

    #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } }    return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT