# Code works wrong at specific entered values

Question:
Why following code doesnt work right if you enter for example length a = 4.3, length b = 5.3 and height = 1.5. Right result of trapeze area is 7.2 meanwhile code doesnt recognize this as a right result
https://replit.com/@jackkozlovvski/CommonTestyParentheses?v=1

``````#include <iostream>

using namespace std;

int main()
{

float a, b, height, result;

cout << "Find trapeze area\n";
cout << "Enter lenght a\n";
cin >> a;
cout << "Enter lenght b\n";
cin >> b;
cout << "Enter height\n";
cin >> height;
cout << "Enter result \n";
cin >> result;
if (result == (a + b) * height/2 )
{
cout << "Well done!\n";
}
else
{
do
{
cout << "Fail, try again\n";
cin >> result;

} while ( result != (a + b) * height/2 );

cout << "Well done!\n";

}

}
``````

Hey @jackkozlovvski welcome to the forums!

Disclaimer - The Following Code Was Generated By ChatGPT An AI

The provided code is mostly correct, but it has a minor logical issue. The user is asked to input the `result` before checking whether it matches the trapezoid area formula. The correct approach would be to first calculate the trapezoid area using the given inputs and then compare it with the userâ€™s input. Hereâ€™s the fixed code:

``````#include <iostream>

using namespace std;

int main()
{
float a, b, height, result;

cout << "Find trapezoid area\n";
cout << "Enter length a\n";
cin >> a;
cout << "Enter length b\n";
cin >> b;
cout << "Enter height\n";
cin >> height;

// Calculate the expected result
float expected_result = (a + b) * height / 2;

cout << "Enter result\n";
cin >> result;

if (result == expected_result)
{
cout << "Well done!\n";
}
else
{
do
{
cout << "Fail, try again\n";
cin >> result;
} while (result != expected_result);

cout << "Well done!\n";
}

return 0;
}
``````

In this corrected code, the program first calculates the expected result based on the user inputs for `a`, `b`, and `height`. Then, it compares the userâ€™s input with the expected result. If they match, it outputs â€śWell done!â€ť immediately. If they donâ€™t match, it enters a loop where the user is prompted to try again until they enter the correct value, at which point it outputs â€śWell done!â€ť and exits the loop.

2 Likes

The problem is due to rounding errors which are inherent in floating-point arithmetic which mean that the computed result is slightly more than 7.2.
Search online for ways to approximately compare floating-point numbers that would suit your program

2 Likes

Thanks for your reply, but unfortunatelly fixed code still doesnt work at specific values which are used for my example.

but this code does work

``````#include <iostream>

using namespace std;

int main()
{
double a, b, height, result;

cout << "Find trapezoid area\n";
cout << "Enter length a\n";
cin >> a;
cout << "Enter length b\n";
cin >> b;
cout << "Enter height\n";
cin >> height;

// Calculate the expected result
float expected_result = (a + b) * height / 2;

cout << "Enter result\n";
cin >> result;

// Correct method to compare
// floating-point numbers
if (abs(result - expected_result) < 1e-6)
{

cout << "Well done!\n";
}
else
{
do
{
cout << "Fail, try again\n";
cin >> result;
} while (abs(result - expected_result) > 1e-6);

cout << "Well done!\n";
}

return (0);
}
``````

Thanks for you tip which helped me to solve problem

1 Like

nice, so using a 0.1 error interval. Note that this results in an implicit conversion to `double` so you may want to use `fabs` instead

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.