This question already has answers here:
What is the proper function for comparing two C-style strings?
(2 answers)
Compare equality of char[] in C
(5 answers)
Closed 2 years ago.
I'm trying to make a simple program that takes one argument. I use as main:
int main(int argc, char* argv[]) {
if (argv[1] == "string") {
cout<<"STRING AS ARG!!"<<endl;
}
return 0;
}
But when I run it with program.exe string nothing happens. What is wrong with the piece of code?
The problem is that both operands are C strings and the comparison does a raw pointer comparison. You need to make at least one operand a std::string or a std::string_view.
std::string_view is the best option because it avoids the overhead of the std::string, but you need C++17:
#include <string_view>
using namespace std::literals;
if (argv[1] == "string"sv)
The std::string versions. If you have C++14 you can use the literal:
#include <string>
using namespace std::literals;
if (argv[1] == "string"s)
Or if you are pre C++14:
#include <string>
if (argv[1] == std::string("string"))
argv[1] is a pointer (char *). You can not compare it to "string" using ==, it'll always return false. You should use strcmp:
if (strcmp(argv[1], "string") == 0)
Related
This question already has answers here:
String plus Char - what is happening?
(5 answers)
Closed 8 months ago.
#include <iostream>
#include <conio.h>
#include <stack>
#include <string>
using namespace std;
int main{
string h = "";
h = ("" + 'a');
cout << h;
return 0;
}
Output: "nity\VC\Tools\MSVC\14.29.30133\include\xstring"
I am honestly clueless as to what to do. I've never had this happen before.
Note: I've found a way to avoid this by appending the char like this:
string g="";
g+='a';
Regardless, why is this?
"" is a literal of type const char[1], which is the identical as const char* in most regards. 'a' is a literal of type char, which is really just an integer type. So if you do "" + 'a', you will get a pointer to 'a' (=97 in ASCII) characters after wherever the compiler decides to put the "". Which is then converted to an std::string.
In the working example, you convert the "" literal to std::string first, then add a char to it. std::string overloads the + and += operators, so it will produce a reasonable result.
This question already has answers here:
Comparing command parameter with argv[] is not working
(4 answers)
Closed 1 year ago.
I executed the below program as: ./aout w.
#include<iostream>
using namespace std;
int main(int argc, char** argv)
{
if (argv[1] == "w")
{
cout << "this was worked";
}
else
{
cout << "this did not worked";
}
}
OUTPUT: this did not worked.
I tried executing: if(&argv[1] == "w")
if(argv[1]=='w')
The line
if (argv[1] == "w")
is equivalent to
char const* p1 = argv[1];
char const* p2 = "w";
if ( p1 == p2 )
i.e. you are comparing two pointers, not the strings that the pointers point to.
You can use std::strcmp. However, since you are using C++, you might as well use std::string.
if (std::string(argv[1]) == "w")
If you are able to use C++17, you may use std::string_view too.
if (std::string_view(argv[1]) == "w")
If you compare arrays and pointers using the equality operator, then you are comparing whether they are the same array or point to the same object. You aren't comparing the content of the arrays or pointed elements. Since argv[1] points to a different array than the string literal, they compare unequal regardless of their content.
tried executing: if(&argv[1] == "w")
Now, you're comparing char** to a const char* (after array decay). Comparing pointers of unrelated types doesn't make much sense.
if(argv[1]=='w').
Now, you're comparing char* to char. Comparing a pointer to a char doesn't make much sense.
To compare the content of two strings, A good solution is to use a string view as either or both of the operands. It's a small change:
using namespace std::literals;
if (argv[1] == "w"sv)
This question already has answers here:
C++, copy set to vector
(8 answers)
How to copy std::string into std::vector<char>? [duplicate]
(2 answers)
Closed 2 years ago.
The following code prints an empty string and i am unable to understand why it is so?
#include <string>
#include <algorithm>
#include <iostream>
int main()
{
std::string s="hello";
std::string r;
std::copy(s.rbegin(),s.rend(), r.begin());
std::cout<<r;
return 0;
}
The problem is r is an empty std::string, it contains no chars. std::copy is trying to copy-assign chars since r.begin(), which leads to UB.
You can make r containing 5 elements in advance.
std::string r(5, '\0');
Or
std::string r;
r.resize(5);
Or use std::back_inserter.
std::string r;
std::copy(s.rbegin(),s.rend(), std::back_inserter(r));
When we use typeid i.e typeid(variable).name()
Does it give out string as the output because if it does it could be helpful in comparisons with strings.
According to the standard, it is an implementation-defined null-terminated const char*:
18.7.1 Class type_info
....
const char* name() const noexcept;
Returns: An implementation-defined NTBS.
Remarks: The message may be a null-terminated multibyte string (17.5.2.1.4.2), suitable for conversion
and display as a wstring (21.3, 22.4.1.4)
Since the content is implementation-defined, it cannot be compared with other strings in a reliable way, unless we restrict ourselves to specific implementations.
typeid(variable).name() returns a pointer a null terminated string, which can be compared using strcmp(). However a better way to check a type of variable is
if (typeid(a) == typeid(int))
I'm getting Ss when I try this.
#include <string>
#include <typeinfo>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
string str = "string";
cout << typeid(str).name();
return 0;
}
Try it: http://cpp.sh/4lsw
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C String — Using Equality Operator == for comparing two strings for equality
Basic question here. I'm compiling this program in g++ and running it with a single -r argument, (./a.out -r) however it does not output the specified cout statement below. Is there anything wrong with this code?
#include <string>
using namespace std;
int main(int argc, char* argv[]) {
if (argv[1] == "-r" ) {
cout << "First arg is -r" << endl;
}
return 0;
}
You cannot compare string literals using ==, because what that does is compare pointers (i.e., memory addresses, which in this case are always going to be different).
Use strcmp or compare std::string objects instead:
if (strcmp(argv[1], "-r") == 0) { ... }
or
if (std::string(argv[1]) == "-r") { ... }
You can't compare string literals with ==. Use strcmp.
(Also, don't forget to check that argc is at least 2).
One mistake in your code is you are comparing the address of the char array instead of comparing the values in the array. As people have said you can do it the C way using strcmp (although it's safer to use strncmp to avoid buffer overruns).
However, it's easier to use std::string in C++.
I would copy the args into a vector of strings.
#include <vector>
#include <string>
int main(int argc, char* argv[])
{
vector<string> args(argv, argv+argc);
...
Usually you would do something along these lines:
while (--argc && **++argv=='-')
{
char *p;
p=*argv+1;
switch(tolower(*p))
{
case 'r':
break;
}
}
basically, while there are some args left, check that there is a '-' char, then switch on the next char to see the modifier ('r', or 'a' etc)