"If" statements are being ignored.

Alright, a couple explanations:
1. I account for the discrepancy between an array subscript and what I refer to as the "Room Number". In for for loop to define my rooms, I set the room number (an integer within the Room class) as (i + 1). That way, Room[0] shows up as Room #1 in the game.
2. ItemDatabase is a function containing strings for an item's name, description, and details. I return the strings to a temporary text variable, format that, then save it to the appropriate strings in the Item class. Sorry that bit is so messy; please bear with me.

My problem is thus: Those "if" statements down near the bottom? They get completely ignored. For one, I have-
if(rand() % 10 <= 5 && itemArray[j].getItemType() != Consumable)
and the thing is, when I generate my rooms, I have lots of Consumable type items who are useful elsewhere. I don't want that.

Likewise, here-
if(roomArray[random - 1].getItemNeeded() != true && random != 1 && random != i + 1)
(I use roomArray[random - 1] because random will always be at least 1, like I want, and I need to convert it to subscript mode)
Here, the line getItemNeeded() != true would ideally ensure that in the process of item and room generation, if it detects that a given room has the bool variable itemNeeded set to true, then it won't generate another "useful" item for that room, but I have all kinds of items stacking up on rooms. Some rooms have 5 or more useful items in a big batch of room generation! I don't understand what's going on. I've tried tons of different things, from enclosing the class and member function portions in parentheses; as you can see I tried the && operators. Nothing works, and I'm frustrated and tired. Neither are a good combination for writing good code, so I came here to seek some assistance and perhaps a fresh set of eyes.

Full code portion in question:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
		for(int j = 0; j < numItems; j++)
		{
			random = dice(1, 10);
			if(random >= 1 && random <= 3)
				tempType = Sharp;
			else if(random == 4 || random == 5)
				tempType = Knowledge;
			else if(random >= 6 && random <= 8)
				tempType = Unlock;
			else if(random == 9 || random == 10)
			{
				tempType = Consumable;
				itemArray[j].setHealth(true);
			}
			else
				tempType = Default;
			itemArray[j].setItemType(tempType);
			tempText = ItemDatabase(3, itemArray[j].getItemType(), random);
			itemArray[j].setName(tempText);
			random = dice(1, 10);
			tempText = ItemDatabase(4, itemArray[j].getItemType(), random);
			tempText = format(tempText); //
			itemArray[j].setDescription(tempText);
			random = dice(1, 10);
			tempText = ItemDatabase(5, itemArray[j].getItemType(), random);
			tempText = format(tempText); // 
			itemArray[j].setDetails(tempText);
			if(rand() % 10 <= 5 && itemArray[j].getItemType() != Consumable)
			{
				random = (rand() % numRooms + 1);
				if(roomArray[random - 1].getItemNeeded() != true && random != 1 && random != i + 1)
				{
						itemArray[j].setUseful(true);
						itemArray[j].setUsefulRoomNumber(random);
						roomArray[random - 1].setItemNeeded(true);
				}
				else
				{
					itemArray[j].setUseful(false);
					itemArray[j].setUsefulRoomNumber(0);
				}
			}			
		}
		roomArray[i].populateItems(numItems, itemArray);
	}
Last edited on
Since I cannot see your whole code I am trying to assume:
if(rand() % 10 <= 5 && itemArray[j].getItemType() != Consumable)
If you find an if statement ignored (I guess you mean it's never called the true condition statement that is) check your condition.
I cannot really help you with itemArray[j].getItemType() != Consumable since I don't know what type itemArray is but the first part should be OK. So since && is being used there is a second condition that's never met.
Check why is never met itemArray[j].getItemType() != Consumable

As you know an && condition requires all parts being true to be true.
Last edited on
Alrighty, I think I found the problem. I wasn't clearing out old data from the itemArray (which is of type Item. I try to be straightforward ^__^;)

Anyways, I added some statements at the beginning of the for loop which set all the flags that were giving me issues to 0, and after a couple test runs of the program my if statements work as intended now. Woo! =)

Topic archived. No new replies allowed.