Calculating the Area and Perimeter

Hello everyone. I am having trouble with my C++ program. My program asks the user to enter any number greater than 0 and less than 20, and if the user enters any number(s) outside the acceptable range, the output should display:

input outside of exceptable range > 0 and < 20
length: 1 width: 1 area: 1 perimeter: 1

But in my program, this output displays no matter what the user types (even if the numbers are in the acceptable range, when it should be displaying the length and width the user enters and also displaying the calculated perimeter and area whenever the user enters number(s) in the acceptable range. Can someone please help? Here is my code:

#include <iostream>
using namespace std;

float class_rectangle_area();

float class_rectangle_perimeter();

float class_rectangle_length();

float class_rectangle_width();

int main() // Main function!

{

float length;

float width;

float area;

float perimeter;


cout << endl;

cout << "input length: ";

cin >> length;

cout << "input width number: ";

cin >> width;

cout << endl;

if (class_rectangle_length() && class_rectangle_width())
{
cout << "length: " << class_rectangle_length();

cout << " ";

cout << "width: " << class_rectangle_width();

cout << " "; // Space

cout << "area: " << class_rectangle_area();

cout << " ";

cout << "perimeter: " << class_rectangle_perimeter();
}

else
{
cout << "input outside of exceptable range > 0 and < 20" << endl;

cout << "length: 1";

cout << " ";

cout << "width: 1";

cout << " ";

cout << "area: 1";

cout << " ";

cout << "perimeter: 1";

cout << endl;
}

return 0;

}

float class_rectangle_area()

{
float area;

float length;

float width;

area = length * width;

return 0;
}

float class_rectangle_perimeter()

{
float perimeter;
float length;

float width;

perimeter = (length * 2) + (width * 2);

return 0;
}




float class_rectangle_length()
{
float length;

float value;

(length<20.0 && length>0.0);

return 0;

}

float class_rectangle_width()
{
float width;

float second_value;

(width<20 && width>0);

return 0;

}
Last edited on
The functions you are calling to try to calculate whether the values are within the acceptable range don't do anything useful; they always return 0, so they never change based on the input. Those functions themselves also seem to imply you don't really understand how functions work; you are declaring some uninitialized variables, calculating a boolean result that you immediately ignore, then unconditionally return 0.
Okay, so this time I tried this, but get get displayed from the prompt asking me the length and width, but it does not display the length, width, calculated area and perimeter. What to do now?

#include <iostream>
using namespace std;

float class_rectangle_area();

float class_rectangle_perimeter();

float class_rectangle_length();

float class_rectangle_width();

int main() // Main function!

{

float length;

float width;

float area;

float perimeter;


cout << endl;

cout << "input length: ";

cin >> length;

cout << "input width number: ";

cin >> width;

cout << endl;

if (class_rectangle_length() && class_rectangle_width())
{
cout << "length: " << class_rectangle_length();

cout << " ";

cout << "width: " << class_rectangle_width();

cout << " "; // Space

cout << "area: " << class_rectangle_area();

cout << " ";

cout << "perimeter: " << class_rectangle_perimeter();
}

else
{
cout << "input outside of exceptable range > 0 and < 20" << endl;

cout << "length: 1";

cout << " ";

cout << "width: 1";

cout << " ";

cout << "area: 1";

cout << " ";

cout << "perimeter: 1";

cout << endl;
}

return 0;

}

float class_rectangle_area()

{
float area;

float length;
cin >> length;
float width;
cin >> width;
area = length * width;

return area;
}

float class_rectangle_perimeter()

{
float perimeter;
float length;
cin >> length;
float width;
cin >> width;

perimeter = (length * 2) + (width * 2);

return perimeter;
}




float class_rectangle_length()
{
float length;
cin >> length;
float value;

value=(length<20.0 && length>0.0);

return value;

}

float class_rectangle_width()
{
float width;
cin >> width;
float second_value;

second_value=(width<20 && width>0);


return second_value;

}
The lengths and widths you have in each function may have the same names, but they are all different variables. I really think you need to re-read whatever material you have on functions, trying to see what they say on identifier scopes.

This might help:
http://www.cplusplus.com/doc/tutorial/namespaces/
I know they are different variables, that is why I declared them in each function and that is acceptable. I do not know what the problem is even after reading it. I could declare them globally, but my professor does not want us to do that yet.
Topic archived. No new replies allowed.