Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equals returning false in c++

I'm fairly new to cpp and I am trying to do a project. It says that the code must take in a filename as an argument and will be run by:

./main -i filename

I have written a for-loop that will iterate through the list of arguments to find the "-i" argument so that I can determine the filename. But this line always return false:

argv[i] == "-i"

Below is my code:

#include <string>
#include <iostream>

int main(int argc, char *argv[]) {
    std::string test = argv[0];
    for(int i = 0; i < argc; i++){
        if(argv[i] == "-i"){
            test = argv[i+1];
            break;
        }
    }
    std::cout << test;
    return 1;
}
like image 260
Arjun C Avatar asked Oct 02 '18 11:10

Arjun C


People also ask

Does === return false?

Note that === never causes type coercion, but checks for correct types first and yields false if they are not equal!

Is it possible for equals() to return false even if contents of two objects are same?

Yes. You can also override the equals() method and play with it.

What is equal return C++?

The equal-to operator ( == ) returns true if both operands have the same value; otherwise, it returns false . The not-equal-to operator ( != ) returns true if the operands don't have the same value; otherwise, it returns false .

How to say not equal to in c++?

The != operator checks if the first operand is not equal to the second operand. If the first operand is not equal to the second operand, returns true. Else returns false.

How do you know if two values are equal in JavaScript?

The equality operator == returns true if its operands are equal, false otherwise. Value types equality. Operands of the built-in value types are equal if their values are equal: For the ==, <, >, <=, and >= operators, if any of the operands is not a number (Double.NaN or Single.NaN), the result of operation is false.

What happens when a function does not return a value?

Similarly when it does not return a value, the calling function does not receive any data from the called function. Function with arguments but no return value : When a function has arguments, it receive any data from the calling function but it returns no values.

What is function argument and return in C?

C function argument and return values. A function in C can be called either with arguments or without arguments. These function may or may not return values to the calling functions. All C functions can be called either with arguments or without arguments in a C program. Also, they may or may not return any values.

When are two string operands equal in C++?

Two string operands are equal when both of them are null or both string instances are of the same length and have identical characters in each character position: string s1 = "hello!"; string s2 = "HeLLo!"; Console.WriteLine (s1 == s2.ToLower ()); // output: True string s3 = "Hello!";


2 Answers

argv[i] == "-i"

In the line above you compare two pointers: char* and const char*, respectively.

In other words, instead of comparing argv[i] and "-i" two pointers are compared which are pretty much unlikely to point to the same location. As a result, the check doesn't work in your case.

You can fix it in multiple ways, for example wrap "-i" into std::string to make the comparison work properly:

const auto arg = std::string{ "-i" };

for(int i = 0; i < argc; i++){
    if(argv[i] == arg){
        test = argv[i+1];
        break;
    }
}

Starting with C++17 you might also use a std::string_view:

const std::string_view sv{ "-i" };

for(int i = 0; i < argc; i++){
    if(argv[i] == sv){
        test = argv[i+1];
        break;
    }
}

which is a preferable way as it avoids a std::string creation.

like image 95
Edgar Rokjān Avatar answered Oct 13 '22 17:10

Edgar Rokjān


You cannot compare pointers to char to string literals (char const*) using ==. Use std::strcmp() (<cstring>) or construct a std::string (<string>) from it to make it comparable to a char* using ==.

like image 21
Swordfish Avatar answered Oct 13 '22 17:10

Swordfish