I've spent upwards of 12 hours straight trying to figure out what I'm doing wrong and have been working on this program on and off for about a week now. Assignment is due early tomorrow morning, any help at all would be appreciated, I'm really desperate to pass.
When debugging, I found that the program breaks when I scan for data for p_rectangle->length/width, I'm sure it has something to do with how I set up main() or memory allocation, but I just can't figure it out.
Thanks guys.
//**********************************************************************
//* *
//* This program calculates and prints the area and perimeter of a *
//* rectangle and then draws a representation of the proportionality *
//* between length and width as a rectangle with a minimum length *
//* and width of 1. *
//* *
//**********************************************************************
#include <iostream>
using namespace std;
//**********************************************************************
//* Symbolic Constants *
//**********************************************************************
#define COURSE_NAME "Object-Oriented Programming and Design"
// Assigned course name
#define COURSE_NUMBER "CS246" // PCC assigned course number
#define MINIMUM_RECTANGLE_LENGTH 1.0 // Program preset range maximum
#define MINIMUM_RECTANGE_WIDTH 1.0 // Program preset range minimum
#define PROGRAM_NUMBER "1" // Teacher assigned program number
#define RECTANGLE_BORDER "*" // Rectangle border character
#define RECTANGLE_CENTER " " // Rectangle center character
//**********************************************************************
//* Program Structures *
//**********************************************************************
// Measurements specifications of a rectangle
struct rectangle
{
float area,
length,
perimeter,
width;
};
//**********************************************************************
//* Function Prototypes *
//**********************************************************************
void print_heading ();
// Print the program heading
void get_dimensions(struct rectangle *p_rectangle);
// Get the rectangle length and heigth and calculate the area and perimeter
void draw_rectangle(float length, float width);
// Draw a picture of a rectangle
void print_rectangle_specifications(struct rectangle *p_rectangle);
// Print the area, perimeter, width, and length
//**********************************************************************
//* Main Function *
//**********************************************************************
int main()
{
struct rectangle *p_rectangle; // Points to measurements specifications of a rectangle
// Print the program heading
print_heading ();
// Get the rectangle length and height and calculate the area and perimeter
get_dimensions(p_rectangle);
// Draw a picture of a rectangle
draw_rectangle(p_rectangle->length, p_rectangle->width);
// Print the area, perimeter, width, and length
print_rectangle_specifications(p_rectangle);
// Say goodbye and terminate the program
cout << "\n\nThanks for drawing a rectangle today ;)";
cout << "\n\n\n\n\n\n";
free(p_rectangle);
return 0;
}
//**********************************************************************
//* Print the program heading *
//**********************************************************************
void print_heading()
{
cout << "\n\n\n\n\n\n" << COURSE_NUMBER
<< " " << COURSE_NAME
<< " - Program " << PROGRAM_NUMBER;
cout << "\n\n This program draws a rectangle";
cout << "\n ==============================";
return;
}
//**********************************************************************
//* Get the length and width of your rectangle *
//**********************************************************************
void get_dimensions(struct rectangle *p_rectangle)
{
cout << "\n\nEnter the rectangle length now: ";
cin >> p_rectangle->length;
cout << "\nEnter the rectangle width here: ";
cin >> p_rectangle->width;
p_rectangle->area = (p_rectangle->length * p_rectangle->width);
p_rectangle->perimeter = (p_rectangle->length + p_rectangle->width) * 2;
return;
}
//**********************************************************************
//* Draw a picture of the rectangle *
//**********************************************************************
void draw_rectangle(float length, float width)
{
int columns,
i_length = (int)length,
i_width = (int)width,
rows;
// Checks if rectangle length and width is less than the minimum length
// and width
if ((i_length < MINIMUM_RECTANGLE_LENGTH) && (i_width < MINIMUM_RECTANGE_WIDTH))
{
cout << "\n\n\nA " << i_length << " by " << i_width;
cout << " is too small to draw.";
cout << "\nIt must be at least size " << MINIMUM_RECTANGLE_LENGTH;
cout << " by " << MINIMUM_RECTANGLE_LENGTH << " (length by width).";
}
else
{
// Scales the length and width of the rectangle on a 1-to-1 scale
columns = i_length * 1.5,
rows = i_width;
// Prints the rectangle message
cout << "\n\nHere is a picture of your rectangle";
// Prints the top row of the rectangle
for (int top_row = 1; top_row <= columns; top_row++)
cout << RECTANGLE_BORDER;
// Prints all middle rows of the rectangle
for (int middle_row = 1; middle_row <= columns - 2; middle_row++)
cout << "\n" << RECTANGLE_BORDER;``
for (int middle_column = 1; middle_column <= rows; middle_column++)
{
cout << RECTANGLE_CENTER;
if (rows == middle_column)
cout << RECTANGLE_BORDER;
}
// Prints the bottom row of the rectangle
if (i_width > 1)
for (int bottom_row = 1; bottom_row <= columns; bottom_row++)
cout << RECTANGLE_BORDER;
}
return;
}
//**********************************************************************
//* Print the calculations of the rectangle *
//**********************************************************************
void print_rectangle_specifications(struct rectangle *p_rectangle)
{
cout << "\n\nThe rectangle specifications are:";
cout << "\n Length = " << p_rectangle->length << " units";
cout << "\n Width = " << p_rectangle->width << " units";
cout << "\n Area = " << p_rectangle->area << " sq. units";
cout << "\n Perimeter = " << p_rectangle->perimeter << " units";
return;
}