Im trying to shuffle my deck of cards using pointers. My loops just print out the default value, not cycling through the cards. how do i fix this. shuffle functiuon is at the bottom? I am trying to loop through and increment pointers while replacing my destination pointer with my source pointer. or the dereferenced value. what am i doing wrong?
#include <iostream>
#include <fstream>
#include <ctime>
#include <stdlib.h>
#include <string>
using namespace std;
//global constant(s)
const int maxCards = 52;
//Structs
struct card
{
char suit[8];
char rank[6];
int cvalue;
char location;
};
struct player
{
char name[100];
int total;
};
//Function List
char * strcopy(char destination[], const char source[]);
void shuffle(card* destinationP,card* sourceP);
//program
int main()
{
//begin seeding the time for randomize later
srand(time(NULL));
//declaration of variables
bool gameOn =true;
int choice;
char tempfName[100];
char templName[100];
int count =0;
//create struct array(s)
card deck[52];
card shuffledDeck[52];
player people[4];
//create pointer and set initial value
card * deckPointer = NULL;
card * shuffledDeckPointer= NULL;
player *peoplePointer = NULL;
//
deckPointer = &deck[0]; //assign address of deck to deckPointer
shuffledDeckPointer = &shuffledDeck[0];
peoplePointer = &people[0]; //assign address of people to peoplePointer
//sets default values for the card arrays
for(int i=0;i<52;i++)
{
strcopy(shuffledDeck[i].suit, "suit");
strcopy(shuffledDeck[i].rank,"rank");
shuffledDeck[i].cvalue = 0;
strcopy(deck[i].suit,"suit");
strcopy(deck[i].rank,"rank");
deck[i].cvalue = 0;
}
//set up card file to be read in
ifstream fin;
char finName[12];
//get file name from user
cout << "Enter file name...(cardFile.txt)" << endl;;
cin >> finName;
//open the file
fin.open(finName);
//check if cardFile.txt opens correctly
if(!fin.good())
{
cout << "Error with card file" << endl;
return 0;
}
else
{
card *deckPointer = NULL;
//prime fin
//fin >> deck[i].suit;
//fin >> deck[i].rank;
//fin >> deck[i].cvalue;
while(fin.good())
{
for(deckPointer = &deck[0]; deckPointer < &deck[maxCards];deckPointer++)
{
fin >> (*deckPointer).suit;
fin >> (*deckPointer).rank;
fin >> (*deckPointer).cvalue;
}
}
}
cin.clear();
}
//Functions
//copy string function
char * strcopy(char *destination, const char* source)
{
char *p = destination;
while(*p++ = *source++);
return destination;
}
//Shuffle function
void shuffle(card *destinationP,card *sourceP)
{
int randomNumber = 0;
int count=0;
for(int j=0;j<52;j++)
{
//choose a random number up to 52
randomNumber = rand()%52;
count = 0;
if(count < randomNumber)
{
while(count < randomNumber)
{
*sourceP++;
count++;
}
//check if destination is empty ie will accept
//a card and will not overwrite a card
if((*destinationP).cvalue == 0)
{
// copy unshuffled "deck" to the shuffled "deck"
strcopy((*destinationP).suit, (*sourceP).suit);
strcopy((*destinationP).rank, (*sourceP).rank);
(*destinationP).cvalue = (*sourceP).cvalue;
*destinationP++;
}
}
else
{
if((*destinationP).cvalue == 0)
{
// copy unshuffled "deck" to the shuffled "deck"
strcopy((*destinationP).suit, (*sourceP).suit);
strcopy((*destinationP).rank, (*sourceP).rank);
(*destinationP).cvalue = (*sourceP).cvalue;
*destinationP++;
}
}}}
cardhas to worry about these details. 2) Develop new functionality in isolation. Don't try to shuffle cards, try to shuffle ints. When the shuffling works perfectly, then shuffle cards. 3) Learn to prepare a minimal complete example. It will help you find the bug, and help us find the bug if you can't.