I have to build a card game war in a C++ card game. The program must randomize a set of cards for both the user and a computer player. the cards are 2-14 for one array then a parrallel array for suit 0-3. then organize each hand in descending order and run the game. this is what i have so far:

#include <iostream>
#include <string>
#include <stdlib.h>
#include <ctime>

using namespace std;

void dealCardSet (int[], int[], int [], int [], int);
void showUserCards (int [], int [], int);
void showCompCards (int [], int [], int);
void getOneNewCard (int, int [], int [], int);
void dealRandCard (int, int [], int[]);
void bubblesort (int[], int[], int);
void compBubbleSort (int [], int[], int);
//void playwar

int main ()
{

int N, throwaway, cards[N], suit[N], compCards[N], compSuit[N];

unsigned seed = time(0);
srand (seed);

        cout <<" How many cards do you want to play? -> ";
        cin >> N;

        dealCardSet (cards, suit, compCards, compSuit, N);
        showUserCards (cards, suit, N);
        showCompCards (compCards, compSuit, N);
        getOneNewCard (throwaway, cards, suit, N);
        bubblesort (cards, suit, N);
        compBubbleSort (compCards, compSuit, N);

return 0;
}

void dealCardSet (int cards[], int suit[], int compCards [], int compSuit[], int N)
{
        for(int a=0; a<N; a++)
        {
                cards[a]=rand()%13+2;
                suit[a]=rand()%4+1;
        }
        for(int a=0; a<N; a++)
        {
                compCards[a]=rand()%13+2;
                compSuit[a]=rand()%4+1;
        }

}

void showUserCards (int cards [], int suit [], int N)
{
        for(int a=0; a<N; a++)
        {
                cout<<"Card"<<a <<" : " <<cards[a]<<" ("<<suit[a]<<")"<<endl;
        }
}

void showCompCards (int compCards [], int compSuit [], int N)
{
        for(int a=0; a<N; a++)
        {
                cout<<"Card"<<a <<" : " <<compCards[a]<<" ("<<compSuit[a]<<")"<<endl;
        }
}

void getOneNewCard(int throwaway, int cards [], int suit [], int N)
{
        cout<<"If you want a new card, choose index of card to throw away.Otherwise, type -1."<<endl;
        cout<<"Your Choice = ";
        cin>> throwaway;
        while (throwaway!=-1)
        {
        dealRandCard (throwaway, cards, suit);
        cout << "Your New Cards" << endl;

                for(int a=0; a<N; a++)
                {
                        cout<<"Card"<<a <<" : " <<cards[a]<<" ("<<suit[a]<<")"<<endl;
                }
        }
}

void dealRandCard (int throwaway, int cards [], int suit [])
{
        cards[throwaway]=rand()%13+2;
        suit[throwaway]=rand()%4+1;
}
void bubblesort (int cards[], int suit[], int N)
{
   int a, b, temp, temp2;
      for (a=0; a<N; a++)
        for (b=0; (b<N-1); b++)
           if (cards[b]<cards[b+1])
            {
            temp=cards[b];
            cards[b]=cards[b+1];
            cards[b+1] = temp;
            temp2= suit[b];
            suit[b]= suit[b+1];
            suit[b+1]=temp2;
            }
}
void compBubbleSort ( int compCards[], int compSuit[], int N)
{
 int a, b, temp, temp2;
      for (a=0; a<N; a++)
        for (b=0; (b<N-1); b++)
           if (compCards[b]<compCards[b+1])
            {
            temp=compCards[b];
            compCards[b]=compCards[b+1];
            compCards[b+1] = temp;
            temp2= compSuit[b];
            compSuit[b]= compSuit[b+1];
            compSuit[b+1]=temp2;
            }
}

Please be more specific with your question..."When I run the program, I'm seeing ____. I tried ____ but it didn't solve it." or something along that line.

Does the code compile?

Does the program run?

Does it do what it is supposed to do?

sorry bout that
it does compile but when i a.out i get a segmentation fault.
I believe this means I have an overfilled array but I can't where this would be.

The code:

int main ()
{

int N, throwaway, cards[N], suit[N], compCards[N], compSuit[N];

will declare arrays of length N (but at the time of the array construction, N is either zero or undefined). The arrays will NOT be resized when the user enters a value for N later in the function.

So if you later then fill the arrays up to the new value of N, you are overwriting memory that doesn't belong to you.

This article has been dead for over six months. Start a new discussion instead.