I am trying to brush up on C++, which I haven't used in 5 years. I am having problems with the following. Does anyone see the problem in this code? ("sqr(x) is defined in macro.h)
#include "stdafx.h"
#include "macro.h"
#include %26lt;iostream%26gt;
#include %26lt;cmath%26gt;
using namespace std;
int main()
{
float a, b, c, result;
char side;
cout%26lt;%26lt;"For which side are you trying to solve - a, b, or c? \n";
cin%26gt;%26gt;side;
if (side == 'a')
{
cout %26lt;%26lt; " b = ";
cin %26gt;%26gt; b;
cout %26lt;%26lt; " c = ";
cin %26gt;%26gt; c;
cout %26lt;%26lt; endl;
result = sqrt((sqr(c))-(sqr(b)));
cout %26lt;%26lt; "Side a = "%26lt;%26lt;result%26lt;%26lt;endl;
}
else if (side == 'b')
{
cout %26lt;%26lt; " a = ";
cin %26gt;%26gt; a;
cout %26lt;%26lt; " c = ";
cin %26gt;%26gt; c;
cout %26lt;%26lt; endl;
result = sqrt((sqr(c))-(sqr(a)));
cout %26lt;%26lt; "Side a = "%26lt;%26lt;result%26lt;%26lt;endl;
}
else
{
cout %26lt;%26lt; " a = ";
cin%26gt;%26gt;a;
cout %26lt;%26lt; " b = ";
cin%26gt;%26gt;b;
cout %26lt;%26lt; endl;
result = sqrt((sqr(a))+(sqr(b)));
cout %26lt;%26lt; "Side b = "%26lt;%26lt;result%26lt;%26lt;endl;
}
return 0
C++ problems?
Your parentheses are all messed up.
All three lines of result need to be fixed...or maybe just 2.
You don't need to define a method to do "sqr"
#include %26lt;iostream%26gt;
#include %26lt;cmath%26gt;
using namespace std;
int main()
{
float a, b, c, result;
char side;
cout%26lt;%26lt;"For which side are you trying to solve - a, b, or c? \n";
cin%26gt;%26gt;side;
if (side == 'a')
{
cout %26lt;%26lt; " b = ";
cin %26gt;%26gt; b;
cout %26lt;%26lt; " c = ";
cin %26gt;%26gt; c;
cout %26lt;%26lt; endl;
result = sqrt((c*c)-(b*b));
cout %26lt;%26lt; "Side a = "%26lt;%26lt;result%26lt;%26lt;endl;
}
else if (side == 'b')
{
cout %26lt;%26lt; " a = ";
cin %26gt;%26gt; a;
cout %26lt;%26lt; " c = ";
cin %26gt;%26gt; c;
cout %26lt;%26lt; endl;
result = sqrt((c*c)-(a*a));
cout %26lt;%26lt; "Side a = "%26lt;%26lt;result%26lt;%26lt;endl;
}
else
{
cout %26lt;%26lt; " a = ";
cin%26gt;%26gt;a;
cout %26lt;%26lt; " b = ";
cin%26gt;%26gt;b;
cout %26lt;%26lt; endl;
result = sqrt((a*a)+(b*b));
cout %26lt;%26lt; "Side b = "%26lt;%26lt;result%26lt;%26lt;endl;
}
return 0;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment