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;
}
Note that === never causes type coercion, but checks for correct types first and yields false if they are not equal!
Yes. You can also override the equals() method and play with it.
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 .
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.
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.
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.
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.
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!";
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.
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 ==
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With