private void btnHasil_Click(object sender, EventArgs e)
{
int rataipa = Convert.ToInt32(NIPA.Text);
int uipa, uipa1, uipa2, uipa3, uipa4;
if ((rataipa >= 61) && (rataipa <= 69))
{
uipa1 = (70 - rataipa) / 10;
uipa2 = (rataipa - 60) / 12;
}
else if ((rataipa == 60) && (rataipa >= 70) && (rataipa <= 72))
{
uipa2 = (rataipa - 60) / 12;
}
else if ((rataipa >= 76) && (rataipa <= 84))
{
uipa3 = (85 - rataipa) / 13;
uipa4 = (rataipa - 75) / 10;
}
else if ((rataipa >= 72) && (rataipa <= 75) || (rataipa == 85))
{
uipa3 = (85 - rataipa) / 13;
}
else
uipa = 1;
}
i try to build the errors shows in variable uipa and it shows the variable 'uipa' is assigned but its value is never used but i used it after that and now i don't know how to fix. anyone can help ?
You assign uipa at the very end of the code but then you never actually do anything with it.
else uipa = 1;
To remove the warning, you need to do something with uipa or remove the above statement entirely. As far as the compiler is concerned, uipa serves no purpose.
If you do decide to use uipa later in say an if statement, be sure to initialise uipa to some default value too else you will run into a different warning about unitialised variables.
int uipa = 0, ....; // initialise uipa here to a default value
.
.
.
else uipa = 1;
if (uipa ==1) // this will solve the variable is assigned but never used problem
{
// do something
}
int rataipa = Convert.ToInt32(NIPA.Text);
int uipa, uipa1, uipa2, uipa3, uipa4;
if ((rataipa >= 61) && (rataipa <= 69))
{
uipa1 = (70 - rataipa) / 10;
uipa2 = (rataipa - 60) / 12;
}
else if ((rataipa == 60) && (rataipa >= 70) && (rataipa <= 72))
{ uipa2 = (rataipa - 60) / 12; }
else if ((rataipa >= 76) && (rataipa <= 84))
{
uipa3 = (85 - rataipa) / 13;
uipa4 = (rataipa - 75) / 10;
}
else if ((rataipa >= 72) && (rataipa <= 75) || (rataipa == 85))
{ uipa3 = (85 - rataipa) / 13; }
else
{ //add this bracket
uipa = 1;
}
Related
I'm ripping my hair out on the following so I'm kindly asking for help here.
I have an array of durations in minutes whose total is one day (1440 min.)
I have another array with a value which represent a protection level : 0 to 5.
var levels = new int[3];
levels[0] = 0;
levels[1] = 5;
levels[2] = 0;
var minutes = new int[3];
minutes[0] = 658; // 10 hours and 58 minutes
minutes[1] = 1; // 1 minute
minutes[2] = 781; // 13 hours and 1 minutes
In plain English we should read:
Level 0 duration is 10 hours and 58 minutes, then Level 1 duration is 1 minute and finally we're back to level 0 for 13 hours and 1 minute. End of the day ;)
Of couse this is a simple example, this could be more richer than that, but I hope this helps understand the problem I'm trying to solve which is exposed below.
I need to compute an average of protection level for each hour of the day (from 0 to 23)\
The final result set would be an array of tuples with 6 items
var finalResult = new (int level0percent, int level1percent, int level2percent, int level3percent, int level4percent, int level5percent)[24];
With the data in this example that would mean:
100% for level 0 for Hours from 0 to 9
96% for level 0 for Hour 10
A this point we have 2 minutes left (4%) to be given to Hour 10.
We consume 2% (1 minute) for level 5
Still 2% (1 minute) to consume on the next level, which is 0 for now.
We add 2% (1 minute) to level 0
Then the remaining is now 13 hours for level 0 at index 11 to 23.
The tricky part for me is to process, in this case, the hour at index 10. May be I should restructure the data in a form that would be simplier to process, but I can't figure it out.
Any help appreciated.
PS : If Linq could be of any help, please do not hesitate to use it.
EDIT : Fixed the minutes array.
EDIT 2 : This what I've tried so far...
A level class to hold an intermediary result set:
public class Levels
{
public int level0 { get; set; }
public int level1 { get; set; }
public int level2 { get; set; }
public int level3 { get; set; }
public int level4 { get; set; }
public int level5 { get; set; }
}
The processing code so far... May not compile and/or work:
// Construct the final result to be sent back to the client
var averagesOn24HoursByHourNew = Enumerable.Range(0, 24).Select(i => new Levels()).ToArray();
// This index will allow us to advance each hour of the day (from 0 to 23)
int lastIndexOnAveragesOn24HoursByHourNew = 0;
// Used to gather the remaining minutes for incomplete hours
int lastMinuteLeftOnLastHour = 0;
for (var indexLevel = 0; indexLevel < levels.Count; indexLevel++)
{
if (levels[indexLevel] == 0)
{
int durationInMinutes = (int)minutes[indexLevel];
var nbMinutesLeft = durationInMinutes % 60;
var nbHoursAtOneHundred = durationInMinutes / 60;
if (lastMinuteLeftOnLastHour != 0)
{
averagesOn24HoursByHourNew[lastIndexOnAveragesOn24HoursByHourNew].level0 += lastMinuteLeftOnLastHour * 100 / 60;
nbMinutesLeft -= lastMinuteLeftOnLastHour;
lastMinuteLeftOnLastHour = 0;
}
int index = 0;
for (index = lastIndexOnAveragesOn24HoursByHourNew; index < nbHoursAtOneHundred + lastIndexOnAveragesOn24HoursByHourNew; index++)
{
#if DEBUG
Debug.Assert(index <= 23);
#endif
averagesOn24HoursByHourNew[index].level0 += 100;
datiCountForOneHour[index] += 1;
}
#if DEBUG
// When th whole day is processed the index will be 24
Debug.Assert(index < 25);
#endif
if (nbMinutesLeft != 0)
{
averagesOn24HoursByHourNew[index].level0 += nbMinutesLeft * 100 / 60;
if (lastMinuteLeftOnLastHour != 0 && lastMinuteLeftOnLastHour > nbMinutesLeft)
{
lastMinuteLeftOnLastHour -= nbMinutesLeft;
}
else
{
lastMinuteLeftOnLastHour = 60 - nbMinutesLeft;
}
}
lastIndexOnAveragesOn24HoursByHourNew = index;
}
else if (levels[indexLevel] == 1)
{
int durationInMinutes = (int)minutes[indexLevel];
// A duration of zero means less than one minute
// set it to one minute
if (durationInMinutes == 0)
{
durationInMinutes = 1;
}
else if (durationInMinutes == 1439)
{
// FIXME Add Missing minute if needed
durationInMinutes = 1440;
}
var nbMinutesLeft = durationInMinutes % 60;
var nbHoursAtOneHundred = durationInMinutes / 60;
int index = 0;
for (index = lastIndexOnAveragesOn24HoursByHourNew; index < nbHoursAtOneHundred + lastIndexOnAveragesOn24HoursByHourNew; index++)
{
#if DEBUG
Debug.Assert(index <= 23);
#endif
if (lastMinuteLeftOnLastHour != 0)
{
averagesOn24HoursByHourNew[index].level1 += lastMinuteLeftOnLastHour * 100 / 60;
lastMinuteLeftOnLastHour = 0;
}
else
{
averagesOn24HoursByHourNew[index].level1 += 100;
}
datiCountForOneHour[index] += 1;
}
#if DEBUG
// When th whole day is processed the index will be 24
Debug.Assert(index < 25);
#endif
if (nbMinutesLeft != 0)
{
averagesOn24HoursByHourNew[index].level1 += nbMinutesLeft * 100 / 60;
if (lastMinuteLeftOnLastHour != 0 && lastMinuteLeftOnLastHour > nbMinutesLeft)
{
lastMinuteLeftOnLastHour -= nbMinutesLeft;
}
}
lastIndexOnAveragesOn24HoursByHourNew = index;
}
else if (levels[indexLevel] == 2)
{
int durationInMinutes = (int)minutes[indexLevel];
// A duration of zero means less than one minute
// set it to one minute
if (durationInMinutes == 0)
{
durationInMinutes = 1;
}
else if (durationInMinutes == 1439)
{
// FIXME Add Missing minute if needed
durationInMinutes = 1440;
}
var nbMinutesLeft = durationInMinutes % 60;
var nbHoursAtOneHundred = durationInMinutes / 60;
int index = 0;
for (index = lastIndexOnAveragesOn24HoursByHourNew; index < nbHoursAtOneHundred + lastIndexOnAveragesOn24HoursByHourNew; index++)
{
#if DEBUG
Debug.Assert(index <= 23);
#endif
if (lastMinuteLeftOnLastHour != 0)
{
averagesOn24HoursByHourNew[index].level2 += lastMinuteLeftOnLastHour * 100 / 60;
lastMinuteLeftOnLastHour = 0;
}
else
{
averagesOn24HoursByHourNew[index].level2 += 100;
}
datiCountForOneHour[index] += 1;
}
#if DEBUG
// When th whole day is processed the index will be 24
Debug.Assert(index < 25);
#endif
if (nbMinutesLeft != 0)
{
averagesOn24HoursByHourNew[index].level2 += nbMinutesLeft * 100 / 60;
if (lastMinuteLeftOnLastHour != 0 && lastMinuteLeftOnLastHour > nbMinutesLeft)
{
lastMinuteLeftOnLastHour -= nbMinutesLeft;
}
}
lastIndexOnAveragesOn24HoursByHourNew = index;
}
else if (levels[indexLevel] == 3)
{
int durationInMinutes = (int)minutes[indexLevel];
// A duration of zero means less than one minute
// set it to one minute
if (durationInMinutes == 0)
{
durationInMinutes = 1;
}
else if (durationInMinutes == 1439)
{
// FIXME Add Missing minute if needed
durationInMinutes = 1440;
}
var nbMinutesLeft = durationInMinutes % 60;
var nbHoursAtOneHundred = durationInMinutes / 60;
int index = 0;
for (index = lastIndexOnAveragesOn24HoursByHourNew; index < nbHoursAtOneHundred + lastIndexOnAveragesOn24HoursByHourNew; index++)
{
#if DEBUG
Debug.Assert(index <= 23);
#endif
if (lastMinuteLeftOnLastHour != 0)
{
averagesOn24HoursByHourNew[index].level3 += lastMinuteLeftOnLastHour * 100 / 60;
lastMinuteLeftOnLastHour = 0;
}
else
{
averagesOn24HoursByHourNew[index].level3 += 100;
}
datiCountForOneHour[index] += 1;
}
#if DEBUG
// When th whole day is processed the index will be 24
Debug.Assert(index < 25);
#endif
if (nbMinutesLeft != 0)
{
averagesOn24HoursByHourNew[index].level3 += nbMinutesLeft * 100 / 60;
if (lastMinuteLeftOnLastHour != 0 && lastMinuteLeftOnLastHour > nbMinutesLeft)
{
lastMinuteLeftOnLastHour -= nbMinutesLeft;
}
}
lastIndexOnAveragesOn24HoursByHourNew = index;
}
else if (levels[indexLevel] == 4)
{
int durationInMinutes = (int)minutes[indexLevel];
// A duration of zero means less than one minute
// set it to one minute
if (durationInMinutes == 0)
{
durationInMinutes = 1;
}
else if (durationInMinutes == 1439)
{
// FIXME Add Missing minute if needed
durationInMinutes = 1440;
}
var nbMinutesLeft = durationInMinutes % 60;
var nbHoursAtOneHundred = durationInMinutes / 60;
int index = 0;
for (index = lastIndexOnAveragesOn24HoursByHourNew; index < nbHoursAtOneHundred + lastIndexOnAveragesOn24HoursByHourNew; index++)
{
#if DEBUG
Debug.Assert(index <= 23);
#endif
if (lastMinuteLeftOnLastHour != 0)
{
averagesOn24HoursByHourNew[index].level4 += lastMinuteLeftOnLastHour * 100 / 60;
lastMinuteLeftOnLastHour = 0;
}
else
{
averagesOn24HoursByHourNew[index].level4 += 100;
}
datiCountForOneHour[index] += 1;
}
#if DEBUG
// When th whole day is processed the index will be 24
Debug.Assert(index < 25);
#endif
if (nbMinutesLeft != 0)
{
averagesOn24HoursByHourNew[index].level4 += nbMinutesLeft * 100 / 60;
if (lastMinuteLeftOnLastHour != 0 && lastMinuteLeftOnLastHour > nbMinutesLeft)
{
lastMinuteLeftOnLastHour -= nbMinutesLeft;
}
}
lastIndexOnAveragesOn24HoursByHourNew = index;
}
else if (levels[indexLevel] == 5)
{
int durationInMinutes = (int)minutes[indexLevel];
// A duration of zero means less than one minute
// set it to one minute
if (durationInMinutes == 0)
{
durationInMinutes = 1;
}
else if (durationInMinutes == 1439)
{
// FIXME Add Missing minute if needed
durationInMinutes = 1440;
}
var nbMinutesLeft = durationInMinutes % 60;
var nbHoursAtOneHundred = durationInMinutes / 60;
int index = 0;
for (index = lastIndexOnAveragesOn24HoursByHourNew; index < nbHoursAtOneHundred + lastIndexOnAveragesOn24HoursByHourNew; index++)
{
#if DEBUG
Debug.Assert(index <= 23);
#endif
if (lastMinuteLeftOnLastHour != 0)
{
averagesOn24HoursByHourNew[index].level5 += lastMinuteLeftOnLastHour * 100 / 60;
lastMinuteLeftOnLastHour = 0;
}
else
{
averagesOn24HoursByHourNew[index].level5 += 100;
}
datiCountForOneHour[index] += 1;
}
#if DEBUG
// When th whole day is processed the index will be 24
Debug.Assert(index < 25);
#endif
if (nbMinutesLeft != 0)
{
averagesOn24HoursByHourNew[index].level5 += nbMinutesLeft * 100 / 60;
if (lastMinuteLeftOnLastHour != 0 && lastMinuteLeftOnLastHour > nbMinutesLeft)
{
lastMinuteLeftOnLastHour -= nbMinutesLeft;
}
}
lastIndexOnAveragesOn24HoursByHourNew = index;
}
else
{
throw new InvalidOperationException("Invalid protection level found in stats");
}
}
You can try it this way. Probably not the most elegant solution, but gets the job done and can be a basis for further optimization. For explanation see comments in the code.
using System;
using System.Linq;
public class Program
{
class percentages {
public double l0;
public double l1;
public double l2;
public double l3;
public double l4;
public double l5;
public string ToString() {
return $"{l0:F2} {l1:F2} {l2:F2} {l3:F2} {l4:F2} {l5:F2} ";
}
}
public static void Main()
{
var levels = new int[]{0,5,0};
var minutes = new int[]{658, 1, 781};
var plist = new percentages[24];
//sum up the minutes to total duration on this day
//ie minutes will become [658, 659, 1440]
for (int i = 1; i < minutes.Length; i++)
minutes[i] += minutes[i-1];
//lindex is the current index in the levels/minutes array
var lindex = 0;
//pindex is the current index in the percentages array
var pindex = 0;
var p = new percentages();
//a constant of how much 1 minute contributes in percent of an hour
var m1 = 100.0 / 60;
//check every minute of the day
for (int m = 1; m <= 1440; m++) {
//increment the respective level by 1 minute
switch(levels[lindex]) {
case 0: p.l0 += m1; break;
case 1: p.l1 += m1; break;
case 2: p.l2 += m1; break;
case 3: p.l3 += m1; break;
case 4: p.l4 += m1; break;
case 5: p.l5 += m1; break;
}
//if the minutes reached a level change increment the index in the level/minute array
if (m == minutes[lindex]) lindex++;
//if the minutes reached a full hour, add the current percentages to the list, increment index and reset for the next hour
if (m % 60 == 0){
plist[pindex++] = p;
Console.WriteLine(p.ToString());
p = new percentages();
}
}
}
}
Be aware, there are no sanity checks whatsoever. This will break if levels and minutes don't have the same length or if the sum of minutes is not 1440. For further use, you should add these checks.
I suggest you create another array level_at_min where the index is a minute in the day.
level_at_min will be of size 1440 (I show an algorithm to do it later).
In your example you will have
level_at_min[0] = 0
...
level_at_min[657] = 0
level_at_min[658] = 5
level_at_min[659] = 0
...
level_at_min[1439] = 0
If you have that array, you can create an array of lists (of length 60) levels_at_hour,
that will give for your example
levels_at_hour[0] = [0,0, ..., 0]
levels_at_hour[1] = [0,0, ..., 0]
....
levels_at_hour[11] = [0,..., 5,0]
...
levels_at_hour[23] = [0,0,...,0]
With this you can compute the average protection level at each hour, or the percentage of each protection level at each hour, whatever you want.
Algorithm to create level_at_min: (n the length of array levels and array minutes)
minute_counter = 0
for each block in 0 to n-1
for each minute in minute_counter to (minute_counter+minutes[block])
level_at_min[min] = levels[block]
minute_counter = minute_counter + minutes[block]
Algorithm to create levels_at_hour:
for each hour in 0 to 23:
for each minute in i*60 to (i+1)*60
add level_at_min[minute] to the list levels_at_hour[hour]
This is not the actual code but it's what I want to do.
for loop is must but the nested if else loop inside should be executed according to the value of count_final which can be random between 1 to 3.
Like if the value of count_final is 3, all if...else loop should be considered. but if the value of count_final is 2, then only if...(1st)else if and else part only be executed. And if count_final=1 then only if and else part is executed (not any else-if).
Thought of putting another if...else within every if...else and checking count_final, but what if I'm not getting values of count2 and count3 when count_final=1.
Same, when count_final=2, I'm not getting the value of count3.
Ask in comment if you don't understand my question.
int count_final=Session["abc"];
//count_final=1;
//count_final=2;
//count_final=3;
for(int i=1;i<=10;i++)
{
if ((count1 <= count5) && (count1 <= count6))
{
Label1.Text="Hello1";
}
else if (count2 <= count4 && count2 <= count6)
{
Label2.Text="Hello2";
}
else if (count3 <= count4 && count3 <= count5)
{
Label3.Text="Hello3";
}
else
{
Label1.Text="Hello1";
}
}
Seems you have collection of "conditions" where amount of executed conditions depend on value of finalCount.
var rules = new Func<string>[]
{
() => (count1 <= count5 && count1 <= count6) ? "Hello1" : null,
() => (count2 <= count4 && count2 <= count6) ? "Hello2" : null,
() => (count3 <= count4 && count3 <= count5) ? "Hello3" : null
};
Label1.Text = rules.Take(finalCount)
.Select(rule => rule())
.Where(result => result != null)
.DefaultIfEmpty("Hello1")
.First();
Of course this solution is assuming that finalCount is always 1, 2 or 3.
DefaultIfEmpty is playing role of last else - will be used all conditions fails.
if i understand right.. which is a little unlikely
just add more criteria to your ifs!
if ((count1 <= count5) && (count1 <= count6))
{
if (count_final == 3 || count_final == 2) Label1.Text="Hello1";
}
else if (count2 <= count4 && count2 <= count6)
{
if (count_final == 3) Label2.Text="Hello2";
}
else if (count3 <= count4 && count3 <= count5)
{
if (count_final == 3) Label3.Text="Hello3";
}
else
{
if (count_final == 3 || count_final == 1) Label1.Text="Hello1";
}
Remembering that from what I understood there will be loops in that can achieve nothing, eg, if count_final == 2 and its not less or equal to count5 or count6, nothing will happen, same as for count_final == 1, if it matches any of the first bits the last else wont happen.
I suggest extending the conditions of your else if statements:
int count_final=Session["abc"];
//count_final=1;
//count_final=2;
//count_final=3;
for(int i=1; i<=10; i++)
{
if ((count1 <= count5) && (count1 <= count6))
{
Label1.Text="Hello1";
}
else if (count_final >= 2 && count2 <= count4 && count2 <= count6)
{
Label2.Text="Hello2";
}
else if (count_final >= 3 && count3 <= count4 && count3 <= count5)
{
Label3.Text="Hello3";
}
else
{
Label1.Text="Hello1";
}
}
When count_final == 1 this will not try to evaluate count2, count3 or count4 (which I understand is a requirement) because && will not evaluate its right hand side when there is false on the left.
Is this what you are looking for? To use less if/else statements:
int count_final = Session["abc"]; // random between 1 and 3
for(int i=1; i <= 10; i++)
{
switch(count_final)
{
case 3:
Label1.Text="Hello1";
// no break; so all gets executed
case 2:
Label2.Text="Hello2";
case 1:
Label3.Text="Hello3";
default: Label1.Text="Hello1";
}
}
I'm trying to determine the Big O notation of two methods that are based on Jim Mischel's BinaryHeap class.
public void Add(T item) {
int i = _items.Count;
_items.Add(item);
while (i > 0 && _items[(i - 1) / 2].CompareTo(item) > 0) {
_items[i] = _items[(i - 1) / 2];
i = (i - 1) / 2;
}
_items[i] = item;
}
public T Remove() {
T firstItem = _items[0];
T tempItem = _items[_items.Count - 1];
_items.RemoveAt(_items.Count - 1);
if (_items.Count > 0) {
int i = 0;
while (i < _items.Count / 2) {
int j = (2 * i) + 1;
if ((j < _items.Count - 1) && (_items[j].CompareTo(_items[j + 1]) > 0)) ++j;
if (_items[j].CompareTo(tempItem) >= 0) break;
_items[i] = _items[j];
i = j;
}
_items[i] = tempItem;
}
return firstItem;
}
For the Add method, I believe it's O(log(n)) since the loop appears to be of the form:
for(int i = n; i > 0; i=(i-1)/2);
And for the Remove method, I believe it's O(log(log(n)) since the loop appears to be of the form:
for(int i=0; i < n/2; i = (2*i)+1);
Is this correct?
How can I check so that if any elements relate to the if the foreach would set it instead of later going to the else?
for (int i = 0; i < 3; i++)
{
Grass grass = new Grass(32 + 32 * i, 32 * 12);
GrassList.Add(grass);
}
foreach (Grass grass in GrassList)
{
if (grass.Rect.X - 32 <= Rect.X && grass.Rect.X + 32 >= Rect.X && grass.Rect.Y - 65 <= Rect.Y)
{
FallSpeed = 0;
isTouchingGround = true;
}
else
{
FallSpeed = 3;
isTouchingGround = false;
}
}
You can use the LINQ expression with if condition to achieve what you want also
FallSpeed = 3;
isTouchingGround = false;
if(GrassList.Any(
grass => grass.Rect.X - 32 <= Rect.X &&
grass.Rect.X + 32 >= Rect.X &&
grass.Rect.Y - 65 <= Rect.Y))
{
FallSpeed = 0;
isTouchingGround = true;
}
The best option is to short-circuit your evaluation. As soon as you know that you are touching the ground, break out of the loop. Your else branch is basically your default case.
FallSpeed = 3;
isTouchingGround = false;
foreach (Grass grass in GrassList)
{
if (grass.Rect.X - 32 <= Rect.X && grass.Rect.X + 32 >= Rect.X && grass.Rect.Y - 65 <= Rect.Y)
{
FallSpeed = 0;
isTouchingGround = true;
break;
}
}
I am programming a console "game" and I need to declare a "hp" of character in IF statements which depends on level of this character.
if ((char_level > 0) && (char_level < 4))
{
char_hp = 100;
}
if ((level > 4) && (level < 6))
{
char_hp = 120;
}
if ((level > 6) && (level < 8))
{
char_hp = 150;
}
if ((level > 8) && (level < 10))
{
char_hp = 180;
}
Then I need to use it later in code in a fight. After a successful fight character gets a new level and after that, program will get back to check these IF statements and if level is bigger than 4, character's hp will be increased to 120. But declaration of char_hp in IF statements does not change the value of hp in general and when the next fight comes after reaching level 4, character's hp is still like at the end of previous battle was. I am new in C# programming and I have tried everything but I can't solve it, if it is possible.
The same problem is with the "hp" of enemy that is randomly generated...then I need to use it in that fight
if((level>0) && (level<4))
{
random_enemy_hp = RND.Next(89, 111);
goto enemy;
}
if((level>4) && (level<6))
{
random_enemy_hp = RND.Next(109, 141);
goto enemy;
}
if ((level > 6) && (level < 8))
{
random_enemy_hp = RND.Next(149, 184);
goto enemy;
}
if ((level > 8) && (level < 10))
{
random_enemy_hp = RND.Next(189, 221);
goto enemy;
}
EDIT: I meant "saving values to variables" in IF statements, so I can use them later in code. This is how my code starts, then there are "Console.WriteLine()"-s, principe of a fight and statements shown above.
string name;
int char_hp = 100;
int level = 1;
int random_enemy_hp;
Random RND = new Random();
You're completely on the wrong track. You should be doing something like:
int[] charLevelHp = { 100, 100, 100, 100,
120, 120, 120,
150, 150,
180, 180 };
int charLevel = 1;
int charHp = charLevelHp[charLevel];
I can't help but notice that you're comparing with char_level for your first couple if-statement, but you're comparing to level for your subsequent if-statements
if ((char_level > 0) && (char_level < 4))
{
char_hp = 100;
}
if ((level > 4) && (level < 6))
{
char_hp = 120;
}
I think you might have intended to use char_level for all of the conditions.
if ((char_level > 0) && (char_level < 4))
{
char_hp = 100;
}
if ((char_level > 4) && (char_level< 6))
{
char_hp = 120;
}
If that's the issue, it would be consistent with the kinds of errors you're seeing.