I am trying to convert a string that I have already parsed with spaces into a int array:
//example of string before parsing
arrElement = "1,2,3";
//parsing
for(int i =0; i < size; i++){
if(arrElements[i] == ','){
arrElements[i] = ' ';
}
}
//string is now "1 2 3"
//trying to convert numbers only into int
stringstream str;
int intCount = 0;
int intNum[size];
for(int i = 0; i < size; i++){
str << arrElements[i];
if(str == " ") {
}
else {
str >> intNum[intCount];
intCount++;
}
}
I am currently getting the result there are five integers reads, instead of the three in the example I made. In addition when I print out the array, I am completely different numbers:
209664128 32764 0 0 0
I sort of understand the issue, but I am new c++ so I could be wrong, and I am unsure how to resolve this issue. Any help would be greatly appreciated.
int intNum[size];-- Ifsizeis a variable (not a constant), then this is not legal C++. C++ requires constants when specifying the number of entries in an array.std::vector<int> intNum(size);