What am I doing wrong with this code??? #include <iostream> using namespace std;
ID: 666511 • Letter: W
Question
What am I doing wrong with this code???
#include <iostream>
using namespace std;
double getLength();
double getWidth();
double calcArea(double length, double width);
double calcPerimeter(double length, double width);
void displayData(double length, double width, double perimeter, double area);
int main()
{
double length = 1, width = 1, perimeter, area;
while (1)
{
if (length && width>0)
{
length = getLength();
width = getWidth();
area = calcArea(length, width);
perimeter = calcPerimeter(length, width);
displayData(length, width, perimeter, area);
cout << "valid, accept" << endl;
}
else if (length && width <= 0)
{
cout << "invalid value, re-enter value: " << endl;
}
}
}
double getLength()
{
double length;
cout << "Enter rectangle length: " << endl;
cin >> length;
return length;
}
double getWidth()
{
double width;
cout << "Enter rectangle width: " << endl;
cin >> width;
return width;
}
double calcArea(double length, double width)
{
double area;
area = length * width;
return area;
}
double calcPerimeter(double length, double width)
{
double perimeter;
perimeter = 2 * (length + width);
return perimeter;
}
void displayData(double length, double width, double perimeter, double area)
{
cout << "Rectangle: " << length << endl;
cout << "Width: " << width << endl;
cout << "Perimeter: " << perimeter << endl;
cout << "Area: " << area << endl;
system("PAUSE");
return;
}
Explanation / Answer
APART FROM ONE VERY MINOR CHANGE, IT SEEMS FINE. ALSO I RAN IT WITH 6 SETS OF DATA AND IT'S WORKING AS INTENDED. HERE IS THE CODE.
#include <iostream>
using namespace std;
double getLength();
double getWidth();
double calcArea(double length, double width);
double calcPerimeter(double length, double width);
void displayData(double length, double width, double perimeter, double area);
int main()
{
double length = 1, width = 1, perimeter, area;
while (1)
{
if (length && width>0)
{
length = getLength();
width = getWidth();
area = calcArea(length, width);
perimeter = calcPerimeter(length, width);
displayData(length, width, perimeter, area);
cout << "valid, accept" << endl;
}
else if (length && width <= 0)
{
cout << "invalid value, re-enter value: " << endl;
}
}
}
double getLength()
{
double length;
cout << "Enter rectangle length: " << endl;
cin >> length;
return length;
}
double getWidth()
{
double width;
cout << "Enter rectangle width: " << endl;
cin >> width;
return width;
}
double calcArea(double length, double width)
{
double area;
area = length * width;
return area;
}
double calcPerimeter(double length, double width)
{
double perimeter;
perimeter = 2 * (length + width);
return perimeter;
}
void displayData(double length, double width, double perimeter, double area)
{
cout << "Length: " << length << endl;
cout << "Width: " << width << endl;
cout << "Perimeter: " << perimeter << endl;
cout << "Area: " << area << endl;
system("PAUSE");
return;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.