views:

450

answers:

5

Hi, Is there a way to define a global variable by user input? Lets say I use

#include...

#define N 12
double array[N][N]; 

void main();...

But I would like the user to be able to choose what N is. Do I have to have N as a local variable or is there a way around this(without macros)? I've a pretty small program but with a lot of different variables that need the N value.

Alternatively, is there a way I could send a group of variables into a function without having to explicitly write them out every time.

for example

myfunction(var1,var2,var3...)

and instead write something like

myfunction(Allvariables)

Thanks a lot for Your answers! This is a great forum.

+1  A: 

No, that can't be done this way. You need to use dynamic (runtime) memory allocation (new[]). To perform static (compile-time) memory allocation the compiler needs to know the memory block size at compile time.

sharptooth
+2  A: 

1/ Yes but you need dynamic memory allocation. The program parameters are passed as argc and argv to the main function

int main(int argc, char **argv)

argc is the number of parameters argv is the array of null terminated strings representing these arguments

argv[0] is the program itself.

2/You can either use variadic function va_start & the like, or functions overriding, or group your data in a structure and pass that to the function

Edouard A.
Thanks, I will check in the structure alternative , that is probably what I need here!
mrbuxley
+5  A: 
int* data;

int main()
{
    int n;
    // get n from the user.

    data = new int[n];

    // use data.

    .
    .

    delete[] data;
}

or just forget pointers for ever and use vector!

std::vector<int> data;

data.push_back(55);

// just push_back data!

=======================================================================

EDIT :: If you want to use Edouard A. way :)

#include <iostream>
#include <sstream>
#include <vector>

int main(int argc, char* argv[])
{
    std::vector<double>::size_type dataSize = 0;
    std::stringstream convertor(argv[1]);
    {
     if(argc > 1)
     {
      convertor >> dataSize;

      if(convertor.fail() == true)
      {
       // do whatever you want here in case
       // the user didn't input a number.
      }
     }
    }
    std::vector<double> data(dataSize);

    // use the vector here.

    return 0;
}

I prefere to use lexical_cast in this case, but I am not sure if you have Boost.

#include <iostream>
#include <vector>
#include <boost/lexical_cast.hpp>

int main(int argc, char* argv[])
{
    typedef std::vector<double>::size_type vectorSize;

    if(argc < 2)
    {
     // err! The user didn't input anything.
    }

    vectorSize dataSize = boost::lexical_cast<vectorSize>(argv[1]);
    std::vector<double> data(dataSize);

    // use the vector here.

    return 0;
}
AraK
Thanks man! I ended up using vectors after all!And worked like a charm!
mrbuxley
+1  A: 

I'm not really sure what you're trying to do with myFunction but it sounds like you want to look at either creating a struct or pass a std::vector

Salgar
thanks for the links, they are helpful and yes, I want to create a Struct..
mrbuxley
A: 

Make a class (or struct) AllVariables and pass that in.

You don't say whether you want N defined at run time or compile time. If you want it defined at compile time, you can define N as a compiler command line arguement.

Les