I've just started learning how to program C++ about a day or two ago, and made a simple pythagorean theorem solver.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <math.h> // C math library
usingnamespace std;
int main ()
{
float a, b, result;
while ((a > 0) & (b > 0));
{
cout << "\n Please input side the two side lengths";
cin >> a >> b;
result = sqrt((a*a)+(b*b));
cout << "\n The hypotenuse length is: " << (result);
}
return 0;
}
I compiled it and the program ran, but nothing appeared on the screen except a flashy underscore indicating it's waiting for an input. When I tried entering a value, nothing appeared on the screen. Does anyone know why this happened?
Any help would be greatly appreciated!
I can't explain the behavior of your console, but I do know what's wrong.
1. ; on line 8. that means a empty loop.
2. you compare a and b to 0 before they were set. Behavior is not defined here. a do while loop would work better here.
little problems:
3. there is a difference between & and &&. & is a bitwise operator, meaning that it does to every pair of bits of two numbers what && does to two booleans. In this case the result is the same, but just so that you know..
4. you don't need so many parentheses. while(a > 0 && b > 0), sqrt(a*a+b*b), cout << result are all fine.