### Static const char *Numbers

Hi,
As a hobby C++ programmer I still have a lot to learn

I have the following code lines in my app:

static const char *Numbers = "2400:2401:2402:2403:2404:2405:2406"

.
.
.
strcpy_s(IOCP.InicioStr, Numbers);

This works as it should, but I now need to modify my code and include all numbers from 2400 to 2800 . Is there a way I can do this in a simpler way than writing all 400 numbers, like i.e 2400-2800 in the first line?

rgs
roarkr

You want to write the numbers 2400 to 2800 to a string, with a colon between each number?

Non-standard (uses itoa which is not standard):

 ``12345678910`` ``````string target; char buffer [5]; for (int i=2400; i<2801; ++i) { target = target + itoa(i, buffer, 10); if (i!=2800) // Don't add one right on the end { target = target+":"; } }``````

Standard:

 ``12345678910`` ``````stringstream target; for (int i=2400; i<2801; ++i) { target << i; if (i!=2800) // Don't add one right on the end { target<< ":"; } } string outputString = target.str();``````

You now have a C++ string with the data you want. If you need a C style char pointer, you can get one from the string object.

Last edited on
closed account (9y8C5Di1)
This is the basic concept.

NOTE: i have not included the definition of many of the functions referred to, but i have revealed in their identifiers their purpose. So it should not be too hard to create them.

There are probably other ways of doing this, but this concept works for me.

string *findNumbers(string elem){string *p=new string[number of elements expected];
short y=0;
for(short x=0;elem[x]!='\0';x++){

if(isNumber(elem[x])==true){p[y]="";
for(x++;elem[x]!=':'&&elem[x]!='\0';x++){
unsigned short size=p[y].size();
p[y].resize(size+1,elem[x]);
}
y++;
}
}
return p;
}

int main(){

string *p=findNumbers("200:329:42:");

float numb=convertString(p[0]);

delete[]p;

return 0;
}
Last edited on
Topic archived. No new replies allowed.