thanks hombre.. sounds like the moral of the story is.. 'thou shalt not attempt to modify a hard-coded statically declared string literal.."
Second Question:
Why does the following boolean test fail?
Code:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int iCount = 3;
char **Lines = new char*[3];
//Lines = {{"H1001, "1-24"}, {"A, 25-36"}, {"H1007, 36-45"}};
Lines[0] = new char[strlen("H1007, 1-24\0")];
Lines[1] = new char[strlen("A, 25-36\0")];
Lines[2] = new char[strlen("H1007, 36-47\0")];
strcpy(Lines[0], "H1007, 1-24\0");
strcpy(Lines[1], "A, 25-36\0");
strcpy(Lines[2], "H1007, 36-47\0");
for(int i=0; i<3; i++)
cout << Lines[i] << endl;
char **Name = new char*[iCount];
char **FiberCount1 = new char*[iCount];
char **FiberCount2 = new char*[iCount];;
int /*size,*/ word_index;
bool cont = true;
i=0;
for(int j=0; i<iCount; i++, j=0)
{
word_index = 0;
cont = true;
do{
//Extract Fiber Name
while(Lines[i][j]!=',')
{
j++;
}
Lines[i][j]='\0';
Name[i] = new char[j-word_index+1];
strcpy(Name[i], Lines[i]+word_index);
//Name[i][j-word_index] = '\0';
word_index = j;
word_index++;
//Extract Beginning Fiber Count
while(Lines[i][j]!='-')
{
j++;
}
Lines[i][j] = '\0';
FiberCount1[i] = new char[j-word_index+1];
strcpy(FiberCount1[i], Lines[i]+word_index);
//FiberCount1[i][j-word_index] = '\0';
word_index = j;
word_index++;
//Extract Ending Fiber Count - Code works fine up until this point
//Even when the null character is reached.. the loop continues infinately
while(Lines[i][j]!=',' || Lines[i][j]!='\0')
{
j++;
}
if(Lines[i][j]=='\0')
cont = false;
Lines[i][j] = '\0';
FiberCount2[i] = new char[j-word_index+1];
strcpy(FiberCount2[i], Lines[i]+word_index);
//FiberCount2[i][j-word_index] = '\0';
}while(cont);
}
return 0;
}
When viewed through the debugger.. it appears that even when Lines[i][j] equals NULL.. the loop continues on..?!?@
*just for fun.. I just threw in a comma in place of the null character.. and the debugger saw the comma.. but the loop continued to loop..