Input arguments with C++


 
Thread Tools Search this Thread
Top Forums Programming Input arguments with C++
# 8  
Old 01-16-2013
That's what the namespace is for, it stores the data outside of main(). Once you do #include "ns.h" like in my code, you can use options::x wherever you need it, including class constructors.

Be sure to declare variables inside ns.h as extern, then declare them properly in one and only one file like I've shown you in my last post. The same goes for functions -- don't define their internals in the .h file. When you do it this way, you'll get the same variables available in all files across your program.
# 9  
Old 01-18-2013
Basically in my program I have main() that uses getopt_long to process the values passed by the user.

Suppose in main() the user can pass the following arguments

Code:
      {"interp-p",      required_argument, 0,  6},
       {"varp",      required_argument, 0,  7},
     {"ifbase",  required_argument, 0,  8},
     {"iftdat",  required_argument, 0,  9},
     {"ifmodl",  required_argument, 0,  10},
      {"ifpopl",  required_argument, 0,  11},
      {"ofmodl",  required_argument, 0,  12},
      {"ofpopl",  required_argument, 0,  13},
      {"ofexpl",  required_argument, 0,  14},
      {"nlayers", required_argument, 0,  15},
      {"ni",      required_argument, 0,  16},
      {"nxp",     required_argument, 0,  17},
      {"nzp",     required_argument, 0,  18},
      {"nxs",     optional_argument, 0,  19},
      {"nzs",     optional_argument, 0,  20},
      {"ngenr",   optional_argument, 0,  21},
      {"nmodl",   optional_argument, 0,  22},
      {"objv",    optional_argument, 0,  23},
      {"genitor", optional_argument, 0,  24},
      {"chrmrep", optional_argument, 0,  25},
      {"elitsel", optional_argument, 0,  27},
      {"msel",    optional_argument, 0,  26},
      {"mcrs",    optional_argument, 0,  28},
      {"rsel",    required_argument, 0,  29},
      {"rcrs",    optional_argument, 0,  30},
      {"rmut",    optional_argument, 0,  31},
      {"hdwvs",   optional_argument, 0,  32},
      {"tstep",   optional_argument, 0,  33},
      {"intgm",   optional_argument, 0,  34},
      {"dangl",   optional_argument, 0,  35},
      {"textr",   optional_argument, 0,  36},
      {"sigma",   optional_argument, 0,  37},
      {"colour",        no_argument, 0,  33},
      {"lglvl",   optional_argument, 0,  34},
      {"brief",         no_argument, 0, 'b'},
      {"detailed",      no_argument, 0, 'd'},
      {"examples",      no_argument, 0, 'e'},
      {"help",          no_argument, 0, 'h'},

Then I define an object T as follows

Code:
Tomog  T;

Somehow I need to have object T know about the values for

Code:
--ifbase
--iftdat
--nxp
--nzp
--varp
--interp-p

So I need to come up with a good scheme to do this
# 10  
Old 01-18-2013
I repeat. That is what the namespace is for. You can use them from anywhere at all.

It's like global variables, in that there's only one of them that you can access from anywhere -- you do not need to pass around arrays or arguments to get your data. But it's declared like a class and can contain functions, so you can keep it nicely organized just like a class.

You see me use them in main, since that's where argv[] comes from. Nothing stops anything else from reading it.

Code:
class myclass
{
public:
        // options::x was set by config::parse(), called by main().
        // We can use it directly, no need to pass anything anywhere.
        myclass() { x = options::x; }
        int x;
};

Code:
int main(int argc, char *argv[])
{
       options::parse(argc, argv);
       myclass something;
}

// These lines should only be in one file, but the functions
// and variables they instantiate can be used globally.
int options::x=-1, options::y=-1, options::z=-1;

bool options::parse(int argc, char *argv[])
{
        x=5;
        y=6;
        z=7;
}


Last edited by Corona688; 01-18-2013 at 01:33 PM..
# 11  
Old 01-18-2013
Does this mean that I can set x, y and z from argv?

Code:
// These lines should only be in one file, but the functions
// and variables they instantiate can be used globally.
int options::x=-1, options::y=-1, options::z=-1;

bool options::parse(int argc, char *argv[])
{
        x = argv[0];
        y = argv[1];
        z = argv[2];
}

# 12  
Old 01-18-2013
Quote:
Originally Posted by kristinu
Does this mean that I can set x, y and z from argv?
Yes. Now you're getting it. Smilie
# 13  
Old 01-18-2013
And I suppose I can use getopt_long in
Code:
option::parse

# 14  
Old 01-18-2013
Yes. Anything you can do in main() you can do in option::parse()
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass arguments based on input file?

This script is running some exe file we are passing three argumnet below custome key word Want to update script based on input files every time it will take argument from input file below is the input files should take this input put it into the script. k.ksh cd /u/kali/temp ... (8 Replies)
Discussion started by: Kalia
8 Replies

2. Shell Programming and Scripting

How to avoid "Too many arguments" error, when passing a long String literal as input to a command?

Hi, I am using awk here. Inside an awk script, I have a variable which contains a very long XML data in string format (500kb). I want to pass this data (as argument) to curl command using system function. But getting Too many arguments error due to length of string data(payloadBlock). I... (4 Replies)
Discussion started by: cool.aquarian
4 Replies

3. Homework & Coursework Questions

Removing punctuations from file input or standard input

Just started learning Unix and received my first assignment recently. We haven't learned many commands and honestly, I'm stumped. I'd like to receive assistance/guidance/hints. 1. The problem statement, all variables and given/known data: How do I write a shell script that takes in a file or... (4 Replies)
Discussion started by: fozilla
4 Replies

4. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

5. Shell Programming and Scripting

[Solved] Read and validate input arguments

Hi, I need to get input arguments, as well as validate them. This is how I'm reading them: #!/bin/bash args="$@" # save arguments to variable ## Read input arguments, if so while ; do case $1 in -v | --verbose ) verbose=true;; -z | --gzip ) compression="gz";; ... (3 Replies)
Discussion started by: AlbertGM
3 Replies

6. Shell Programming and Scripting

function terminating if i give input as space or no input and enter

HI i have written a script to ask input from the user. this script should promote the user for y/n input. if user enters anyother input then y/n the script promotes him again. this below code is working fine for all the cases. except for space and enter " if i give space and enter it is... (2 Replies)
Discussion started by: BHASKARREDDY006
2 Replies

7. Shell Programming and Scripting

Script to delete files with an input for directories and an input for path/file

Hello, I'm trying to figure out how best to approach this script, and I have very little experience, so I could use all the help I can get. :wall: I regularly need to delete files from many directories. A file with the same name may exist any number of times in different subdirectories.... (3 Replies)
Discussion started by: *ShadowCat*
3 Replies

8. Shell Programming and Scripting

grep with two arguments to arguments to surch for

Hello, is it possible to give grep two documents to surche for? like grep "test" /home/one.txt AND /home/two.txt ? thanks (1 Reply)
Discussion started by: Cybertron
1 Replies

9. Shell Programming and Scripting

Accepting user input and arguments in PERL

Hi All, Can we pass arguments while calling the perl script and as well as ask user input during execution of the script? My program is as below: I am passing arg1 and arg2 as argements to test.pl ]./test.pl arg1 arg2 Inside the test.pl I have : print "Do you want a name ? (y/n) : ";... (2 Replies)
Discussion started by: jisha
2 Replies

10. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies
Login or Register to Ask a Question