About usage of command line arguments in another function defined out of main func

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shylaja1
    New Member
    • Oct 2008
    • 5

    About usage of command line arguments in another function defined out of main func

    Hi,

    How do i use command line arguments in the function which is called in main function and defined outside the main function.Even though,i can pass them as arguments..but if i do that...it makes me to pass many arguments.So,pl ease let me know how do i fetch the cmnd line arguments in another function without passing them as parameters to the function.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by shylaja1
    Hi,

    How do i use command line arguments in the function which is called in main function and defined outside the main function.Even though,i can pass them as arguments..but if i do that...it makes me to pass many arguments.So,pl ease let me know how do i fetch the cmnd line arguments in another function without passing them as parameters to the function.
    You could implement a global singleton object that holds a copy of the command
    line arguments. The first thing a main() function should do is create such a single
    instance. All other functions can access the instance for the value(s) of the
    command line arguments.

    kind regards,

    Jos

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Originally posted by shylaja1
      How do i use command line arguments in the function which is called in main function and defined outside the main function.Even though,i can pass them as arguments..but if i do that...it makes me to pass many arguments.So,pl ease let me know how do i fetch the cmnd line arguments in another function without passing them as parameters to the function.
      The prototype for the main function is
      Code:
      int main( int argc, char *argv[]);
      You can pass argc and argv to any function called from main. That's only two parameters to pass.

      Comment

      • shylaja1
        New Member
        • Oct 2008
        • 5

        #4
        Originally posted by donbock
        The prototype for the main function is
        Code:
        int main( int argc, char *argv[]);
        You can pass argc and argv to any function called from main. That's only two parameters to pass.
        Hi ,
        the compiler gives error telling that unknowv variable..argv in the function defined out of main or which is called out frm main...Plz can u implement a prgrm..with the help of pointers.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Originally posted by shylaja1
          the compiler gives error telling that unknowv variable..argv in the function defined out of main or which is called out frm main...Plz can u implement a prgrm..with the help of pointers.
          Please show me
          ... the function prototype for the function you're trying to pass the command line arguments to.
          ... your function main definition (the first line of the function; the one that looks like a prototype).
          ... the line from within main where you call that function and pass it the command line arguments.

          Make sure the prototype line occurs before the definition of the main function.

          Comment

          • shylaja1
            New Member
            • Oct 2008
            • 5

            #6
            Originally posted by donbock
            Please show me
            ... the function prototype for the function you're trying to pass the command line arguments to.
            ... your function main definition (the first line of the function; the one that looks like a prototype).
            ... the line from within main where you call that function and pass it the command line arguments.

            Make sure the prototype line occurs before the definition of the main function.
            prototype....
            void getRncIpAddress AndPort( SIpta &ipta_rnc,in t option,char **argv );

            Function definition....s me changes are made by me....
            void getRncIpAddress AndPort( SIpta &ipta_rnc,in t option,char **argv )
            {
            int ip1oct,ip2oct,i p3oct,ip4oct ;
            u32 udpport;
            cout<<"the function entered"<<endl;

            //cout<<"Please enter the RNC IP Address"<<endl;
            char dot;
            //cout<<*argv1<<e ndl;
            do
            {
            cout<<"while loop entered"<<endl;
            //cout<<"Valid Entry for Octets is between 0-255"<<endl;
            //cin>>ip1oct>>do t>>ip2oct>>dot> >ip3oct>>dot>>i p4oct;
            if( (option == 4) || (option == 8) || (option == 9) )

            {
            cout<<"if loop entered"<<endl;
            int Result;
            //char* value;
            //value = *(++argv);
            //cout<<value<<en dl;
            if (StringToInt(++ *argv), Result))
            {
            ip1oct = Result;
            cout << "The string value is " <<*(++*argv)< < " and the int value is " << Result << endl;

            }
            else
            {
            cout << "Number conversion failed" <<endl;
            }


            //ip1oct = int(*(++argv));
            dot = char(*(++argv)) ;
            ip2oct =int(*(++argv)) ;
            dot = char(*(++argv)) ;
            ip3oct =int(*(++argv)) ;
            dot = char(*(++argv)) ;
            ip4oct = int(*(++argv));
            udpport = u32(*(++argv));

            }
            /* if( ((int(*argv1)) == 5) || ((int(*argv1)) == 6) )
            {
            ip1oct = int(*(++argv));
            dot = char(*(++argv)) ;
            ip2oct =int(*(++argv)) ;
            dot = char(*(++argv)) ;
            ip3oct =int(*(++argv)) ;
            dot = char(*(++argv)) ;
            ip4oct = int(*(++argv));
            udpport = u32(*(++argv));
            }
            if( ((int(*argv1)) == 7) || ((int(*argv1)) == 11) )
            {
            ip1oct = int(*(++argv));
            dot = char(*(++argv)) ;
            ip2oct =int(*(++argv)) ;
            dot = char(*(++argv)) ;
            ip3oct =int(*(++argv)) ;
            dot = char(*(++argv)) ;
            ip4oct = int(*(++argv));
            udpport = u32(*(++argv));

            }*/
            }


            while ( !((ip1oct<=255) &&(ip2oct<=255) &&(ip3oct<=255) &&(ip4oct<=255) ));

            SAal2sIpAddress ipAddress;

            ipAddress.u.ipv 4Address.ipa[0]= (u8)ip1oct;
            ipAddress.u.ipv 4Address.ipa[1]= (u8)ip2oct;
            ipAddress.u.ipv 4Address.ipa[2]= (u8)ip3oct;
            ipAddress.u.ipv 4Address.ipa[3]= (u8)ip4oct;

            ipAddress.type = EAal2sIpAddress Type_ipv4;
            ipAddress.lengt h = 4;
            ipta_rnc.ipAddr ess = ipAddress;
            //cout <<"Please enter RNC UDP Port (49152-63135)"<<endl;
            //cin >> udpport ;
            while( udpport<49152 || udpport>63135 )
            {
            cout <<"Wrong RNC UDP Port Range-(49152-63135)"<<endl;
            cout <<"Please enter again RNC UDP Port with in range (49152-63135)"<<endl;
            //cin>>udpport;
            }

            ipta_rnc.udpPor t = udpport;


            }

            Comment

            • shylaja1
              New Member
              • Oct 2008
              • 5

              #7
              Originally posted by shylaja1
              prototype....
              void getRncIpAddress AndPort( SIpta &ipta_rnc,in t option,char **argv );

              Function definition....s me changes are made by me....
              void getRncIpAddress AndPort( SIpta &ipta_rnc,in t option,char **argv )
              {
              int ip1oct,ip2oct,i p3oct,ip4oct ;
              u32 udpport;
              cout<<"the function entered"<<endl;

              //cout<<"Please enter the RNC IP Address"<<endl;
              char dot;
              //cout<<*argv1<<e ndl;
              do
              {
              cout<<"while loop entered"<<endl;
              //cout<<"Valid Entry for Octets is between 0-255"<<endl;
              //cin>>ip1oct>>do t>>ip2oct>>dot> >ip3oct>>dot>>i p4oct;
              if( (option == 4) || (option == 8) || (option == 9) )

              {
              cout<<"if loop entered"<<endl;
              int Result;
              //char* value;
              //value = *(++argv);
              //cout<<value<<en dl;
              if (StringToInt(++ *argv), Result))
              {
              ip1oct = Result;
              cout << "The string value is " <<*(++*argv)< < " and the int value is " << Result << endl;

              }
              else
              {
              cout << "Number conversion failed" <<endl;
              }


              //ip1oct = int(*(++argv));
              dot = char(*(++argv)) ;
              ip2oct =int(*(++argv)) ;
              dot = char(*(++argv)) ;
              ip3oct =int(*(++argv)) ;
              dot = char(*(++argv)) ;
              ip4oct = int(*(++argv));
              udpport = u32(*(++argv));

              }
              /* if( ((int(*argv1)) == 5) || ((int(*argv1)) == 6) )
              {
              ip1oct = int(*(++argv));
              dot = char(*(++argv)) ;
              ip2oct =int(*(++argv)) ;
              dot = char(*(++argv)) ;
              ip3oct =int(*(++argv)) ;
              dot = char(*(++argv)) ;
              ip4oct = int(*(++argv));
              udpport = u32(*(++argv));
              }
              if( ((int(*argv1)) == 7) || ((int(*argv1)) == 11) )
              {
              ip1oct = int(*(++argv));
              dot = char(*(++argv)) ;
              ip2oct =int(*(++argv)) ;
              dot = char(*(++argv)) ;
              ip3oct =int(*(++argv)) ;
              dot = char(*(++argv)) ;
              ip4oct = int(*(++argv));
              udpport = u32(*(++argv));

              }*/
              }


              while ( !((ip1oct<=255) &&(ip2oct<=255) &&(ip3oct<=255) &&(ip4oct<=255) ));

              SAal2sIpAddress ipAddress;

              ipAddress.u.ipv 4Address.ipa[0]= (u8)ip1oct;
              ipAddress.u.ipv 4Address.ipa[1]= (u8)ip2oct;
              ipAddress.u.ipv 4Address.ipa[2]= (u8)ip3oct;
              ipAddress.u.ipv 4Address.ipa[3]= (u8)ip4oct;

              ipAddress.type = EAal2sIpAddress Type_ipv4;
              ipAddress.lengt h = 4;
              ipta_rnc.ipAddr ess = ipAddress;
              //cout <<"Please enter RNC UDP Port (49152-63135)"<<endl;
              //cin >> udpport ;
              while( udpport<49152 || udpport>63135 )
              {
              cout <<"Wrong RNC UDP Port Range-(49152-63135)"<<endl;
              cout <<"Please enter again RNC UDP Port with in range (49152-63135)"<<endl;
              //cin>>udpport;
              }

              ipta_rnc.udpPor t = udpport;


              }

              its very urgent...plz... i m trying it but i m nt able to...:(

              Comment

              • newb16
                Contributor
                • Jul 2008
                • 687

                #8
                Originally posted by shylaja1
                its very urgent...plz... i m trying it but i m nt able to...:(
                Compiles on my machine. What then?
                ip2oct =int(*(++argv)) ;
                I guess what it is supposed to do, but it clearly does in wrong. You advance argv to next argument string ( if it exist ), take first character of it (e.g. '1', 49 decimal and convert to unsigned char, 49?

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by shylaja1
                  its very urgent...plz... i m trying it but i m nt able to...:(
                  Rent A Coder

                  kind regards,

                  Jos

                  Comment

                  • donbock
                    Recognized Expert Top Contributor
                    • Mar 2008
                    • 2427

                    #10
                    Is this the first time you've tried to acquire and parse the command line arguments? If so, then you should concentrate on that portion of the code -- strip away everything else.

                    Comment

                    • shylaja1
                      New Member
                      • Oct 2008
                      • 5

                      #11
                      Originally posted by donbock
                      Is this the first time you've tried to acquire and parse the command line arguments? If so, then you should concentrate on that portion of the code -- strip away everything else.
                      thank u.....very much..:)

                      Comment

                      Working...