Solved

How do I compare a command line string with another string?

Posted on 2011-02-25
15
360 Views
Last Modified: 2012-05-11
Im trying to compare a command line string with a string in code but i keep getting errors. I have #include <strings.h>     what is typed in the command line is ./funny big or small either. I need to compare it so the programs runs a different function depending if they put in ./funny big or ./funny small How do i go about this?
0
Comment
Question by:HelpMeMaggi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
  • 2
15 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 34986019
Can you provide the code you are using please?
0
 

Author Comment

by:HelpMeMaggi
ID: 34986073
thats the thing, i dont have one. Im using if statements.
I have
main(int argc, char *argv[])
{
int smaller;
int bigger;
char small[] = 'small';
char big[] = 'big';

smaller = strcmp(argv[1],'small');
bigger = strcmp(argv[1], 'bigger');

if (smaller = 0)
      {
            printf("small party isn't it?\n");
        return 0;
     }
else if(bigger = 0)
     {
            printf("Big party!!!\n");
           return 0;
      }
}

this is what i have.
0
 
LVL 24

Expert Comment

by:jimyX
ID: 34986091
String in C is enclosed by using double quotes:
main(int argc, char *argv[])
{
int smaller;
int bigger;
char small[] = "small";
char big[] = "big";

smaller = strcmp(argv[1],"small");
bigger = strcmp(argv[1], "bigger");

if (smaller = 0)
      {
            printf("small party isn't it?\n");
        return 0;
     }
else if(bigger = 0)
     {
            printf("Big party!!!\n");
           return 0;
      }
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 34986097
smaller = strcmp(argv[1],"small");
bigger = strcmp(argv[1], "bigger");

if (smaller == 0)
      {
            printf("small party isn't it?\n");
        return 0;
     }
else if(bigger == 0)
     {
            printf("Big party!!!\n");
           return 0;
      }
}
0
 

Author Comment

by:HelpMeMaggi
ID: 34986099
So the only problem i had was my ", does this mean that if i were to put in ./funny small the output will be small party isn't it? ?
I can't test it at the moment.
0
 
LVL 24

Expert Comment

by:jimyX
ID: 34986109
Your argument has to be in a form that matches what you are comparing in the code:

So you should enter:
small or bigger to match in the if statement.
0
 
LVL 84

Expert Comment

by:ozo
ID: 34986111
you also need use the equality operator rather than the assignment operator in your condition
0
 
LVL 24

Expert Comment

by:jimyX
ID: 34986116
Beside the equal as ozo said.

I am a pascal coder ;-)
0
 
LVL 24

Expert Comment

by:jimyX
ID: 34986121
Nice catch ozo :-)
0
 

Author Comment

by:HelpMeMaggi
ID: 34986134
main(int argc, char *argv[])
{
int smaller;
int bigger;
char small[] = "small";
char big[] = "big";

smaller = strcmp(argv[1],"small");
bigger = strcmp(argv[1], "bigger");

if (smaller == 0)
      {
            printf("small party isn't it?\n");
        return 0;
     }
else if(bigger == 0)
     {
            printf("Big party!!!\n");
           return 0;
      }
}
so it would be like this? and yeah that was a typo by me, i just made this code up fast. Well how i have it.
0
 
LVL 24

Expert Comment

by:jimyX
ID: 34986148
Yes, also check what is typed in the command line to be ./funny small or ./funny bigger.
Just moved "return 0;" at the end to save one line of code ;-)
main(int argc, char *argv[])
{
int smaller;
int bigger;
char small[] = "small";
char big[] = "big";

smaller = strcmp(argv[1],"small");
bigger = strcmp(argv[1], "bigger");

if (smaller == 0)
      {
            printf("small party isn't it?\n");
     }
else if(bigger == 0)
     {
            printf("Big party!!!\n");
      }
 return 0;
}

Open in new window

0
 
LVL 24

Expert Comment

by:jimyX
ID: 34986156
But hang on, what are you using char small and big for?
I think you mean to write:
main(int argc, char *argv[])
{
int smaller;
int bigger;
char small[] = "small";
char big[] = "big";

smaller = strcmp(argv[1],small);
bigger = strcmp(argv[1], big);

if (smaller == 0)
      {
            printf("small party isn't it?\n");
     }
else if(bigger == 0)
     {
            printf("Big party!!!\n");
      }
 return 0;
}

Open in new window

And hence the command line becomes: ./funny small or ./funny big as you stated in your question, and most probably that was your error.
0
 

Author Comment

by:HelpMeMaggi
ID: 34986159
smaller = strcmp(argv[1],small);
bigger = strcmp(argv[1], big);

shouldn't there be " " on small and big? or should they be without " " when being compared?
0
 
LVL 24

Accepted Solution

by:
jimyX earned 500 total points
ID: 34986188
This what brought the confusion in the first place. You compare them with the array you initialized as follows:

char small[] = "small";
smaller = strcmp(argv[1],small);

char big[] = "big";
bigger = strcmp(argv[1], big);

Either that or you can use direct string in quotation, but then there is no need to use the arrays, so it becomes:
main(int argc, char *argv[])
{
int smaller;
int bigger;

smaller = strcmp(argv[1],"small");
bigger = strcmp(argv[1], "big");

if (smaller == 0)
      {
            printf("small party isn't it?\n");
     }
else if(bigger == 0)
     {
            printf("Big party!!!\n");
      }
 return 0;
}

Open in new window


In command line you write the same string you are comparing against otherwise you get no matching.
0
 

Author Closing Comment

by:HelpMeMaggi
ID: 34986223
Not complete code but i worked with what i have. thank you!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question