Equals returning false in c++

The name of the pictureThe name of the pictureThe name of the pictureClash 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;










share|improve this question



















  • 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














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;










share|improve this question



















  • 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












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;










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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












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;







share|improve this answer




















  • 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


















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.






share|improve this answer






















  • 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

















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;







share|improve this answer








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.

















    Your Answer





    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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






























    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;







    share|improve this answer




















    • 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















    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;







    share|improve this answer




















    • 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













    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;







    share|improve this answer












    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;








    share|improve this answer












    share|improve this answer



    share|improve this answer










    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 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

















    • 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
















    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













    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.






    share|improve this answer






















    • 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














    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.






    share|improve this answer






















    • 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












    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.






    share|improve this answer














    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 1 hour ago

























    answered 1 hour ago









    Swordfish

    3,576727




    3,576727











    • 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
















    • 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















    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










    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;







    share|improve this answer








    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.





















      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;







      share|improve this answer








      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.



















        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;







        share|improve this answer








        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;








        share|improve this answer








        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.









        share|improve this answer



        share|improve this answer






        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.



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery