0

I followed some of the answers here on the site but I still have a problem: after I generate my vector of cards when I run the program the card numbers are totally off: first card:100 second card:22 third:0 fourth:0 fifth:28 etc.

This is my code:

    enum numar {AS=11 ,DOI=2, TREI=3, PATRU=4, CINCI=5, SASE=6, SAPTE=7, OPT=8,
NOUA=9, ZECE=10, VALET=10, DAMA=10, REGE=10,
PrimulNumar=AS, UltimulNumar=REGE};
enum culoare {INIMA, CAROU, NEGRU, TREFLA, PrimaCuloare=INIMA,
UltimaCuloare=TREFLA};



 int k=0;
        for (int r = PrimulNumar; r <= UltimulNumar; ++r) {
                for (int s = PrimaCuloare; s <= UltimaCuloare; s++) {
                        pc[k]= new Carte((numar)r, (culoare)s, false);
                        k++;

What should I do?

Emil Omir
  • 87
  • 1
  • 6
  • The code is in romanian but I think you will understand. – Emil Omir Jan 17 '11 at 17:26
  • First of all I'm not sure if make sense to create 2 items in the enum with the same value. Could you please explain what are you trying to do? – Elalfer Jan 17 '11 at 17:31
  • 1
    @Elalfer: `enum numar` contains card ranks. Ace = 11, two = 2, etc. Ten == jack == queen == king == 10. – Fred Larson Jan 17 '11 at 17:34
  • 1
    Is that homework ? For one thing, PrimulNumar=11 and UltimulNumar=10. I think your array is just uninitialized ^^ – neuro Jan 17 '11 at 17:34
  • 1
    All an `enum` is in most languages is just a convenient way of declaring a bunch of integer constants. – Omnifarious Jan 17 '11 at 17:38

1 Answers1

5

So...replacing enums with values:

int k = 0;
for (int r = 11; r <= 10; ++r)
  for (int s = 0; s <= 3; ++s
  {
  }

See the problem?

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
  • Yeah, I see the problem. How can I do it anyway? I took the ideea from here: http://stackoverflow.com/questions/2941890/c-enum-in-foreach – Emil Omir Jan 17 '11 at 17:42
  • Note that they didn't set values for their enums. That's an important detail that totally changes the outcome. If you're having trouble sensing why, review enums in C++: http://enel.ucalgary.ca/People/Norman/enel315_winter1997/enum_types/ – Edward Strange Jan 17 '11 at 17:55
  • But I don't understand how to give the same value to ten jack queen and the king... That's why I tried to give values. – Emil Omir Jan 17 '11 at 18:10
  • If you do that you'll not be able to tell them apart. – Edward Strange Jan 17 '11 at 18:11
  • @Emil: You could let the compiler assign the enum values (so AS==0,DOI==2, ... REGE==12) and then use an array of scores for the ranks, i.e. `int scores[] = {11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10 10};`. Then scores[AS] == 11, scores[DAMA] == 10, etc. – Fred Larson Jan 17 '11 at 19:32