I want to make function that can return two new string that is the composition of the old one but I got an above error.
string constru(string num, int pos_be, int pos_end)
{
string f_num="";
string s_num="";
f_num.append(num.at(pos_be));
f_num.append(num.at(pos_end));
num.erase(pos_be);
num.erase(pos_end);
for(int i=0; i<num.size();i++)
{
s_num.append(num.at(i));
}
return f_num,s_num;
}
The Error is at the line f_num.append(num.at(pos_be)) as well as the other lines that I used append with string. Does anyone want know what went wrong here?
The problem here is, the at function returns a char and not a string. But the append function supports a string. So you get this error. Convert the char to string before you append.
f_num.append(std::string(num.at(pos_be)));
f_num.append(std::string(num.at(pos_end)));
f_num.append(std::string(num.at(pos_be)));
f_num.append(std::string(num.at(pos_end)));
Related
I am trying to print the elements of a set containing strings on graphics.h console using outtext() function,but i get this error:
cannot convert 'std::string {aka std::basic_string}' to 'char*' for argument '1' to 'void outtext(char*)'|
this the piece of code that gives error:
for(i=0;i<20;i++){
for(j=0;j<20;j++){
outtext(str[i][j]);
}
}
the template for the outtext function in the graphics.h header is like this:
void outtext(char *textstring);
i have used c_str() like this:
for(i=0;i<20;i++){
for(j=0;j<20;j++){
outtext(str[i][j].c_str());
}
}
but this time it gives this error:
error: invalid conversion from 'const char*' to 'char*' [-fpermissive]|
You can try this one as well:
char *cstr = new char[21]; // just in case string length is maxed at 20, leave 1 character for '\0'
for (int i = 0; i<20; i++) {
for (int j = 0; j<20; j++) {
strcpy_s(cstr, str[i][j].length() + 1, str[i][j].c_str());
outtext(cstr);
}
}
delete[] cstr;
Just added a char* string to temporarily hold the converted std::string value. The tricky part is that char* strings normally have the terminating character \0 which std::string don't have, so you have to add 1 more character to the size of each "row" of str.
I take it this question is about the 30 years old BGI graphics library and Borland C++. The root of the problem is that this library was poorly written, as it didn't implement const correctness.
The Turbo C++ compiler did not follow anything remotely close to any C++ standard, so you are mostly out of luck. If you had a proper C++ compiler you could use const_cast, but I very much doubt this is available to you.
The only solution left is the dirty, bad way:
outtext((char*)str[i][j].c_str()); // bad practice
You should never cast away const like this in neither C nor C++.
If you can change the prototype of the output function then it is better to change void outtext(char *textstring); to void outtext(const char *textstring); because there is no need for the output function to modifiy the string. Otherwise you could use const_cast before passing to the function like outtext(const_cast<char*>(str[i][j].c_str())) or copy the string to another char* and passed the copied value.
This is in reference to the following answer by Synxis.
https://codereview.stackexchange.com/questions/18684/find-all-substrings-interview-query-in-c/18715#18715
Suppose, I have to print all substrings of the string "cbaa". To do this, I have to invoke the method like this:
findAllSubstrings2("cbaa");
If I take a string from user, and do the following:
string s;
cin>>s;
findAllSubstrings2(s);
it gives the following error:
[Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'void findAllSubstrings2(const char*)'
Why does this happen?
As the error message says the parameter of function findAllSubstrings2 is declared as having type const char * while you are trying to pass an argument of type std::string
string s;
//...
findAllSubstrings2(s);
You should use member function c_str or data (starting from C++ 11) of class std::string. For example
findAllSubstrings2(s.c_str());
you using string, in function is char try to use char[] s;
use c_str() method in string class when passing the argument
string s;
cin>>s;
findAllSubstrings2(s.c_str());
You probably should change the type of the parameter of the function. Somethink like:
void findAllSubstrings2(string s){
//... function implementation...
}
I know there are a lot of questions like this out there on StackOverflow, but I haven't been able to find any that help resolve my case. Whenever I try to do something like this:
// str = some string or char array
// some magic to get around fpermissive errors
stringstream convert;
convert << str;
// capture the stream's temporary string
const string store = convert.str();
// get a manageable array
const char* temp = store.c_str();
and then try to do something like atoi(temp[0]), I keep getting the classic conversion error that char couldn't be converted to const char. In the documentation for atoi and many other functions, const char is a required parameter. How can a char be sent in if there's only a const one? Does retrieving a char at a specific position auto-cast to char?
I'm not sure if this is what is causing the error, but atoi takes as its parameter not a char, but the pointer to one. So instead of
atoi(temp[0])
try this
atoi(&temp[0])
as that is a pointer.
I have a string array and an integer array. I want to convert the elements of string array to integer and then store them in the integer array. I wrote this code :
string yuzy[360];
int yuza[360];
for(int x = 0;x<360;x++)
{
if(yuzy[x].empty() == false)
{
yuza[x]=atoi(yuzy[x]);
cout<<yuza[x]<<endl;
}
else
continue;
}
this piece of code gives this error:
error: cannot convert 'std::string {aka std::basic_string}' to 'const char*' for argument '1' to 'int atoi(const char*)'
When I write the content of the string (-75dbm) in atoi function it works fine. But when I write (yuzy[x]), I get the error. How can I make atoi works well with string array?
Thanks.
atoi() takes C strings (char pointers) and not C++ string objects. Use
atoi(yuzy[x].c_str());
instead.
As an alternative to atoi, you could use std::stoi and related functions, if you have C++11 support.
yuza[x] = std::stoi(yuzy[x]);
atoi accept a c-style string as parametter, so, you could use atoi(yuzy[x].c_str());
I am using the following code in order to reverse a char array. My code as well as the error can be found below.
My code:
char * reverseStr(char* s) {
int i=0; //legnth of string
while(s[i]) i++;
char reversed[i];
for(int j=0; j<i; j++) {
reversed[j] = s[i-j - 1]; //look at this later
}
return *(reversed);
}
The error:
Compiling...
Compile error: your program did not compile correctly:
program.c: In function 'char* reverseStr(char*)':
program.c:18: error: invalid conversion from 'char' to 'char*'
--> 17: }
--> 18: return *(reversed);
Thank you in advance!
Your return value and type is wrong.
Furthermore, your declaration of reversed is invalid and would leak memory in any case.
Also, calculating the string length instead of using std::strlen isn’t recommended and the standard library has the std::reverse function to reverse strings.
Well, you are returning a char instead of a char*, so you are only returning the first letter in the reversed string instead of a string. Which causes your error messages, because you try to treat a char as a char*.
Check the error message:
program.c: In function 'int itoa2(int, char*, int)':
program.c:45: error: invalid conversion from 'char' to 'const char*'
It clearly tells you what the error is: invalid cast from const char* to char
In your code i is not const.
char reverseStr(char* s) {
int i=0; // --->> NOT CONST
while(s[i]) i++;
char reversed[i];
for(int j=0; j<i; j++) {
reversed[j] = s[i-j - 1]; //look at this later
}
return *(reversed);
}
char reversed[i]; ---> Variable Length Array in C++?? i is supposed to be known at Compile time.
strcpy receives (char*, const char*) as parameters.However, the return type of your function is char, thus the error appears.
And char reversed[] is allocated on the stack of the function, please don't use it as a return value.