Convert vector<string> * char to int - c++

I need help in my following code and hope that you can help me through. All I wanted is to pass in INT type to setX() and setY(). However, there is no way for me to convert vector char* to int. Is there alternative to this?
template<class T>
vector<string> Delimiter(T inputString){
int count=0;
char str[inputString.length()];
strcpy(str,inputString.c_str());
char * pch;
vector<string> returnContainer;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str,",[]");
while (pch != NULL)
{
returnContainer.push_back(pch);
pch = strtok (NULL, " ,[]");
count++;
}
for(int i=0; i<returnContainer.size(); i++){
cout << "return:" << returnContainer[i] << endl;
}
return returnContainer;
}
//Main()
fileDataAfterFiltered = Delimiter(fileData[i]); // Delimiter (vector<string> type)
point2DObj[point2DCount].setX(fileDataAfterFiltered[1]); // error
point2DObj[point2DCount].setY(fileDataAfterFiltered[2]); // error
//Assn3.cpp:107:59: error: no matching function for call to ‘Point2D::setX(std::basic_string&)’

Delimiter() returns a vector<string> and you give one of these strings to setX() and setY(), but both expect an integer parameter. You must convert the string to int
int x = atoi(fileDataAfterFiltered[1].c_str());
point2DObj[point2DCount].setX(x);
int y = atoi(fileDataAfterFiltered[2].c_str());
point2DObj[point2DCount].setY(y);
But: in C++ array and vector elements start at 0 not 1, so you might want to replace this with fileDataAfterFiltered[0] and fileDataAfterFiltered[1] respectively.

If you are using a C++11 compiler, function std::stoi() will do the trick:
point2DObj[point2DCount].setX(std::stoi(fileDataAfterFiltered[1]));
Otherwise you can use the old atoi():
point2DObj[point2DCount].setX(atoi(fileDataAfterFiltered[1].c_str()));
Aside from this, your code has many other problems, but I hope you can fix them by yourself.

there's plenty of ways of converting string to int. boost::lexical_cast is one which will magically do the conversion you want. Otherwise you can use atoi (if you don't care about errors), or strtol (if you do).
point2DObj[point2DCount].setX(atoi(fileDataAfterFiltered[1].c_str()));
point2DObj[point2DCount].setX(boost::lexical_cast<int>(fileDataAfterFiltered[1]));

Related

Passing pointer to char* array into function to manipulate values on Arduino

[PLEASE CHECK FINAL EDIT BELOW FOR UPDATE]
My C++ is a bit rusty (to say the least) and I'm having an issue trying to pass a char array into a function to manipulate the values. Example code below:
void myFunction(char* splitStrings,String stringToSetInLoop) {
char substringPtr[stringToSetInLoop.length()];
stringToSetInLoop.toCharArray(substringPtr, stringToSetInLoop.length());
for(int i = 0; i < 10; i++) {
splitStrings[i] = *substringPtr;
}
}
char *mySplitStrings[10];
myFunction(*mySplitStrings,String("Repeat Me"));
Serial.println(mySplitStrings[0]);
The code does not crash, but it outputs a blank line. I suspect that I need to initialize a 2 dimensional array outside the function to pass in so that memory space is allocated. I'm guessing that, although the substring pointer exists inside the function, the memory is destroyed, leaving the char* array mySplitStrings[0] pointing at nothing. Also, I think I need to pass in the reference to the array memory space, not as a pointer.
The ultimate goal here is to be able to pass a char array into a function, assign some values to it, then use those values back in the main code loop. If there's a better way to achieve this, then please let me know.
Thanks in advance. Please free me from my personal pointer/reference hell!
EDIT: Further note, this code is being run on an arduino, so the C++ is limited.
EDIT: When I try to pass in a reference to the char* pointer, I get this error, which I'm not sure how to change the function parameters to fix: error: cannot convert char* ()[10] to char for argument 1 to void myFunction(char*, String). Can anybody please take a stab at showing me a working example?
EDIT:
Thanks to the responses... I now have a working static library function that splits strings passed as a char* array. I know it's not pretty, but it does work. Thanks you to those who contributed. Code below:
void ExplodeString::explode(char* explodeResults[], String str, String delimiter) {
int delimiterPosition;
int explodeResultsCounter=0;
String subString;
do {
delimiterPosition = str.indexOf(delimiter);
if(delimiterPosition != -1) {
subString = str.substring(0,delimiterPosition);
char *subStringPtr[subString.length()+1];
subString.toCharArray(*subStringPtr, subString.length()+1);
explodeResults[explodeResultsCounter++] = strdup(*subStringPtr);
str = str.substring(delimiterPosition+1, str.length());
} else { // here after the last delimiter is found
if(str.length() > 0) {
subString = str;
char *subStringLastPtr[subString.length()+1];
subString.toCharArray(*subStringLastPtr, subString.length()+1);
explodeResults[explodeResultsCounter++] = strdup(*subStringLastPtr);
}
}
} while (delimiterPosition >=0);
}
Usage:
char* explodeResults[10];
ExplodeString::explode(explodeResults, String("cat:dog:chicken"), String(":"));
Serial.println(explodeResults[0]);
Serial.println(explodeResults[1]);
Serial.println(explodeResults[2]);
EDIT: Man, this is sooo much easier when you use the stdlib:
void ExplodeString::explode(std::vector<std::string> &explodeResults, std::string str, char delimiter) {
std::stringstream data(str);
std::string line;
while(std::getline(data,line,delimiter))
{
explodeResults.push_back(line);
}
}
Usage:
std::vector<std::string> commandsResult;
char delimiter[] = ",";
std::string _inputString = "my,string,to,parse";
ExplodeString::explode(commandsResult, _inputString, delimiter[0]);
How to pass an array of char*:
void myFunction(char* splitStrings[10], String stringToSetInLoop) {
// ...
char *mySplitStrings[10];
myFunction(mySplitStrings, String("Repeat Me"));
This will also work:
void myFunction(char* splitStrings[], String stringToSetInLoop) {
and this:
void myFunction(char** splitStrings, String stringToSetInLoop) {
Also, seems there is STL for avr platform - include it, C++ without STL is smth strange.
You are not allocating space for character arrays and just passing pointer of character array.
Instead of using char*splitStrings[10], you can use 2d char array with sufficient space to accomodate max length string. Assuming you max string length is less that 64 you can do something like this.
char splitString[10][64];
void myFunction(char**splitStrings,String stringToSetInLoop)
or
void myFunction(char splitString[][64], String stringToSetInLoop)
{
int len = stringToSetInLoop.length();
for(int i = 0; i<10; i++)
{
for(int j = 0; i<len; j++)
{
splitString[i][j] = stringToSetInLoop.charAt(j);
}
}
}

Segmentation Fault in C++ most probably in strtok operation

I was writing a postfix evaluation program which could handle multiple digits. Therefore I first read the inputs to a character array, and split them to array of (character array). Whenever I run my program, it gives a segmentation error (g++ on Ubuntu 13.10)
Here's my function to split
int split(char str[], char *ret[]){
int c=0;
char * pch;
pch = strtok (str," ");
while (pch != NULL)
{
//printf ("%s\n",pch);
strcpy(ret[c], pch); c++;
//cout<<ret[c];
pch = strtok (NULL, " ");
}
return c;
}
And part of my main()
char* s;
s = new char[200];
cout<<"Enter Postfix Expression:\n > ";
cin.getline(s,200);
char* st[200];
//int size=3;
int size = split(s, st); // < Is what I'm passing correct?
I could not understand why the segfault occurred. Any suggestions?
EDIT: Thanks to yugnum for the answer. The following did the trick
...
ret[c] = new char[strlen(pch)];
strcpy(ret[c], pch); c++;
...
char* st[200];
int size = split(s, st);
int split(char str[], char *ret[]){
...
strcpy(ret[c], pch);
...
}
this is what is wrong, you just pass bunch of uninitialized pointers to strcpy while it requires pointer that points to valid allocated memory.
You need to define st as 2D array, so split function will return an array of split strings
For example:
char st[20][200]
and then:
int split(char str[], char *ret[][]){
So you'll have 20 of 200 byte char array.
But it's not safe at all, I suggest you considering something else like vector or std::string. Also you need to allocate your array before strcpy.
Then
strcpy(ret[c], pch);
with c as counter, will copy split new string to your char array.
But consider std::string, which is best choice

Why does this combination of strtol and strtok not work?

Could anyone tell me what is wrong with this code?
for(int i=0;i<4;i++)
{
long int a = strtol(strtok("1-2-3-4","-"),(char**)NULL,10);
cout << a <<endl
}
I'm running on Solaris Unix. It's giving me a segmentation fault.
The fault is in strtol().
The error is with the strtok call, not with strtol. You can't call strtok on a string literal, since it's going to try to modify the string. Modifying a string literal causes undefined behaviour in C++.
The problems are legion.
I would expect that the core dump is because the string "1-2-3-4" is stored in read-only memory, so when strtok() modifies it (to isolate the first token), the program crashes. You say the crash is in strtol(); that would suggest that the return value from strtok() is NULL.
The first call to strtok() uses the string as an argument; the second call passes NULL in its place to indicate 'continue where you left off last time'. As written, if the string was modifiable, then you'd parse 1 four times.
This is closer to correct (though untested):
char input[] = "1-2-3-4";
char *data = input;
for (int i = 0; i < 4; i++)
{
char *token = strtok(data, "-");
if (token != 0)
{
long int a = strtol(token, NULL, 10);
cout << a << endl;
}
data = NULL;
}
In general, you need to do error detection from strtol(); further, doing so is quite fraught. However, with the sample string, you would not have to worry about that.
As the problem is already discussed, I'd like to show an alternate approach :
#include <stdio.h>
#include <string.h>
int main ()
{
long int a;
char str[] ="1-2-3-4";
char * pch;
pch = strtok (str,"-");
while (pch != NULL)
{
a = strtol(pch,(char**)NULL,10);
cout << a <<endl;
pch = strtok (NULL, "-");
}
return 0;
}

invalid conversion from 'char' to 'char*' using strcpy

Ok so here are the parts of my code that I'm having trouble with:
char * historyArray;
historyArray = new char [20];
//get input
cin.getline(readBuffer, 512);
cout << readBuffer <<endl;
//save to history
for(int i = 20; i > 0; i--){
strcpy(historyArray[i], historyArray[i-1]); //ERROR HERE//
}
strcpy(historyArray[0], readBuffer); //and here but it's the same error//
The error that i'm receiving is:
"invalid conversion from 'char' to 'char*'
initializing argument 1 of 'char* strcpy(char*, const char*)'
The project is to create a psudo OS Shell that will catch and handle interrupts as well as run basic unix commands. The issue that I'm having is that I must store the past 20 commands into a character array that is dynamically allocated on the stack. (And also de-allocated)
When I just use a 2d character array the above code works fine:
char historyArray[20][];
but the problem is that it's not dynamic...
And yes I do know that strcpy is supposed to be used to copy strings.
Any help would be greatly appreciated!
historyArray points to (the first element of) an array of 20 chars. You can only store one string in that array.
In C, you could create a char** object and have it point to the first element of an array of char* objects, where each element points to a string. This is what the argv argument to main() does.
But since you're using C++, it makes a lot more sense to use a vector of strings and let the library do the memory management for you.
Stop using C idioms in a C++ program:
std::deque<std::string> historyArray;
//get input
std::string readBuffer;
std::getline(std::cin, readBuffer);
std::cout << readBuffer << std::endl;
//save to history
historyArray.push_front(readBuffer);
if(historyArray.size() > 20)
historyArray.pop_back();
As a result, we have:
No buffer-overflow threat in readBuffer / getline()
No pointers, anywhere, to confuse us.
No arrays to overstep the ends of
Arbitrarily long input strings
Trivially-proven memory allocation semantics
Two solutions. The first is if you for some reason really want arrays, the other is more recommended and more "C++"ish using std::strings.
char * historyArray[20]; // Create an array of char pointers
// ...
historyArray[i] = new char[SIZE]; // Do this for each element in historyArray
Then you can use strcpy on the elements in historyArray.
Second solution which I repeat is recommended (I've fixed a few other things):
string historyArray[20];
getline(cin, readBuffer); // Make readbuffer an std::string as well
cout << readBuffer << endl;
for(int i = 19; i > 0; i--){ // I think you meant 19 instead of 20
historyArray[i] = historyArray[i-1];
}
historyArray[0] = readBuffer;
historyArray[i] is a char. It is a single character. You want to use a sting. Your fundemental problem is that historyArray is a char* which means that it points to a memory range containing characters. You want it to be a char** which is a pointer to a pointer to a string. Your initialization code would be
char** historyArray;
historyArray = new char* [20];
for (int i = 0; i < 20; i++)
{
historyArray[i] = new char [512]; //Big enough to have a 512 char buffer copied in
}
Error 1: You're indexing past your array bounds with i being set to 20.
Error 2: historyArray[i] is a char, not a char *. You need &historyArray[i].
strcpy(&historyArray[i], &historyArray[i-1]);
Array notation gives references while strcopy wants pointers. Convert references to pointers with address-of (&) operator.
char * historyArray;
historyArray = new char [20];
//get input
cin.getline(readBuffer, 512);
cout << readBuffer <<endl;
//save to history
for(int i = 20; i > 0; i--){
strcpy(&(historyArray[i]), &(historyArray[i-1])); //ERROR HERE//
}
strcpy(historyArray, readBuffer); //and here but it's the same error//
But that will only fix the compiler errors, not the logical errors in the code. Your using C++ so the string solution:
vector<string> history;
cin.getline(readBuffer,512);
history.push_back(readBuffer);
Alternatively if you want one long string containing everything from readBuffer:
string history;
cin.getline(readBuffer,512);
history = history += string(readBuffer);
For example...

Can getline() be used to get a char array from a fstream

I want to add a new (fstream) function in a program that already uses char arrays to process strings.
The problem is that the below code yields strings, and the only way i can think of getting this to work would be to have an intermediary function that would copy the strings, char by char, into a new char array, pass these on to the functions in the program, get back the results and then copy the results char by char back into the string.
Surely (hopefully) there must be a better way?
Thanks!
void translateStream(ifstream &input, ostream& cout) {
string inputStr;
string translated;
getline(input, inputStr, ' ');
while (!input.eof()) {
translateWord(inputStr, translated);
cout << translated;
getline(input, inputStr, ' ');
}
cout << inputStr;
the translateWord func:
void translateWord(char orig[], char pig[]) {
bool dropCap = false;
int len = strlen(orig)-1;
int firstVowel = findFirstVowel(orig);
char tempStr[len];
strcpy(pig, orig);
if (isdigit(orig[0])) return;
//remember if dropped cap
if (isupper(orig[0])) dropCap = true;
if (firstVowel == -1) {
strcat(pig, "ay");
// return;
}
if (isVowel(orig[0], 0, len)) {
strcat(pig, "way");
// return;
} else {
splitString(pig,tempStr,firstVowel);
strcat(tempStr, pig);
strcat(tempStr, "ay");
strcpy(pig,tempStr);
}
if (dropCap) {
pig[0] = toupper(pig[0]);
}
}
You can pass a string as the first parameter to translateWord by making the first parameter a const char *. Then you call the function with inputStr.c_str() as the first parameter. Do deal with the second (output) parameter though, you need to either completely re-write translateWord to use std::string (the best solution, IMHO), or pass a suitably sized array of char as the second parameter.
Also, what you have posted is not actually C++ - for example:
char tempStr[len];
is not supported by C++ - it is an extension of g++, taken from C99.
You can use the member function ifstream::getline. It takes a char* buffer as the first parameter, and a size argument as the second.