Thread: question about my code

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    17

    question about my code

    im working a card program that will shuffel and display the cards. im trying to randomize numbers but this code will not print anything. it seems like it goes into an endless loop.


    for(int g=0; g<52; g++)
    {
    do
    {
    card1 = rand() % 52;

    for(int d=0; d<52; d++)
    {
    if(card_array[d]==card1)
    counter++;
    }
    if (counter > 4)
    {
    u=0;
    }
    else if (counter < 4)
    {
    card_array[g] = card1;
    u=1;
    }
    }while(u==0);

    cout << card1 <<endl;

    }


    any help would be good.

  2. #2
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    Output counter and u and re-examine you conditions and I bet you'll be able to find your problem.

    /*Edit */
    HINT: Think about setting counter back to zero during the loop.
    Last edited by taylorguitarman; 02-18-2002 at 01:12 AM.
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    17
    and i what i got was an endless loop. gotta work that out!

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    17
    im still having much trouble. I want the rand to generate a number btw 0-52. and check its value with previously generated values that are stored in array. if a match occurs more than 4 times , randomize again. if there isnt, store it in the next array "window". But the code doesnt work. I understand the logic behind it and i tried to put into code but it doenst translate into workable code.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    17
    good point with the counter. let me try that

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    17
    ive tried re-working the code, but i keep coming with idea. it it randomizes but it still will intsert the value in the array even thoughh counter>4. when i try to output the array of values, it returns numbers out of the range of 52 and sometimes negative values. im guessing its the address, but im not telling it to store the address.

  7. #7
    Unregistered
    Guest
    I did this with randgen

    int DealCard(apvector<int> & cards)
    {
    RandGen r;
    int value;
    do
    {
    value = r.RandInt(52);
    } while(cards[value] != 1);
    cards[value] = 0;
    return value;
    }

    void PrintCard(int card)
    {
    switch (card%13) {
    case 0: cout<<"2 of "; break;
    case 1: cout<<"3 of "; break;
    case 2: cout<<"4 of "; break;
    case 3: cout<<"5 of "; break;
    case 4: cout<<"6 of "; break;
    case 5: cout<<"7 of "; break;
    case 6: cout<<"8 of "; break;
    case 7: cout<<"9 of "; break;
    case 8: cout<<"10 of "; break;
    case 9: cout<<"Jack of "; break;
    case 10: cout<<"Queen of "; break;
    case 11: cout<<"King of ";break;
    case 12: cout<<"Ace of "; break;
    }
    switch (card / 13) {
    case 0: cout<<"spades. "; break;
    case 1: cout<<"hearts. "; break;
    case 2: cout<<"clubs. "; break;
    case 3: cout<<"diamonds. "; break;
    }
    }


    You mod the cards to get the value and you divide to get the suit....

  8. #8
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    How about a different approach to shuffling the deck?
    Initialize the deck in order (like a fresh deck).
    Then, "shuffle" the cards for a couple seconds by swapping two random indices.
    Output the deck using Unregistered's idea.

    Here's a sample program:
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hi all i have a question about a code
    By iweapons in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2007, 11:05 AM
  2. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  3. My First If Code, Question? :P
    By dimirpaw in forum C++ Programming
    Replies: 3
    Last Post: 11-29-2005, 08:49 PM
  4. End of Code Loop Question
    By JuanSverige in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 10:35 AM
  5. EXE to code? Decompile question.
    By RoD in forum Game Programming
    Replies: 4
    Last Post: 10-01-2002, 05:28 PM