nested loop just outputting garbage noob

i have to create a card game with 30 rounds. i have nearly finished but i a struggling with the end.
questions are;
why is the sorceress not printing cName after the second round?
how do i deduct the attk value from the health?

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
 for (int round = 1; round <=30; round++) // loops until round is equal to 30
	{
		int sorceressHealth = 30;
		int wizardHealth = 30;
		for (int x = 0; x < ARRAYROW; x++) {
			
			
			system("pause");
			//Round begins
			cout << endl << "Round " << round << "  fight!" << endl;


			//Sorceress draws card
			cout << "Sorceress draws " << sorDeck[x].sName << endl;
			//Card attacks Wizard
			cout << sorDeck[x].sName << " deals " << sorDeck[x].sAttk << " damage to Wizard!" << endl;


			//wizard draws card
			cout << "Wizard draws " << wizDeck[x].wName << endl;

			//Card attacks sorceress
			cout << wizDeck[x].wName << " deals " << wizDeck[x].wAttk << " damage to Sorceress!" << endl;


			cout << endl << "Sorceress has:" << sorceressHealth << " Health" << endl; // displays health
			cout << "Wizard has:" << wizardHealth << " Health" << endl;
		}

			if (round == 30) {
				cout << "its a tie!" << endl;
				system("pause");
			}
			if (wizardHealth == 0) {
				cout << "Sorceress Wins!" << endl;
				system("pause");
			}
			if (sorceressHealth == 0) {
				cout << "Wizard Wins!" << endl;
				system("pause");
			}
	}
Last edited on
I'm not sure how to help with the name question but as for the health area. I would define the health outside of the for loop otherwise anytime the for loop iterates it will just go back to 30 even if you change it. So declare and define the default value of it outside. And a simple operation such as health -= attack; should subtract the attack value from the health.
not sure what cName is referring to, but it's a good idea to check your structure
i have read a text file in and stored in an array cName refers to card name there is also card type, card attack and card defense. i have the rounds working now just the cards and health to go.

the wizard cards work but the sorceress deck doesnt
The problem with cName is that "cName" doesn't appear anywhere the code you posted, so we can't help you there.

Why do you have two nested loops? As coded, each round includes going through all of the cards in the decks. Is that what you mean?

You have to break out of the loops when someone wins.

It looks like sorDeck and wizDeck are collections of two different classes. Why are the classes different? They both have a name and attack value, so why not use the same type? You could even go further with something like:
1
2
3
4
5
6
7
8
9
10
class Card {
    string name;
    unsigned attack;
};

class Player {
    int health;
    vector<Card> deck;
    // other data related to a player.
};

its meant to say sName. sorry

why would attack be unsigned?

how do i only select cards with type 1?
Last edited on
why is the sorceress not printing [sName] after the second round?

Is it pausing at line 8?

why would attack be unsigned?

I assumed that an attack always deals positive damage. If an attack can actually deal negative damage, then it should be signed.

how do i only select cards with type 1?

I didn't know that cards had a type.

We might be able to help more if you post the full code.
Topic archived. No new replies allowed.