error with #define & array output

last night i was able to display data from a text file in rows and columns using this 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
#include <vector>
#include <iostream>
#include <string>
#include <fstream>

using namespace std;
#define Rows = 4
#define Cols = 3

int main()
{
	fstream infile;
	infile.open("numbers.txt");

	int Matrix[Rows][Cols];

	//Input
	for (int i = 0; i < Cols; i++)
	for (int j = 0; j < Rows; j++)
		infile >> Matrix[j][i];

	//Output
	for (int i = 0; i < Cols; i++)
	{
		for (int j = 0; j < Rows; j++)
			std::cout << Matrix[j][i] << "\t";
		std::cout << "\n";
	}

	return 0;
}




today when i tried to run it to print and turn into a class, it had compile errors in the underlined areas.

it would be greatly appreciated if some one could tell me why the error is occurring or an alternative but similar way i could display it in the same fashion.


7
8
#define Rows 4
#define Cols 3 
if you know the reasoning behind the error, would you mind explaining it?

I realize where the issue is occurring.

You're not helping me with homework as I've already turned it in with mistakes. When I run the last successful build, it still works so i am really confused as to what happened.
Last edited on
#define takes everything after the name given and places that whereever it finds the name in your code. So with the way you did it before, this is what your code would have looked like to the compiler:

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int main()
{
	fstream infile;
	infile.open("numbers.txt");

	int Matrix[= 4][= 3];

	//Input
	for (int i = 0; i <= 3; i++)
	for (int j = 0; j <= 4; j++)
		infile >> Matrix[j][i];

	...

	return 0;
}
Okay thank you. summer class got ahead of me fast. going to look this one up more
Topic archived. No new replies allowed.