Error: cannot convert `std::string' to `const char*' for argument `1' to `int std::strcmp(const char*, const char*)'

Hello All,

I'm new to C++ programming, for practice i'm building a basic currency converter, i'm receiving the error "cannot convert `std::string' to `const char*' for argument `1' to `int std::strcmp(const char*, const char*)'" when trying to compare the answer from the user with "Yes", "YES" or "yes".

Can you please explain what is wrong with the below code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
        
        string sYesNo;
        string sAnswer = "Yes";
        cout << "Do you wish to do another conversion? (Yes/No): ";
        cin >> sYesNo;
        
        if(std::strcmp(sYesNo, sAnswer) == 0)
        {
                   cout << "============================================\n";
                   cout << "\n";
                   continue;
        }
        else
        {
                   cout << "Thanks for trying my Currency Converter\n";
                   cout << "\n";
                   break; 
        } 


I also tried the following without success, appreciate your comment on this one as well:

1
2
3
4
5
6
7
8
9
10
11
12
        if (sYesNo == "Yes" || "yes" || "YES")
        {
                   cout << "============================================\n";
                   cout << "\n";
                   continue;
        }
        else if (sYesNo == "NO" || "No" || "no")
        {
                   cout << "Thanks for trying my Currency Converter\n";
                   cout << "\n";
                   break; 
        }


1
2
3
4
5
6
7
8
9
10
11
          switch (sYesNo)
          {
                 case ("YES"||"Yes"||"yes"):
                      cout << "============================================\n";
                      cout << "\n";
                      break;
                 default:
                      cout << "Thanks for trying my Currency Converter\n";
                      cout << "\n";
                      break;
          }


Thanks in advance

BR,
Barbosa
Hello,
as the error message tells you, strcmp() function expects two arguments of type const char*, whereas sYesNo and sAnswer are of type string. Fortunately a string is more or less a char*, because that's how the data is represented internally. You can get the char*-representation by calling std::string::c_str(), so
if(std::strcmp(sYesNo.c_str(), sAnswer.c_str()) == 0)
would fix your problem.
However, string has been invented to no longer need to deal with character arrays. Using its comparison operator (which is what you have done in the second example) is much more convenient and should be preferred over strcmp and string::c_str().
Make sure you understand how to use the boolean operators. My suggestion is you use
if(sYesNo == "Yes" || sYesNo == "yes" || sYesNo == "YES")
Alternatively, you could first convert sYesNo to lowercase letters and then just compare to "yes".
Here are edited versions of your attempts that will now work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        string sYesNo;
        string sAnswer = "Yes";
        cout << "Do you wish to do another conversion? (Yes/No): ";
        cin >> sYesNo;
        
        if(std::strcmp(sYesNo.c_str(), sAnswer.c_str) == 0)
        {
                   cout << "============================================\n";
                   cout << "\n";
                   continue;
        }
        else
        {
                   cout << "Thanks for trying my Currency Converter\n";
                   cout << "\n";
                   break; 
        }  


strcomp takes 2 c-strings, aka character arrays. You are trying to pass in two strings, which won't work, so I used c_str() to convert them to c-strings. This isn't a very efficient way of comparing strings thought. You could use the compare function for strings which you can read about here: http://www.cplusplus.com/reference/string/string/compare/

1
2
3
4
5
6
7
8
9
10
11
12
        if (sYesNo == "Yes" || sYesNo == "yes" || sYesNo == "YES")
        {
                   cout << "============================================\n";
                   cout << "\n";
                   continue;
        }
        else if (sYesNo == "NO" sYesNo == || "No" || sYesNo == "no")
        {
                   cout << "Thanks for trying my Currency Converter\n";
                   cout << "\n";
                   break; 
        } 


You cannot chain together ORs (or ANDs for that matter) like you are trying to. Each must be seperate as shown above. This would be a fine way of testing for this.

1
2
3
4
5
6
7
8
9
10
11
12
13
          switch (sYesNo)
          {
                 case ("YES"):
                 case ("Yes"):
                 case ("yes"):
                      cout << "============================================\n";
                      cout << "\n";
                      break;
                 default:
                      cout << "Thanks for trying my Currency Converter\n";
                      cout << "\n";
                      break;
          } 


Again, you can't chain together OR statements like that, but since a switch will keep executing code until it hits a break, you can do the above. In the case of "YES", it will go to the "YES" case and then just "fall through" the other 2 cases into the cout statements, before finally hitting the break.
You can't do the switch. That switch compares string with const char*. You can't do switches with strings either, has to be a constant integral. Doesn't make any sense to do it with const char * either, because you would have to do the if/else just to figure out which const char* to do the switch with.
Thanks all for your support.
It is now clear for me and working fine :)
Topic archived. No new replies allowed.