Printing individual values from 2d array

I've been working on a program where I'm taking data from a text file and putting it into a 2d array called str[4][4].
The issue is that later on I want to output individual values from str[4][4] based on two variables, but haven't found success. I can't find a way to make printf work, and when I use a dummy int value I get the message "cannot convert 'std::string' to 'int' in assignment. I feel like there should be an easy way round this but has been a while since I've used c++
The code for you:

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
#include <stdio.h>
#include <malloc.h>
using namespace std;

int main()
{
	FILE *fIn = fopen("input.txt", "r");
	int str[4][4];

	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			fscanf(fIn, "%d", &str[i][j]) ;
		}
	}

	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			printf("%d\t", str[i][j]) ;
		}
		printf("\n");
	}

	fclose(fIn);
	getchar();
	return 0;
}
Last edited on
Thanks, that's very helpful. However I need to manipulate some of the data into an equilateral triangle, based on what I did when using random integers instead of importing from a textfile I thus to change the print section to:
1
2
3
4
5
6
7
8
9
10
11
for (int i = 0; i < 4; i++)
	{
		
		for(i=20-3*j;i>0;--i)
		printf(" ");
		for (int j = 0; j <= i; j++)
		{
			printf("%d\t", str[i][j]) ;
		}
		printf("\n");
	}


However this has the issue that j isn't defined, and I haven't been able to solve using dummy variable to take the value of j.

Topic archived. No new replies allowed.