Read text file with multiple variables into array

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;
}
I am looking for some guidance as to how to keep both the integer and string as a single card

You want to use a struct:

1
2
3
4
5
6
7
8
9
10
struct Card
{
     int value;
     std::string name;
}

int main()
{
     Card deck[50]; //Array of the struct - 50 cards
}



Also, if the output is coming out wrong, make sure you're taking the values in correctly and whether or not the file has the info you think is in there. You should also check if the file opened with "!inFile.is_open()".


EDIT: I can see you're using a class, very much almost the same as a struct. You can either have a separate struct that you put in the class as a variable, or make the class hold the variables (makes more sense):

1
2
3
4
5
6
7
8
9
10
11
class Card
{
private:
	int value;
	std::string name;
};

int main()
{
	Card deck[50];
}
Last edited on
Either use a data structure to ensure they are kept together as @Zapshe has pointed out, or make sure that when performing a shuffle, both datas are shuffled at the same time.

Also, while(!inFile.eof()) isn't a good way to read things. It will read until the end of the stream, but that doesn't mean it knows the next thing it grabs is the end of the stream - that's why you are getting an arbitrary value.

It's better to do this:

1
2
3
while(inFile >> values[count] >> cards[count]) {
  count++;
}
Either:
- use a struct (as per @zapshe)
or
- put them in parallel arrays and shuffle (in a separate array) the INDICES 0, 1, 2, ... , N-1, where N is the number of cards
or
- combine the two
Topic archived. No new replies allowed.