command line argument

command line argument

Post by L0DER on Sun Oct 12, 2008 2:44 pm
([msg=13605]see command line argument[/msg])

I tried to write program that start other programs by specific argument:
and somehow even when x = argc[1] it passing straight to the else.
Code: Select all
#include <cstdlib>
#include <iostream>
// \2 makes a simliy
int main(int argc, char *argv[])
{
   char *x;
        x = argv[1];
        std::cout << x << "\n"; \\ To see what x is
        std::cout << agrv[1] << "\n"; \\ To see what x is
               if(x)
{
               if (x == "now")
               {
                     std::cout << "in now if\n"; \\Debug-like
                     }
                     else
                     {
                         std::cout << "in now else\n";  \\Debug-like
                         }
}
          else
          {
               system("echo No arguments Inputed");
          }
            system("pause");
            return EXIT_SUCCESS;
}

ignore that now written twice. it's should be only one.
Image
L0DER
New User
New User
 
Posts: 26
Joined: Tue Apr 22, 2008 12:58 am
Blog: View Blog (0)


Re: command line argument

Post by BhaaL on Mon Oct 13, 2008 12:25 pm
([msg=13641]see Re: command line argument[/msg])

Apart from your bad syntax (you better copy+paste the code next time, too many mistakes with comments and mistyped words), you should check if argc is at least 2 before accessing argv[1]. Not passing an argument will most likely crash your application if you do that.
Other than that, x is a char*, aswell as argv[1]. You cannot compare those as if they were strings, you have to use strcmp() instead. Either that, or pass it into a std::string first.

Code: Select all
#include <iostream>

int main(int argc, char** argv) {
  if (argc > 1) {
    char *x = argv[1];
    if (!strcmp(x, "new")) std::cout << "Got 'new' as argument" << std::endl;
    else std::cout << "Got '" << x << "' as argument instead of 'new'" << std::endl;
  }
  else {
    std::cout << "No arguments passed" << std::endl;
  }
}
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: command line argument

Post by L0DER on Mon Oct 13, 2008 4:01 pm
([msg=13650]see Re: command line argument[/msg])

Wow, Looks like you are angry at me. :D
Anyway, Thanks for the constructive criticism.
First of all, what was wrong with the syntax, please post the incorrect code with the correct way that it should be done.
Second why argv[] should be checked for value which greater then two, shouldn't it be checking for existences of
array ( argv[1] )?
What does strcmp does. Its compering between two strings, right?
So why it cannot be done with an if statement ?
L0DER
New User
New User
 
Posts: 26
Joined: Tue Apr 22, 2008 12:58 am
Blog: View Blog (0)


Re: command line argument

Post by BhaaL on Tue Oct 14, 2008 10:56 am
([msg=13682]see Re: command line argument[/msg])

L0DER wrote:Wow, Looks like you are angry at me. :D

Not really, you just make your post worth less than with correct syntax already. You could have expected posts that tell you that, rather than pointing out the real mistakes.
L0DER wrote:First of all, what was wrong with the syntax, please post the incorrect code with the correct way that it should be done.

Mostly talking about wrong comments.
Two forward slashes // begin a comment, not two backslashes \\. Those would just result in compiler errors. Besides, you misspelt argv once (agrv).
L0DER wrote:Second why argv[] should be checked for value which greater then two, shouldn't it be checking for existences of
array ( argv[1] )?

argv is an array, but since it is C(++), you do not know its length. argc tells you the length of it.
Ever tried the following?
Code: Select all
int myArray[10];
myArray[23] = 42;

If it compiles, it will crash with an access violation. myArray can have 10 entries, but you try to access the 23rd.
Same thing (might) happens with argv. Just because it didnt crash doesnt mean it is the right way to go. Use argc instead.
L0DER wrote:What does strcmp does. Its compering between two strings, right?

As the name says, string compare. Check the Manpage for more.
L0DER wrote:So why it cannot be done with an if statement ?

Not saying it cant be done with if, just not the way you tried.
Your code looked very PHP'ish, you wont get errors there if you try $myArray[23] - on the contrary, it is created there. sizeof($myArray) will be either 11 if you made a hash, or 23 if you have an ordinary array.
In C/++ however, the size 10 is coded into you program - going off those limits will most certainly break.
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: command line argument

Post by L0DER on Tue Oct 14, 2008 2:43 pm
([msg=13688]see Re: command line argument[/msg])

Mostly talking about wrong comments.
Two forward slashes // begin a comment, not two backslashes \\. Those would just result in compiler errors. Besides, you misspelt argv once (agrv).

Come on I taught I made trouble mistakes, but the things that I was worried about was a couple of flipped slashes and misspelled agrv[].
Nothing that wasn't going to be fixed once was compiled, and checked for errors. :)
argv is an array, but since it is C(++), you do not know its length. argc tells you the length of it.
Ever tried the following?

Code: Select all
int myArray[10];
myArray[23] = 42;


If it compiles, it will crash with an access violation. myArray can have 10 entries, but you try to access the 23rd.
Same thing (might) happens with argv. Just because it didnt crash doesnt mean it is the right way to go. Use argc instead.

Actually I have tried it, so I know it from experience, fair enough, better to check the agrc first. :lol:
*********************************************************************************************************************************
So how can I make it with 'If statement' or better 'switch'.
How to compare between two char pointers arrays?
L0DER
New User
New User
 
Posts: 26
Joined: Tue Apr 22, 2008 12:58 am
Blog: View Blog (0)


Re: command line argument

Post by BhaaL on Wed Oct 15, 2008 11:07 am
([msg=13738]see Re: command line argument[/msg])

L0DER wrote:So how can I make it with 'If statement' or better 'switch'.

You cannot switch over strings (which are arrays, and not scalar values).

L0DER wrote:How to compare between two char pointers arrays?

BhaaL wrote:
L0DER wrote:What does strcmp does. Its compering between two strings, right?

As the name says, string compare. Check the Manpage for more.
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: command line argument

Post by L0DER on Wed Oct 15, 2008 2:52 pm
([msg=13748]see Re: command line argument[/msg])

Ok, I managed to make the code cleaner and more flexible.(even with switch) :D

Code: Select all
#include <cstring>
#include <cstdlib>
// \2 makes a simliy
int checkAgr(int argc, char *argv){
    if  (argc <= 1)
    {
             system("echo Error, no argument was accepted");
             system("pause");
             return 1;
    }
             else
             {
                 char *action[3] = {"-start", "-conf", "-Ic"}, *buffer;
                 int mod = 0;
                 for(int count=0;count<3;count++)
                 {
                 if (!strcmp(argv, action[count]))
                 {
                             mod = count;
                             break;
                             }
                         
                 }
                 return mod;
             }
             }

int main(int argc, char *argv[])
{
        char *x = argv[1];
        switch(checkAgr(argc, x))
        {
               case 0:
                     system("start one.exe");
                     system("start two.exe");
                     system("start three.exe");
                     break;
               case 1:
                    system("start starter.exe  /cfg"); 
                    break;
               case 2:
                    system("start importentfiles.exe");
                              }
return 0;
}

Took me some time to come up with idea how to make the code better. But it was harder to apply it.
now for multiple comparisons, strcmp() called only once and no more huge amount of ifs and elses. :)
L0DER
New User
New User
 
Posts: 26
Joined: Tue Apr 22, 2008 12:58 am
Blog: View Blog (0)


Re: command line argument

Post by BhaaL on Thu Oct 16, 2008 12:36 pm
([msg=13808]see Re: command line argument[/msg])

Looks good, except that you failed again :P
You still assign argv[1] to x without checking.
Either perform the check outside, or pass in all of argv, not just the 2nd entry.
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: command line argument

Post by L0DER on Thu Oct 16, 2008 1:33 pm
([msg=13821]see Re: command line argument[/msg])

double post. :oops:
Last edited by L0DER on Thu Oct 16, 2008 1:56 pm, edited 1 time in total.
L0DER
New User
New User
 
Posts: 26
Joined: Tue Apr 22, 2008 12:58 am
Blog: View Blog (0)


Re: command line argument

Post by L0DER on Thu Oct 16, 2008 1:47 pm
([msg=13822]see Re: command line argument[/msg])

You mean like that:

Code: Select all
#include <cstring>
#include <cstdlib>
// \2 makes a simliy
int checkAgr(char *argv)
{
           
                 char *action[3] = {"-start", "-conf", "-lc"}, *buffer;
                 int mod = 0;
                 for(int count=0;count<3;count++)
                 {
                 if (!strcmp(argv, action[count]))
                 {
                             mod = count;
                             break;
                             }
                 }
                 return mod;
             }
             
int main(int argc, char *argv[])
{
        if (argc > 1)
        {
        char *x = argv[1];
         
        switch(checkAgr(x))
        {
               case 0:
                     system("start three.exe");
                     system("start do.exe");
                     system("start done.exe");
                     break;
               case 1:
                    system("start two.exe  /cfg"); 
                    break;
               case 2:
                    system("start one.exe");
                              }
                              }
               else
               {
                    system("echo Error, no argument was accepted");
                    system("pause");
                    return 1;
                    }
return 0;
}

L0DER
New User
New User
 
Posts: 26
Joined: Tue Apr 22, 2008 12:58 am
Blog: View Blog (0)


Next

Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests

cron