4
#include <iostream>

using namespace std;

int main(int argc,char* argv[]){
    if(argv[1] == ""){
        cout << "please put something" << endl;
    }else if(argv[1] == "string"){
        cout << "yeah string" << endl;
    }else if(argv[1] == "integer"){
        cout << "yeah integer" << endl;
    }
}

I don't know what's wrong: I try to check if argument supplied for argv[1] is empty so it will be false and application will exit, so please tell me what is wrong in my code.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Mohd Shahril
  • 2,257
  • 5
  • 25
  • 30

5 Answers5

7

Everybody is giving you a different answer. And in fact everybody is right.

The signature of main, int main(int argc, char *argv[]) is inherited from C. In C strings are pointer to char. When you use operator== on them, you only compare pointer value.

The C way to compare string content is to use strcmp.

if (strcmp(argv[1], "integer") == 0){

It is safer and easier for you to do it the C++ way.

if (std::string(argv[1]) == "integer"){

This line create a temporary std::string from argv[1]. you must include string for this to work.

Finally check if argc == 2 in order to know if an argument was supplied. It is true that argv is null terminated by the standard 3.6.1 but it definitely make things clearer to check that argv is indeed equal to 2.

log0
  • 10,489
  • 4
  • 28
  • 62
4

Can't use == for argv[] as it's char* type, use strcmp instead.

int main(int argc, char*argv[])
{
    if(argc == 1){
        cout << "please put something" << endl;
    }else if(strcmp(argv[1], "string") == 0){
        cout << "yeah string" << endl;
    }else if (strcmp(argv[1], "integer") == 0){
        cout << "yeah integer" << endl;
    }
  return 0;
}

Or you need to make a copy of argv[1] to std::string then you can use == operator.

billz
  • 44,644
  • 9
  • 83
  • 100
3

If you want to code in C++ you can use std::string, include its header:

#include <string>

Convert the first argument to std::string and use its operator==:

std::string first = argv[1];

if(first == ""){
    cout << "please put something" << endl;
}else if(first == "string"){
    cout << "yeah string" << endl;
}else if(first == "integer"){
    cout << "yeah integer" << endl;
}

Comparison of char * to string literals doesn't make sense, if you make a std::string out of argv[1] then it's a different story, it will work as you expected.

But, you should check the number of arguments supplied to the program by the user first, it's in argc.

piokuc
  • 25,594
  • 11
  • 72
  • 102
1

First check if argc > 1. Otherwise, you're accessing an invalid memory.

if(argc < 2){
    cout << "please put something" << endl;
}else if(argv[1] == "string"){
    cout << "yeah string" << endl;
}else if(argv[1] == "integer"){
    cout << "yeah integer" << endl;
}
grebulon
  • 7,697
  • 5
  • 42
  • 66
  • 2
    "Otherwise, you're accessing an invalid memory." - Nope, since `argv` is NULL-terminated - even if there are no arguments, `argv[1]` is valid, it's just `NULL`. –  Dec 22 '12 at 11:09
  • so if i make string(argv[1]) == NULL it's make sense or not ? – Mohd Shahril Dec 22 '12 at 11:27
  • (string(argv[1]) == NULL) works exactly the same as (argc < 2). The reason is that in standard C, the last value of the argv array is always NULL. That is as long as your compiler implements the standard... – grebulon Dec 22 '12 at 16:45
0
         #include <string>
         #include <iostream>

This will use the c++ way to compare strings. The safer way. Convert the first argument to std::string, then use the string::compare function

          std::string inString = argv[1];

          if(inString.compare("") != 0) 
          {
             std::cout << "please put something in" << std::endl;
          }
          else if(inString.compare("string") == 0 ){
             std::cout << "yeah you got a string" << std::endl;
          }
          else if(inString.compare("integer") == 0)
          {
             std::cout << "yeah you got an integer" << std::endl;
          }

Note: with string::compare(string or pointer to a string) see reference on this function. Relation between the string and what you are comparing to:

0 They compare equal. If compare yields < 0, either the value of the first character that does not match is lower in the compared string, or all compared characters match but the compared string is shorter. If >0, then either the value of the first character that does not match is greater in the compared string, or all compared characters match but the compared string is longer.