Cpp 2D struct Array

okay.. i made a struct that with 3 data types (1) string charName (2) float CharHealth (3) int charExp.

the program is then supposed to read a file with information and place it into the struct array

example of information

Alice 23.4 3210
Xander 45.3 1110
Bernard 12.9 2024
Yanni 23.7 1098
Craw 50.5 980
Zack 11.9 1024

i have been able to only place the names column down but i constantly have problems with the other 2 columns that should have the float data and the other integar data

when i try to print out from column down i get the string data perfectly but the others keep getting errors. have searched around for quiet sometime and i still cant find a solution to this . What am i doing wrong?

AliceXanderBernardYanniCrawZack
1.96741e-03807.16531e-0301.12104e-0446.78168e-0399.80909e-045
1.4013e-04501.12104e-0446.77467e-0391.06515e-0380y


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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct charData
{
	string charName;
	float charHealth;
	int charExp;
};

void charHE();
void charNameBubbleSort();

int main()
{
	const int NUM_COLS = 3;
	int NUM_ROWS = 6;
	int charNumber = 0;
	int userInput;
	int loop = 0;
	int i,j;
	string line;
	
	ifstream myIn;    // Declare read file stream
	myIn.open("party.dat"); // Open file
	
	// While statement used to count the number of lines
    
	struct charData charArray[NUM_ROWS][NUM_COLS];
	
	while(!myIn.eof())
	{
		for ( j = 0; j <  NUM_COLS ; j++)
		{
			for ( i = 0; i < NUM_ROWS ; i++)
			{
				 myIn >> charArray[i][j].charName;
				 
				 myIn >>  charArray[i][j].charHealth;
				 myIn >>  charArray[i][j].charExp;
			
			}
			
			charNumber++;
				
		}
	
	}
	cout << charArray[0][0].charName;
	cout << charArray[1][0].charName;
	cout << charArray[2][0].charName;
	cout << charArray[3][0].charName;
	cout << charArray[4][0].charName;
	cout << charArray[5][0].charName;
	cout << "" <<endl;
	
	cout << charArray[0][1].charHealth;
	cout << charArray[1][1].charHealth;
	cout << charArray[2][1].charHealth;
	cout << charArray[3][1].charHealth;
	cout << charArray[4][1].charHealth;
	cout << charArray[5][1].charHealth;
	
	cout << "" <<endl;
	
	cout << charArray[0][2].charHealth;
	cout << charArray[1][2].charHealth;
	cout << charArray[2][2].charHealth;
	cout << charArray[3][2].charHealth;
	cout << charArray[4][2].charHealth;
	cout << charArray[5][2].charHealth;
	
 }
Last edited on
1
2
3
4
5
6
7
// Notice how this struct defines a row in-file?
struct charData
{
	string charName;
	float charHealth;
	int charExp;
};


Your for loop reads something that it shouldn't read ;) remove 1 for loop and something else to get it working

Why you are accessing charArray[row][column].something instead of charArray[row].property?
forgive me if i say anything dumb cause am a noob XD

if i remove the for loop then i wont be able to access the whole 2D array to place the information am reading

and am not really sure what u mean by the second question... sorry :(
Your struct charData can hold the complete line so there is no need for a 2d array.
I would do it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct charData charArray[NUM_ROWS];

  int row = 0;
  while (myIn)
  {
    myIn >> charArray[row].charName >> charArray[row].charHealth >> charArray[row].charExp;
    row++;
  }
  
  for (row = 0; row < NUM_ROWS; row++)
  {
    cout << charArray[row].charName << '\t' << charArray[row].charHealth << '\t';
    cout << charArray[row].charExp << '\n';
  }


Output:
1
2
3
4
5
6
Alice   23.4    3210
Xander  45.3    1110
Bernard 12.9    2024
Yanni   23.7    1098
Craw    50.5    980
Zack    11.9    1024
Topic archived. No new replies allowed.