c++ logic error with if/else statements

Started by
3 comments, last by wodinoneeye 13 years, 12 months ago
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?
Advertisement
omg....

Software Engineer | Credited Titles: League of Legends, Hearthstone

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 + 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; 
You should be able to get rid of all but the first

&& pLevel < 51 on those if statments as the pLevel isnt modified and it wont get past the first nested level if pLevel is >= 51
--------------------------------------------[size="1"]Ratings are Opinion, not Fact
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...
--------------------------------------------[size="1"]Ratings are Opinion, not Fact

This topic is closed to new replies.

Advertisement