const int to declare an array of struct error

Hello all,

So basically I'm having a problem that I can't really locate info on via google. Basically I have a struct that I am trying to declare an array of. The size of the array is based on one of the command line arguments. This is the gist of what I am doing here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct Checker{
	int breakTime;
	int moneyInReg;
	int returnTime;
};


//that is before main, then within main I have:

const int numCheckers = atoi(argv[1]);

//and then I declare the struct array like so:

Checker registers[numCheckers];


it is telling me that numCheckers is an undeclared identifier. Why is this?

Thank you all for the help, I hope that was concise enough.

Cheers,
The Kvlt Kitty
I thought maybe making it a static const int would help, but I am still getting the same error. It seems like it's saying it's undeclared, meaning it's a scope issue, so could it be because I am declaring it within an if statement?

Here is the if statement it is within:

1
2
3
4
if(argc > 1){
	static const int numCheckers = atoi(argv[1]);
}
Last edited on
You need a dynamic array or vector

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
struct Checker{
	int breakTime = 0;
	int moneyInReg = 0;
	int returnTime = 0;
};

int main(int argc, char* argv[])
{
	//that is before main, then within main I have:

	const int numCheckers = atoi(argv[1]);

	//and then I declare the struct array like so:

	Checker *registers = new Checker[numCheckers];

	
	delete [] registers;

	cin.ignore();
	return 0;
}
Last edited on
Awesome, thank you for the help. Just curious, could you explain a bit more why this is?
Topic archived. No new replies allowed.