Equals returning false in c++
Clash Royale CLAN TAG#URR8PPP
up vote
7
down vote
favorite
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;
c++ g++ command-line-arguments
add a comment |Â
up vote
7
down vote
favorite
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;
c++ g++ command-line-arguments
4
Welcome to Stack Overflow! This is a very simple mistake in C++, but congratulations in writing a clear question with a Minimal, Complete, and Verifiable example.
– Martin Bonner
1 hour ago
I understand you are writing your own parser for learning purposes. Once you know how to do it and you understand the path to achieve it, have a look here. gnu.org/software/libc/manual/html_node/…
– Al Bundy
14 mins ago
add a comment |Â
up vote
7
down vote
favorite
up vote
7
down vote
favorite
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;
c++ g++ command-line-arguments
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;
c++ g++ command-line-arguments
c++ g++ command-line-arguments
edited 1 hour ago
Martin Bonner
22.9k33059
22.9k33059
asked 1 hour ago


Arjun C
434
434
4
Welcome to Stack Overflow! This is a very simple mistake in C++, but congratulations in writing a clear question with a Minimal, Complete, and Verifiable example.
– Martin Bonner
1 hour ago
I understand you are writing your own parser for learning purposes. Once you know how to do it and you understand the path to achieve it, have a look here. gnu.org/software/libc/manual/html_node/…
– Al Bundy
14 mins ago
add a comment |Â
4
Welcome to Stack Overflow! This is a very simple mistake in C++, but congratulations in writing a clear question with a Minimal, Complete, and Verifiable example.
– Martin Bonner
1 hour ago
I understand you are writing your own parser for learning purposes. Once you know how to do it and you understand the path to achieve it, have a look here. gnu.org/software/libc/manual/html_node/…
– Al Bundy
14 mins ago
4
4
Welcome to Stack Overflow! This is a very simple mistake in C++, but congratulations in writing a clear question with a Minimal, Complete, and Verifiable example.
– Martin Bonner
1 hour ago
Welcome to Stack Overflow! This is a very simple mistake in C++, but congratulations in writing a clear question with a Minimal, Complete, and Verifiable example.
– Martin Bonner
1 hour ago
I understand you are writing your own parser for learning purposes. Once you know how to do it and you understand the path to achieve it, have a look here. gnu.org/software/libc/manual/html_node/…
– Al Bundy
14 mins ago
I understand you are writing your own parser for learning purposes. Once you know how to do it and you understand the path to achieve it, have a look here. gnu.org/software/libc/manual/html_node/…
– Al Bundy
14 mins ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
4
down vote
accepted
argv[i] == "-i"
It compares two pointers so the result is always false.
You can fix it in multiple ways, for example:
const auto arg = std::string "-i" ;
for(int i = 0; i < argc; i++)
if(argv[i] == arg)
test = argv[i+1];
break;
You really ought to point out the out-of-bounds error if the code is called without a filename following the-i
. Probably the best way is to test againstargc-1
.
– Martin Bonner
1 hour ago
It'a also a good idea to start with index 1 as 0 is used for the program name.
– Thomas
18 mins ago
add a comment |Â
up vote
6
down vote
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 ==
.
Also, given
for(int i = 0; i < argc; i++)
if(argv[i] == "-i") // i might be argc-1
test = argv[i+1]; // i+1 == argc ==> boom!
break;
if "-i"
is the last element in the array argv
your code will access argv
out of bounds.
Simplest fix toi
out of range is to usei < argc-1
as the limit
– Martin Bonner
1 hour ago
or usestd::string_view
(C++17).
– Jarod42
1 hour ago
add a comment |Â
up vote
0
down vote
try this:::
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv)
string test;
for(int i = 0; i < argc; i++)
cout<<"n"<<argv[i]<<endl;
if((string)argv[i] == "-i")
test = argv[i+1];
cout<<"test= "<<test<<endl;
break;
cout << test<<endl;
system("pause");
return 0;
New contributor
Elbædry Jøba is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
argv[i] == "-i"
It compares two pointers so the result is always false.
You can fix it in multiple ways, for example:
const auto arg = std::string "-i" ;
for(int i = 0; i < argc; i++)
if(argv[i] == arg)
test = argv[i+1];
break;
You really ought to point out the out-of-bounds error if the code is called without a filename following the-i
. Probably the best way is to test againstargc-1
.
– Martin Bonner
1 hour ago
It'a also a good idea to start with index 1 as 0 is used for the program name.
– Thomas
18 mins ago
add a comment |Â
up vote
4
down vote
accepted
argv[i] == "-i"
It compares two pointers so the result is always false.
You can fix it in multiple ways, for example:
const auto arg = std::string "-i" ;
for(int i = 0; i < argc; i++)
if(argv[i] == arg)
test = argv[i+1];
break;
You really ought to point out the out-of-bounds error if the code is called without a filename following the-i
. Probably the best way is to test againstargc-1
.
– Martin Bonner
1 hour ago
It'a also a good idea to start with index 1 as 0 is used for the program name.
– Thomas
18 mins ago
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
argv[i] == "-i"
It compares two pointers so the result is always false.
You can fix it in multiple ways, for example:
const auto arg = std::string "-i" ;
for(int i = 0; i < argc; i++)
if(argv[i] == arg)
test = argv[i+1];
break;
argv[i] == "-i"
It compares two pointers so the result is always false.
You can fix it in multiple ways, for example:
const auto arg = std::string "-i" ;
for(int i = 0; i < argc; i++)
if(argv[i] == arg)
test = argv[i+1];
break;
answered 1 hour ago


Edgar RokjÄÂn
14.5k42650
14.5k42650
You really ought to point out the out-of-bounds error if the code is called without a filename following the-i
. Probably the best way is to test againstargc-1
.
– Martin Bonner
1 hour ago
It'a also a good idea to start with index 1 as 0 is used for the program name.
– Thomas
18 mins ago
add a comment |Â
You really ought to point out the out-of-bounds error if the code is called without a filename following the-i
. Probably the best way is to test againstargc-1
.
– Martin Bonner
1 hour ago
It'a also a good idea to start with index 1 as 0 is used for the program name.
– Thomas
18 mins ago
You really ought to point out the out-of-bounds error if the code is called without a filename following the
-i
. Probably the best way is to test against argc-1
.– Martin Bonner
1 hour ago
You really ought to point out the out-of-bounds error if the code is called without a filename following the
-i
. Probably the best way is to test against argc-1
.– Martin Bonner
1 hour ago
It'a also a good idea to start with index 1 as 0 is used for the program name.
– Thomas
18 mins ago
It'a also a good idea to start with index 1 as 0 is used for the program name.
– Thomas
18 mins ago
add a comment |Â
up vote
6
down vote
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 ==
.
Also, given
for(int i = 0; i < argc; i++)
if(argv[i] == "-i") // i might be argc-1
test = argv[i+1]; // i+1 == argc ==> boom!
break;
if "-i"
is the last element in the array argv
your code will access argv
out of bounds.
Simplest fix toi
out of range is to usei < argc-1
as the limit
– Martin Bonner
1 hour ago
or usestd::string_view
(C++17).
– Jarod42
1 hour ago
add a comment |Â
up vote
6
down vote
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 ==
.
Also, given
for(int i = 0; i < argc; i++)
if(argv[i] == "-i") // i might be argc-1
test = argv[i+1]; // i+1 == argc ==> boom!
break;
if "-i"
is the last element in the array argv
your code will access argv
out of bounds.
Simplest fix toi
out of range is to usei < argc-1
as the limit
– Martin Bonner
1 hour ago
or usestd::string_view
(C++17).
– Jarod42
1 hour ago
add a comment |Â
up vote
6
down vote
up vote
6
down vote
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 ==
.
Also, given
for(int i = 0; i < argc; i++)
if(argv[i] == "-i") // i might be argc-1
test = argv[i+1]; // i+1 == argc ==> boom!
break;
if "-i"
is the last element in the array argv
your code will access argv
out of bounds.
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 ==
.
Also, given
for(int i = 0; i < argc; i++)
if(argv[i] == "-i") // i might be argc-1
test = argv[i+1]; // i+1 == argc ==> boom!
break;
if "-i"
is the last element in the array argv
your code will access argv
out of bounds.
edited 1 hour ago
answered 1 hour ago


Swordfish
3,576727
3,576727
Simplest fix toi
out of range is to usei < argc-1
as the limit
– Martin Bonner
1 hour ago
or usestd::string_view
(C++17).
– Jarod42
1 hour ago
add a comment |Â
Simplest fix toi
out of range is to usei < argc-1
as the limit
– Martin Bonner
1 hour ago
or usestd::string_view
(C++17).
– Jarod42
1 hour ago
Simplest fix to
i
out of range is to use i < argc-1
as the limit– Martin Bonner
1 hour ago
Simplest fix to
i
out of range is to use i < argc-1
as the limit– Martin Bonner
1 hour ago
or use
std::string_view
(C++17).– Jarod42
1 hour ago
or use
std::string_view
(C++17).– Jarod42
1 hour ago
add a comment |Â
up vote
0
down vote
try this:::
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv)
string test;
for(int i = 0; i < argc; i++)
cout<<"n"<<argv[i]<<endl;
if((string)argv[i] == "-i")
test = argv[i+1];
cout<<"test= "<<test<<endl;
break;
cout << test<<endl;
system("pause");
return 0;
New contributor
Elbædry Jøba is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
0
down vote
try this:::
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv)
string test;
for(int i = 0; i < argc; i++)
cout<<"n"<<argv[i]<<endl;
if((string)argv[i] == "-i")
test = argv[i+1];
cout<<"test= "<<test<<endl;
break;
cout << test<<endl;
system("pause");
return 0;
New contributor
Elbædry Jøba is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
try this:::
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv)
string test;
for(int i = 0; i < argc; i++)
cout<<"n"<<argv[i]<<endl;
if((string)argv[i] == "-i")
test = argv[i+1];
cout<<"test= "<<test<<endl;
break;
cout << test<<endl;
system("pause");
return 0;
New contributor
Elbædry Jøba is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
try this:::
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv)
string test;
for(int i = 0; i < argc; i++)
cout<<"n"<<argv[i]<<endl;
if((string)argv[i] == "-i")
test = argv[i+1];
cout<<"test= "<<test<<endl;
break;
cout << test<<endl;
system("pause");
return 0;
New contributor
Elbædry Jøba is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Elbædry Jøba is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 40 mins ago
Elbædry Jøba
1
1
New contributor
Elbædry Jøba is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Elbædry Jøba is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Elbædry Jøba is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52607440%2fequals-returning-false-in-c%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
4
Welcome to Stack Overflow! This is a very simple mistake in C++, but congratulations in writing a clear question with a Minimal, Complete, and Verifiable example.
– Martin Bonner
1 hour ago
I understand you are writing your own parser for learning purposes. Once you know how to do it and you understand the path to achieve it, have a look here. gnu.org/software/libc/manual/html_node/…
– Al Bundy
14 mins ago