Jump to content

c++ int in grid and switch?

prolemur

I want to let the user select a difficulty from 1-3 and depending on what this number is it will change the length.

However it doesn't allow the use of a non-constant variable in an array or a constant variable in a switch.

#include <iostream>#include <conio.h>using namespace std; int difficulty;int length = 10;int grid[length][length];int main() {	cout<<"Choose a difficulty."<<endl;	cout<<"1. Easy [10x10]"<<endl;	cout<<"2. Medium [15x15]"<<endl;	cout<<"3. Hard [20x20]"<<endl;	cin>> difficulty;	switch (difficulty) {	case 1: length = 10;		break;	case 2: length = 15;		break;	case 3: length = 20;		break;	default: length = 10;		break;	}	cout<<length<<endl;	getch();	return 0;}
Link to comment
Share on other sites

Link to post
Share on other sites

Try something like this.. http://www.cplusplus.com/forum/articles/416/
 

 

#include <iostream>

#include <conio.h>
 
using namespace std;
 
int difficulty;
int length = 10;
int *array;
 
int main() {
 
array = new int[length];
array = new int[length];
array[1, 1] = 2;
array[1, 2] = 3;
 
cout << array[1, 1] << endl;
cout << array[1, 2] << endl;
 
delete[] array;
_getch();
return 0;
}

CPU: i7 4770k | GPU: Sapphire 290 Tri-X OC | RAM: Corsair Vengeance LP 2x8GB | MTB: GA-Z87X-UD5HCOOLER: Noctua NH-D14 | PSU: Corsair 760i | CASE: Corsair 550D | DISPLAY:  BenQ XL2420TE


Firestrike scores - Graphics: 10781 Physics: 9448 Combined: 4289


"Nvidia, Fuck you" - Linus Torvald

Link to comment
Share on other sites

Link to post
Share on other sites

I can't remember what cin assigns to a variable now, but it may be creating an array of input, so perhaps the character you wish to use the switch on is actually at place difficulty[0]?

Link to comment
Share on other sites

Link to post
Share on other sites

I can't remember what cin assigns to a variable now, but it may be creating an array of input, so perhaps the character you wish to use the switch on is actually at place difficulty[0]?

This is wrong..

CPU: i7 4770k | GPU: Sapphire 290 Tri-X OC | RAM: Corsair Vengeance LP 2x8GB | MTB: GA-Z87X-UD5HCOOLER: Noctua NH-D14 | PSU: Corsair 760i | CASE: Corsair 550D | DISPLAY:  BenQ XL2420TE


Firestrike scores - Graphics: 10781 Physics: 9448 Combined: 4289


"Nvidia, Fuck you" - Linus Torvald

Link to comment
Share on other sites

Link to post
Share on other sites

Global variables are initialized by the compiler and they exist in a certain section of the executable (simplification). Hence, you can only initialize the variables whose lengths are known.

If what you are trying to do was possible, with which size would the matrix be initialized? 10? And then when length changed, what would happen?

 

So, the only way to initialize such matrix is during runtime, in the heap or in the stack.

Since the stack is not designed to hold big objects and the matrix is supposed to be global, it is not a good idea to allocate it there.

 

Heap method:

You declare the array as

int **grid;

and then, after knowing the size you want, you have two options: malloc or new.

Since you are using C++, let us take advantage of new:

grid = new int*[length];

foreach i < length

  grid = new int[length];

 

And now you can initialize and access the matrix with grid[x][y].

 

When allocating in the heap do not forget to delete (or free, if you used malloc) the memory to prevent memory leaks.

 

My version (I use linux, so I eliminated the getch at the end):

#include <iostream>using namespace std; int difficulty;int length = 10;int **grid;int main() {	cout<<"Choose a difficulty."<<endl;	cout<<"1. Easy [10x10]"<<endl;	cout<<"2. Medium [15x15]"<<endl;	cout<<"3. Hard [20x20]"<<endl;	cin>> difficulty;	switch (difficulty) {	case 1: length = 10;		break;	case 2: length = 15;		break;	case 3: length = 20;		break;	default: length = 10;		break;	}	cout<<length<<endl;	grid = new int*[length];        for(int i = 0; i < length; i++)		grid[i] = new int[length];	grid[0][length-1] = 5;	for(int i = 0; i < length; i++)		delete grid[i];	delete grid;	return 0;}
Link to comment
Share on other sites

Link to post
Share on other sites

 

Global variables are initialized by the compiler and they exist in a certain section of the executable (simplification). Hence, you can only initialize the variables whose lengths are known.

If what you are trying to do was possible, with which size would the matrix be initialized? 10? And then when length changed, what would happen?

 

So, the only way to initialize such matrix is during runtime, in the heap or in the stack.

Since the stack is not designed to hold big objects and the matrix is supposed to be global, it is not a good idea to allocate it there.

 

Heap method:

You declare the array as

int **grid;

and then, after knowing the size you want, you have two options: malloc or new.

Since you are using C++, let us take advantage of new:

grid = new int*[length];

foreach i < length

  grid = new int[length];

 

And now you can initialize and access the matrix with grid[x][y].

 

When allocating in the heap do not forget to delete (or free, if you used malloc) the memory to prevent memory leaks.

 

My version (I use linux, so I eliminated the getch at the end):

#include <iostream>using namespace std; int difficulty;int length = 10;int **grid;int main() {	cout<<"Choose a difficulty."<<endl;	cout<<"1. Easy [10x10]"<<endl;	cout<<"2. Medium [15x15]"<<endl;	cout<<"3. Hard [20x20]"<<endl;	cin>> difficulty;	switch (difficulty) {	case 1: length = 10;		break;	case 2: length = 15;		break;	case 3: length = 20;		break;	default: length = 10;		break;	}	cout<<length<<endl;	grid = new int*[length];        for(int i = 0; i < length; i++)		grid[i] = new int[length];	grid[0][length-1] = 5;	for(int i = 0; i < length; i++)		delete grid[i];	delete grid;	return 0;}

Thanks this is all over my head right now, but I'll try to understand it :)

The getch at the end was just so it stayed on screen :)

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks this is all over my head right now, but I'll try to understand it :)

The getch at the end was just so it stayed on screen :)

Use this instead

cout << "\nPress ENTER to quit" << endl;cin.ignore(numeric_limits<streamsize>::max(),'\n');cin.get();return 0;

CPU: i7 4770k | GPU: Sapphire 290 Tri-X OC | RAM: Corsair Vengeance LP 2x8GB | MTB: GA-Z87X-UD5HCOOLER: Noctua NH-D14 | PSU: Corsair 760i | CASE: Corsair 550D | DISPLAY:  BenQ XL2420TE


Firestrike scores - Graphics: 10781 Physics: 9448 Combined: 4289


"Nvidia, Fuck you" - Linus Torvald

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks this is all over my head right now, but I'll try to understand it :)

The getch at the end was just so it stayed on screen :)

 

Memory can be a tricky thing....to put it in a different perspective, there are 3 types of memory.  Global types, Local types, and Dynamically allocated types.

 

*I am using windows because I am more familiar with the analogy

Global types:

Global types, are basically variables that can be accessed from anywhere in your code (no matter where you are in a function).  When you open an executable file, before any code is run Windows will set aside enough ram for global variables.  This is why

char hugeRam[268435456]; //256MB if I did my math rightint main() {return 0;}

This simple program that does nothing will consume 256 MB of ram...even though it does nothing.  Since windows needs to be able to allocate that memory before any code is run the numbers must be known when you compile your code.  That is why the number has to be constant.

 

Local Variables:

Local variables are similar to global variables, but are only made when a function is initially called...instead of creating the memory when Windows first launches the program, the memory is now created when you call the function....but still no code is run until the memory is created.  This is also why local variables need a constant value...as when you compile the code, the compiler tells how much memory is needed for each function.

 

Dynamically Allocated Variables:

This is @MikeD's solution, which I like.  Basically memory is created while the program is running....so you can ask for 1024 bytes of room if you want it.  Windows basically just tacks on more memory to the end of your program when you call new or malloc (Like what Mike said, this is called the heap).,  The heap is basically all the data that you the programmer has asked for during run-time...so your code is running, which is why you can ask for how much ever you want in memory.  The problem is if you keep asking for memory without giving back the memory, your program will keep increasing in size...as Windows won't know you are done with the memory and just keep adding on more.

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Memory can be a tricky thing....to put it in a different perspective, there are 3 types of memory.  Global types, Local types, and Dynamically allocated types.

 

*I am using windows because I am more familiar with the analogy

Global types:

Global types, are basically variables that can be accessed from anywhere in your code (no matter where you are in a function).  When you open an executable file, before any code is run Windows will set aside enough ram for global variables.  This is why

char hugeRam[268435456]; //256MB if I did my math rightint main() {return 0;}

This simple program that does nothing will consume 256 MB of ram...even though it does nothing.  Since windows needs to be able to allocate that memory before any code is run the numbers must be known when you compile your code.  That is why the number has to be constant.

 

Local Variables:

Local variables are similar to global variables, but are only made when a function is initially called...instead of creating the memory when Windows first launches the program, the memory is now created when you call the function....but still no code is run until the memory is created.  This is also why local variables need a constant value...as when you compile the code, the compiler tells how much memory is needed for each function.

 

Dynamically Allocated Variables:

This is @MikeD's solution, which I like.  Basically memory is created while the program is running....so you can ask for 1024 bytes of room if you want it.  Windows basically just tacks on more memory to the end of your program when you call new or malloc (Like what Mike said, this is called the heap).,  The heap is basically all the data that you the programmer has asked for during run-time...so your code is running, which is why you can ask for how much ever you want in memory.  The problem is if you keep asking for memory without giving back the memory, your program will keep increasing in size...as Windows won't know you are done with the memory and just keep adding on more.

Thanks, I understood that :)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×