I have this code here

#include <iostream>
using namespace std;

int checkLevel(){
     int exp[101]={0,1,6,21,51,100,172,274,409,583,800,1064,1382,1757,2195,2700,3276,3930,4665,5487,6400,7408,8518,9733,11059,12500,14060,15746,17561,19511,21600,23832,26214,28749,31443,
     34300,37324,40522,43897,47455,51200,55136,59270,63605,68147,72900,77868,83058,88473,94119,100000,106121,112486,119102,125971,133100,140493,148154,156090,164303,172800,181585,190662,200038,
     209715,219700,229997,240610,251546,262807,274400,286329,298598,311214,324179,337500,351181,365226,379642,394431,409600,425153,441094,457430,474163,491300,508845,526802,545178,563975,583200,
     602857,622950,643486,664467,685900,707789,730138,752954,776239,800000};         
	int level = 1;
	int nextLevel = 2;
	int PokemonExp = 0;
	int input;
	int num = 1;
		
		while (num == 1){
			cout << "Enter exp amount: ";
			cin >> input;
			PokemonExp = PokemonExp+input;
			cout << "\nCurrent level is: " << level << endl;
			cout << "\nTreeko gained "<< input << " exp!\n" << endl;
			cout << "Exp is: " << PokemonExp << endl;


				while(PokemonExp >= exp[nextLevel] && PokemonExp <= exp[101]){
					level++;
					cout << "Treeko leveled up to level: " << level << endl;
					cout << "\n";

					        nextLevel++;   
					        if(level < 100){
				            cout << "Current level is: " << level << endl;
				            cout << "\nNext level is: " << nextLevel << endl;
							cout << "\nCurrent EXP amount is: " << PokemonExp << endl;
							cout << "\nEXP to next level is: " << exp[nextLevel]-PokemonExp << endl;
							cout << "\n" << endl;
                        }else{
                            cout << "Current level is: " << level << endl;
				            cout << "\nNext level is: 100" << endl;
							cout << "\nCurrent EXP amount is: " << PokemonExp << endl;
							cout << "\nEXP to next level is: 0" << endl;
							cout << "\n" << endl;
                        }

				}
}
}

//TODO: Make a check for max EXP (800,000)

(in file ExpTable.h)

The while(num == 1) loop, is for testing purposes.

However my problem is with the while loop nested inside that loop. When I enter EXP as 10 (more than enough to level the Pokemon to 2) it just doesn't run it.

This problem only happens in XCode on Mac OSX. Works fine and as intended on Dev C++ on my Vista computer. (Mac comp is school one =/)

Any ideas?

exp[101] is out of bounds.

Wouldn't it give me an error or something then?

It just simply doesn't do my while loop. Works on windows computers.

EDIT: Changing the while loop condition for exp[101] tp exp[100] worked.

I have no idea why seeing as 800000 is exp[101] and that's what I'm testing as. o.o

EDIT2: Apparently exp[100] is 800000 and 101 is my input. o.O

wut?


What confuses me the most, is it worked on Windows with exp[101], and not on mac.

Wouldn't it give me an error or something then?

If your compiler is particularly helpful you might get a warning.

I have no idea why seeing as 800000 is exp[101] and that's what I'm testing as. o.o

No, 800000 is exp[100]. Arrays in C++ are 0-based, which means your array of 101 elements begins at exp[0] and ends at exp[100]. exp[101] is out of bounds and could contain anything. It happens that on Mac that anything stopped the loop immediately and on Windows it didn't.

>>Wouldn't it give me an error or something then?
Not at compile time. It is likely to at run-time though. You were just fortunate that it didn't.

All that the compiler really cares about is that the variable representing the array is a valid pointer to the proper type of data.

Ok, thank you both.

Resolved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.