Hello, I am working on a Blackjack card game project where I am given a text file where each line has an integer for the value of the card and a string for the card face and suit. "10 (K_S)" is an example entry. The instruction is to read each entry from the file and add it to an array to build the deck.
I am looking for some guidance as to how to keep both the integer and string as a single card because I will need to be able to shuffle the deck, display only the string portion and use the integer to add the sum of the cards in a hand.
I am trying to follow the structure for a parallel array, but if you have two separate entries for int and string, won't they get separated when you perform a shuffle?
Right now I haven't gotten to the shuffle because I can't tell if my arrays are even correct. I am trying to cout the arrays at the end just to make sure the file is being read and cards are being created, but my program only asks for the file name, prints -858993460 and returns 0. Where am I going wrong?
Eventually I will need to transition the array(s) into the CardSet class, but I'm trying to get them functional first.
Here's how my main looks so far:
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include <iostream>
#include <fstream>
#include "CardSet.h"
#include "BjCardSet.h"
using namespace std;
int main()
{
//object for deck
CardSet cs;
//initialize
cs.setVariables(1, "(A_S)");
//object for player's hand
BjCardSet bcs;
const int SIZE = 52; //Size declaration of array
int values[SIZE]; //Array for int values
string cards[SIZE]; //array for string cards
int count = 0; //counter variable initialized to 0
string filename;
ifstream inFile;
//prompt user for file name
cout << "File?" << endl;
getline(cin, filename);
cout << endl;
//verify file opens correctly
inFile.open(filename.c_str());
if (!inFile) {
cout << "Unable to open file";
return 1; //exit with error
}
//pull data from text file to populate arrays
inFile >> values[count] >> cards[count];
while (!inFile.eof())
{
count++;
inFile >> values[count]
>> cards[count];
}
//testing arrays
cout << values[count] << "\t";
cout << cards[count] << "\t";
//close text file after populating arrays
inFile.close();
return 0;
}
|