Saving 2dimensional Array in txt

Hey guys,
so I am trying to write an Array to an txt but I have a problem.
The array values of Array[0][1] and Array[1][0] are getting the same value after I put this output into my programm.
The same is happening to the other values of the array.
Array[i][0]=[Array[0][i], so I think there is something wrong happening in this part. Because I made an output of the Array before I write it into the txt data and at this point it is still right. It just turns wrong after the programm tries to write it into the txt.
I hope somebody can help, thanks in advance
1
2
3
4
5
6
7
8
9
#include <fstream>	
...
std::ofstream a_file("RandomNR.txt");
	for (i = 0; i <= 5; i++){

		a_file<< "First " << Array[i][0] << " Second "<< Array[i][1]<< std::endl; //here is something wrong

	}
	a_file.close();  
Last edited on
Can we see what Array is? You say the issue is that Array[i][0] is the same as Array[o][i], but you only save Array[i][0] and Array[i][1], which doesn't make sense.

Do you know how to iterate a 2D array properly?
I'd need to have a look at your array. Double check array declaration and that you are not exceeding array bounds. The following worked for me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <fstream>

int main()
{
	std::ofstream a_file("RandomNR.txt");
	int Array[5][2];

	for(int i = 0; i < 5; i++)
		Array[i][0] = i + 1;

	for(int i = 0; i < 5; i++)
		Array[i][1] = i + 6;

	for(int i = 0; i < 5; i++)
	{

		a_file << "Firs " << Array[i][0] << " Second "<< Array[i][1]<< std::endl;

	}
	a_file.close();
}
Last edited on
Here ist the full code:
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
#include <iostream>     // std::cout
#include <algorithm>    // std::move_backward
#include <array>        // std::array
#include <random>       // std::default_random_engine
#include <chrono>       // std::chrono::system_clock
#include <fstream>		//ein ausgabe in txt datei


int i, j, k;
int Array[6][1];


int main() {
	std::array<int, 10> NrArray{ 403, 301, 104, 315, 123, 172, 208, 309, 410, 121 };

	unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();

	shuffle(NrArray.begin(), NrArray.end(), std::default_random_engine(seed));   //til here the programm shuffles the ArrayNR
	for (i = 0; i <= 5; i++){
		k = NrArray[i];     //here the programm starts to pick numbers from the array and fills the Array "Array" with it(next time i'll pick another name)^^

		for (j = 4; j>0; j--){
			if (k> j * 100){
				Array[i][0] = k - j * 100;
				Array[i][1] = j;
				j = 1;

			}

		}
		printf("Karte %d\n", Array[i][0]);
		printf("%d\n", Array[i][1]);              //the output is correct
	}

	//output txt
	std::ofstream a_file("RandomHand.txt");
	for (i = 0; i <= 5; i++){

		a_file << "Hoehe " << Array[i][0] << " Tiefe " << Array[i][1] << std::endl;
		printf("Karte %d\n", Array[i][0]);
		printf("%d\n", Array[i][1]);           //the output changed as mentioned in OP Array[i][0]=[Array[0][i]
	}
	a_file.close();
	return 0;

}
Last edited on
Try changing line 10 to:

int Array[6][2];

Let me know if that matches your expected output.
omg thx^^
that was really a noob mistake but only through mistakes..
Of course, glad I could help.
Topic archived. No new replies allowed.