Question:
C++ "parse error before '*' token" helpp!?
iamthemessenger
2010-04-19 12:59:54 UTC
i have a homework assignment, and this one function definition is keeping me from running my program. the compiler error just says: parse error before '*' token on line 171. i've tried multiple solutions and i can't seem to get it right. i thought maybe the wording of my formula is wrong? i know the answer is probably really obvious, but any help would be awesome.

double TriArea()
{
int base, h;
double area;

area = 1/2 base * h; //this is line 171

return area;
}
Five answers:
The Phlebob
2010-04-19 13:02:54 UTC
You need another * before base.



Hope that helps.



EDIT: Just noticed another bug there that would baffle you. You're dividing 1 by 2. In Integer arithmetic, this would result in 0. 0 times anything will still be zero, so area will always be 0.



Instead, use a decimal point in that 1/2 to force floating point arithmetic for the rest of the calculation. 1.0/2 would work, or 1/2.0, or even 0.5.
benpope81
2010-04-19 13:19:09 UTC
The syntax error is due to a missing multiplication operator between the "1/2" and the base. You also haven't initialised base, or h.



Try:



double TriArea(int height, int base)

{

return 0.5 * height * base;

}
Jazka
2010-04-19 23:18:50 UTC
Try writing it like this:



area = 1.0/2.0 * (base * height);



You have to write the 1/2 as a double/float otherwise it will treat the / sign like the % and you'll get zero.
Igsolus_
2010-04-19 13:11:15 UTC
1/2 base ? did you mean 1/2*base ? which is base/2 ?
?
2016-04-12 16:40:48 UTC
count your braces in your if elseif statements.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...