DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Registered User
Join Date
Jan 2007
Posts
9

C++ Help

Hello, im pretty new to C++ but i know a bit about it.
I have a formula that i want to turn into c++ code, What the formula does is outputs a lvl and its amount of experience.

The formula is:

This is my attempt at it.
Code:
```
#include <iostream>
#include <math.h>
using namespace std;

void wait()
{
cout << "\nPress Enter to Continue\n";

cin.ignore(1);
}

int main()
{
int xp = 83;

for ( float lvl = 2; lvl <= 99; lvl++)
{
xp += floor( floor(lvl + 300 * pow(2, lvl / 7 )) / 4);
cout << "Level: " << lvl << "\tXP: " << xp << endl;
}

wait();

return 0;
}```
The probblem is that it doesnt output the xp correctly.
lvl 2 outputs xp = 174
lvl 3 outputs xp = 275, when its suppose to output 276.

This formula ends at level 99 and when its reachers that level, the output is off by 39.

lvl 99 outputs xp = 13034394, when its suppose to output 13,034,431

So im wondering if anyone could help me out with this. Would be great to get it working properly.

The following link has what its suppose to look like, also has the formula in some other languages http://www.mirwoj.opus.chelm.pl/rs/index.php?pid=b04

Minor59er
Last edited by minor59er; 01-14-2007 at 09:44 AM.

2. first using [ IMG ] code will be better :

now following the formula u must :
1- start by 1; for(lvl=1 ..
2- u must calculate the summation first in a variable temp say [not xp], in the loop don't divide by 4 but after the loop or in the display result //u may not initialize xp by 83 but 0 .
3- the first floor is around the whole summation ..
try this :

Code:
```int main()
{
int xp = 0;

for ( float lvl = 1; lvl <= 99; lvl++)
{
xp += floor(  lvl + 300 * pow(2, lvl / 7 ));
cout << "Level: " << lvl << "\tXP: " <<floor( xp /4 )<< endl;
}

system("PAUSE");

return 0;
}```
Last edited by Amahdy; 01-14-2007 at 03:29 PM.

3. Registered User
Join Date
Dec 2006
Posts
13
Amahdy is right (he's very strong in math). You were making a sum of the whole formula, but if you look the formula closely, the sum regards only the upper part of the fraction.

4. Registered User
Join Date
Jan 2007
Posts
9
Thanks very much for your help, i tried it out on my graphics calc, and it worked fine, just got to get use to getting things to work in C++, thanks so much again.

Will

5. Registered User
Join Date
Jan 2007
Posts
9
Ok, one more question, when its level 74+, the numbers start to go 1.09628e+006, 1.21042e+006, 1.33644e+006, 1.47558e+006. Any idead to make them look like 1,096,278, 1,210,421, 1,336,443?

Thanks again :)

6. thanks mikkus I'm nothing near u , and happy to see u again :)

well u need the setiosflags(fixed) and because the return from floor() is double u may need to setprecision(0) .. all this could be done in iomanip.h ;
to explain more;
include this :
#include <iomanip>

and the output line must be updated to this :
cout << "Level: " << lvl << "\tXP: " <<setiosflags(ios::fixed)<<setprecision(0)<<floor( xp /4 )<< endl;

about the comma after every three digits u may use also :
setiosflags(ios::viewpoint) but it's not in the output u sent before .

7. Another issue: you should replace float with double or long double for higher accuracy. Furthermore, xp/4 returns an integer because the two operands are of type int. It's beter to use xp/4. to ensure that the result is of type double.

8. Registered User
Join Date
Jan 2007
Posts
9
Thanks everyone for all your help, if i get stuck on anything else, ill come back here for help :)

Will

9. Yup Danny I think also the simple int instead of float will work fine as we only need to count from 1 to say 200 or a normal countable range !
I haven't take attention in this before .

10. I'm not concerned about the small number of 200, float can represent this value. However, I'm more worried about the truncation and other inaccuracy problems that float causes. Besides, float is never a good choice. Except the case of special hardware of embedded systems, the default floating point type should be double. Not a big issue here, but it's never too late to adopt good programming practices;)

11. Senior Member
Join Date
Dec 2003
Posts
3,366
There are a few other good places for floats -- we capture a large data log using a binary write of a float array to keep the logs half sized. Same thing can be used if you are having page faults/memory issues due to a LOT of doubles.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise