Hello,
I am using an ifstream to read an array of integers and insert it into a vector. The input file is referred to as mFile, and mSize is an integer that is defined earlier in my code.
1 2 3
|
string *mData;
mData = new string[mSize+1];
getline(mFile, *mData, '}');
|
The above code creates a string of a dynamic size mSize+1, then copies the array from mFile to the string. This works fine! However, later in my code I need to go through mData with a for() loop and take all of the elements, separated by but not including the commas, and pass them to a vector<int>. See below:
1 2 3 4 5 6 7 8 9 10
|
string mValue;
for(int i = 0; i <= mSize; i++)
{
if(mData[i]!=",")
mValue.append(*mData,i,1);
else
{ int iValue = atoi(mValue.c_str());
mValue.clear();
mVector.push_back(iValue);
} }
|
I believe my problem comes up in the first if() statement. What I'm trying to do is run through every element in mData, take out the numbers and append them if they belong to a multi-digit integer in the array (for example, append 2 and 3 if the element in the array is 23), and throw out all the commas. However I can't figure out how to compare individual elements of mData to a single char (in this case, the if() statement tries to compare element i to a comma), most likely because of how I initialized mData. The code above simply takes all the data from mData, including the commas, appends it character by character to mValue, and after each character is appended, appends mValue so far to mVector.
My result is that mVector<int> contains "3", "3,", "3,2", "3,2,", "3,2,1", "3,2,1," etc. Which obviously does not work since commas are not integers (believe it or not).
Anyways, point is, I'm in way over my head and making things worse more than anything else ^_^' If anyone knows of an easier way to do this, or can perhaps decipher what I did wrong, any help or insight will be greatly appreciated.
Sadface,
R.Young