> I need some help in C++?

I need some help in C++?

Posted at: 2014-12-18 
define SIZE

in the main function, i ask the user for a number n, then i want to use that n for SIZE. How can that be done if it can be done?

i tried SIZE= n. but its giving error.

Don't confuse the #define directive with a variable.

The #define statement only has meaning for the preprocessor.

Anything you define for your chosen identifier, such as SIZE , simply replaces all occurances of SIZE in the source code before compilation.

It is as if you had typed the definition in the code to begin with.

Then the #define statement is replaced with a blank line.

Then the source code is compiled. So you see there is no SIZE variable in your code to assign when the program is run.

Read up on the Preproccessor to get a clearer understanding.

Generally, assigning value for the global variable is considered bad form. It is better to use a constant:

const int size = n;

You can't modify a #define name at run-time, when the program is running. The #define is a pre-processor statement. So it only applies during pre-processing, just before compilation into an executable program. LONG BEFORE the program is running.

If you want a run-time modifiable global scope variable to be available then you need to define it that way:

#include

using namespace std;

int SIZE;

int main( ) {

? ? cin >> SIZE;

? ? cout << SIZE << endl;

? ? return 0;

}

It's not normal practice to use upper case for such a variable. But I kept it upper case so that it "looks like" what you did. You will be able to enter a value for SIZE, this way. And it's value will be available globally in your program. So it's about as close as it gets.

#define SIZE

in the main function, i ask the user for a number n, then i want to use that n for SIZE. How can that be done if it can be done?

i tried SIZE= n. but its giving error.