Error Cannot Convert Const Std String To Const Char
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the
Cannot Convert Std String To Const Char * For Argument 1
workings and policies of this site About Us Learn more about Stack no suitable conversion function from std string to const char * Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs convert string to const char c++ Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join
Convert String To Const Char Arduino
them; it only takes a minute: Sign up How to convert std::string to const char in C++ [duplicate] up vote 2 down vote favorite 2 This question already has an answer here: How to convert a std::string to const char* or char*? 7 answers I tried to research this for a bit and I can't figure out the problem
Cannot Convert ‘std::basic_string’ To ‘const Char*’
Here's the code: #include
here for a quick overview of the site Help Center Detailed answers to any questions you might string to const char arduino have Meta Discuss the workings and policies of this site About
No Known Conversion For Argument 1 From 'string' To 'const Char*' Arduino
Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive] with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, http://stackoverflow.com/questions/20390008/how-to-convert-stdstring-to-const-char-in-c just like you, helping each other. Join them; it only takes a minute: Sign up cannot convert 'std::basic_string
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting http://stackoverflow.com/questions/347949/how-to-convert-a-stdstring-to-const-char-or-char ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a http://www.cplusplus.com/forum/beginner/90885/ minute: Sign up How to convert a std::string to const char* or char*? up vote 604 down vote favorite 239 How can I convert an std::string to a char* or a const char*? c++ string char const share|improve this question const char edited Oct 6 '14 at 7:43 Niall 19.6k85083 asked Dec 7 '08 at 19:30 user37875 4,50672637 1 Instead of: char * writable = new char[str.size() + 1]; You can use char writable[str.size() + 1]; Then you don't need to worry about deleting writable or exception handling. –user372024 Jun 21 '10 at 9:34 7 You can't use str.size() unless the size is known at compile time, also it might overflow your stack if the fixed size value is huge. –paulm Oct string to const 5 '12 at 15:32 1 char* result = strcpy((char*)malloc(str.length()+1), str.c_str()); –cegprakash Jul 12 '14 at 12:10 2 @cegprakash strcpy and malloc aren't really the C++ way. –boycy Sep 25 '14 at 9:29 3 No, but char* dest = new char[str.length() + 1]; std::copy(str.begin(), str.end(), dest) would be more idiomatic C++. strcpy() and malloc() aren't wrong or problematic, but it seems inconsistent to use a C++ string and C library facilities with C++ equivalents in the same block of code. –boycy Sep 27 '14 at 8:38 | show 1 more comment 7 Answers 7 active oldest votes up vote 750 down vote accepted If you just want to pass a std::string to a function that needs const char* you can use std::string str; const char * c = str.c_str(); If you want to get a writable copy, like char *, you can do that with this: std::string str; char * writable = new char[str.size() + 1]; std::copy(str.begin(), str.end(), writable); writable[str.size()] = '\0'; // don't forget the terminating 0 // don't forget to free the string after finished using it delete[] writable; Edit: Notice that the above is not exception safe. If anything between the new call and the delete call throws, you will leak memory, as nothing will call delete for you automatically. There are two immediate ways to solve this. boost::scoped_array boost::scoped_array will delete the memory for you upon going out of scope: std::string str; boost::scoped_array
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 Jan 26, 2013 at 12:31am UTC defaultplayer (6) 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_st