Problem with multidimensional string arrays.

closed account (1w64jE8b)
I'm trying to read information from a file, and it's working for the most part.

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
	//open file
	ifstream myfile;
	myfile.open(argv[1], ios::in);

	//Get the number of accusers
	int number_of_accusers;
	myfile >> number_of_accusers;
	cout << "Number of accusers: " << number_of_accusers << endl;

	string* accusers = new string[number_of_accusers];
	int* number_of_accusations = new int [number_of_accusers];
	string** accusations = new string* [number_of_accusers];

	//Get each accuser
	for(int i = 0; i < number_of_accusers; i++)
	{
		myfile >> accusers[i];
		cout << "Accuser " << i << ": "<< accusers[i] << endl;

		//Get the number of accusations
		myfile >> number_of_accusations[i];
		cout << "Number of accusations: " << number_of_accusations[i] << endl;

		//Get each accusation
		for (int j = 0; j < number_of_accusations[i]; j++)
		{
			accusations[i] = new string[number_of_accusations[i]];
			myfile >> accusations[i][j];
			cout << "Accusation " << j << ": " << accusations[i][j] << endl;
		}
	}

	cout << endl;
	
	//Get each accuser
	for(int i = 0; i < number_of_accusers; i++)
	{
		cout << "Accuser " << i << ": "<< accusers[i] << endl;

		//Get the number of accusations
		cout << "Number of accusations: " << number_of_accusations[i] << endl;

		//Get each accusation
		for (int j = 0; j < number_of_accusations[i]; j++)
		{
			cout << "Accusation " << j << ": " << accusations[i][j] << endl;
		}
	}

	myfile.close();

	delete [] accusers;
	delete [] number_of_accusations;
	delete [] accusations;

	cin.get();

	return 0;
}


The problem I'm having is with the variable accusations, which is an array of string arrays. Here is the file that the program reads:

5
Stephen	1
Tommaso
Tommaso	1
Galileo
Isaac	1
Tommaso
Galileo	1
Tommaso
George	2
Isaac
Stephen


The first number is the number of accusers. Then the name of each accuser followers, along with the number of accusations they make, and finally each accusation. Here is the output I'm getting:

Number of accusers: 5
Accuser 0: Stephen
Number of accusations: 1
Accusation 0: Tommaso
Accuser 1: Tommaso
Number of accusations: 1
Accusation 0: Galileo
Accuser 2: Isaac
Number of accusations: 1
Accusation 0: Tommaso
Accuser 3: Galileo
Number of accusations: 1
Accusation 0: Tommaso
Accuser 4: George
Number of accusations: 2
Accusation 0: Isaac
Accusation 1: Stephen

Accuser 0: Stephen
Number of accusations: 1
Accusation 0: Tommaso
Accuser 1: Tommaso
Number of accusations: 1
Accusation 0: Galileo
Accuser 2: Isaac
Number of accusations: 1
Accusation 0: Tommaso
Accuser 3: Galileo
Number of accusations: 1
Accusation 0: Tommaso
Accuser 4: George
Number of accusations: 2
Accusation 0:
Accusation 1: Stephen


The program loads the data fine, but when I read the information back to make sure that the variables are set correctly, George's first accusation is missing. As it turns out, whenever more than one accusation is made, only the last one is saved. I'm scratching my head over this problem, so if anyone has any ideas, please let me know!

By the way, I'm working on this puzzle from facebook:
http://www.facebook.com/careers/puzzles.php?puzzle_id=20
you are allocating memory to this index multiple times. so if its 2, the old memory will be overwritten.
 
accusations[i] = new string[number_of_accusations[i]];


so the next time you allocate memory, you will put data in:

accusations[i][1] and accusations[i][0] will have nothing.
Edit:

why are you creating mess with strings.. use vector or list. after 10 days, you yourself will not be able to understand this.
closed account (1w64jE8b)
Thanks for the help. I just had to move that one line out of the for loop and now it works perfectly!
Objectify!

A simple way to improve: Don't allocate multiple buffers with the intent that the buffers will work together. Instead put all the data you need in a struct and then allocate the struct:

1
2
3
4
5
6
7
8
9
struct Accusations
{
  string accuser;
  vector<string> accusations;
};

//...

vector<Accusations> allaccusations;
Topic archived. No new replies allowed.