Sign in to follow this  
turlisk

c++ logic error with if/else statements

Recommended Posts

I'm trying to build an EXP calculator for a multiplayer rpg game that will find the exp cap then tell us how many npc's will need to be killed in order to reach this cap and level up. i'm having an issue where it'll keep looping through the code and then not displaying the number properly. below is my code
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string enemy = "bob";
    int eLevel = 1;
    int pLevel = 1;
    int eExp = 7;
    int pExp = 0;
    int lineNum = 1;
    int expCap = 100;
    int goOn = 0;

    cout << "What is your level? ";
    cin >> pLevel;
    cout << "What is the Enemies level? ";
    cin >> eLevel;

    eExp = eExp * eLevel;

    if (pLevel >= 1 && pLevel < 51)
    {
        for (int i = 0; i < 10; i++)
        {
            expCap = expCap * 0.10 + expCap +1;
        }
        if (pLevel >= 10 && pLevel < 51)
        {
            for (int i = 10; i < 20; i++)
            {
                expCap = expCap * 0.10 + expCap +1;
            }
            if (pLevel >= 20 && pLevel < 51)
            {
                for (int i = 20; i < 30; i++)
                {
                    expCap = expCap * 0.15 + expCap +1;
                }
                if (pLevel >= 30 && pLevel < 51)
                {
                    for (int i = 30; i < 40; i++)
                    {
                        expCap = expCap * 0.15 + expCap +1;
                    }
                    if (pLevel >= 40 && pLevel < 51)
                    {
                        for (int i = 40; i < 50; i++)
                        {
                            expCap = expCap * 0.20 + expCap +1;
                        }
                    }
                }
            }
        }
    }
    cout << expCap << endl;
    cin >> goOn;

    while (pLevel == eLevel)
    {
        cout << lineNum << " " << "you killed: " << enemy << " " << eExp << " EXP obtained!" << endl;
        pExp = eExp + pExp;
        if (pExp >= expCap)
        {
            pLevel++;
            cout << "You Leveled up!" << endl;
        }
        lineNum++;
    }
    cout << "eExp = " << eExp << endl;
    cout << "expCap = " << expCap << endl;
    cout << "application terminated" << endl;
        return 0;

as you can see in my for loops i've manually set my limits to the number values as the amount of exp will change depending on your level range, if i set to pLevel as the limit it'll loop through all of those options till pLevel is hit. is there an easier way to import this into a looping statement?

Share this post


Link to post
Share on other sites
Note that expCap * 0.10 + expCap + 1; can be written as expCap * 1.10 + 1;


// Set up exp growth rates per levels of 10:
double rates[] = { 1.10, 1.10, 1.15, 1.15, 1.20 };

int numLoops = pLevel / 10 + 1;
for ( int i = 0; i < numLoops; ++i )
for ( int j = 0; j < 10; ++j )
expCap = expCap * rates[i] + 1;



Also, what do you mean by "not displaying the number properly"? What output do except with your input?

EDIT:

while (pLevel == eLevel) // <<-- Did you mean (pLevel < eLevel) or possibly (pExp < expCap)?
{
cout << lineNum << " " << "you killed: " << enemy << " " << eExp << " EXP obtained!" << endl;
pExp = eExp + pExp;
if (pExp >= expCap)
{
pLevel++;
cout << "You Leveled up!" << endl;
}
lineNum++;
}



That can be simplified to:

int numOfNPCsToKill = expCap / eExp + 1;

Share this post


Link to post
Share on other sites
Also you might want to look at using const data tables for those calculations.

Why compute the multiplier over and over with code when you could precalculate it and put it in a static 'lookup' table.

The code will be much simpler/cleaner...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this