#### Archived

This topic is now archived and is closed to further replies.

# Need help in a little C++ program

This topic is 6781 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

##### Share on other sites
well, first off, you''ll need to change your coversion thingys in your printf and scanf statements for float values (%d to %f), and thats about all i can see because im not familiar with what you''re trying to do (too many things goin'' on )

also, you could change x = x + 1 (the VB way), to x += 1 (the C way). Doesn''t make it any faster, but if you have a huge variable like gv.stuffhere.morethings.more[index].x, you don''t have to type it twice That methiod applies to multiplying, subtracting, etc.

Sorry if I didn''t solve your problem like with the WHILE error or anything, but I''m just trying to help out all I can, anyway

##### Share on other sites
{
x = x + 1;
answer = ((length * width * x)+(((length * -2)+(width * -2)) * x * x)+(4 * x * x * x));
}

There is a potential problem in the logic.
For mathematical purposes, you can simplify the equation to 4x^3 as x increases towards infinity.
So as your x becomes bigger, the only part of the equation that only matters is 4x^3, and since x is always increasing, the while loop will go on forever.

##### Share on other sites
If you use floats, then first of all you''d have to use %f instead of %d (like the others said), but, more importantly, you can''t compare floats in the while loop!
It''s very simple: floats are no precise numbers (like int etc), they are approxiamtions of numbers. So, you''d either have to convert them to ints, or use a different algorithm.

Erik

##### Share on other sites
Also, you''re declaring old_answer to be an int, but ints can''t hold decimals. So, old_answer is probably being rounded up to 1 or down to 0. Change it to a float or double and that part will work.

- null_pointer
Sabre Multimedia

##### Share on other sites
Use double, floats tend to cause hard to find bugs

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632992
• Total Posts
3009755
• ### Who's Online (See full list)

There are no registered users currently online

×