CString and array.

O

Oskar

Hi.
I`m new in cpp and i have a litlle problem.
i have a CString from Edit Box (eg."aaa bbb ccc 7327373 d feaf 323 dvjiv
234") and i want to put the data (space separated) into an array.It shuld be
somethign like this.

array1[0]="aaa";
array1[1]="bbb";
....
array1[43653]="234";

The thing is i don`t have any clue how to do it. :|
Thx for help
 
J

JKop

Oskar posted:
Hi.
I`m new in cpp and i have a litlle problem.
i have a CString from Edit Box (eg."aaa bbb ccc 7327373 d feaf 323
dvjiv 234") and i want to put the data (space separated) into an
array.It shuld be somethign like this.

array1[0]="aaa";
array1[1]="bbb";
...
array1[43653]="234";

The thing is i don`t have any clue how to do it. :|
Thx for help

CString is OFF-TOPIC here.

As for an array of strings:

char* p_array_strings[50];

p_array_strings[0] = "Aa";
p_array_strings[1] = "Bb";
p_array_strings[2] = "Cc";
p_array_strings[3] = "Dd";
p_array_strings[4] = "Ee";
p_array_strings[5] = "Ff";


-JKop
 
O

Oskar

U¿ytkownik "JKop said:
CString is OFF-TOPIC here. Dunno why....(?)

As for an array of strings:

char* p_array_strings[50];

p_array_strings[0] = "Aa";
p_array_strings[1] = "Bb";
p_array_strings[2] = "Cc";
p_array_strings[3] = "Dd";
p_array_strings[4] = "Ee";
p_array_strings[5] = "Ff";
I know how to add data to array, but i need function or smth. that get that
data from CString (separates by spaces) and add it do array.

Greetz.
 
T

Thomas Matthews

Oskar said:
Hi.
I`m new in cpp and i have a litlle problem.
i have a CString from Edit Box (eg."aaa bbb ccc 7327373 d feaf 323 dvjiv
234") and i want to put the data (space separated) into an array.It shuld be
somethign like this.

array1[0]="aaa";
array1[1]="bbb";
...
array1[43653]="234";

The thing is i don`t have any clue how to do it. :|
Thx for help

The CString class is a Microsoft Specialty and best discussed
in a Microsoft newsgroup.

If it were an std::string, you could use the "find" method
to look for spaces.

std::string text;
std::string string_array[43653];

string::size_type position(0);
string::size_type end_posn;

end_posn = text.find(" ", posn);
string_array[0] = text.substr(posn, end_posn - posn);
posn = text.find_firs_not_of(" ", end_posn);
end_posn = text.find(" ", posn);
string_array[1] = text.substr(posn, end_posn - posn);

and so on.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
S

Sharad Kala

Oskar said:
Hi.
I`m new in cpp and i have a litlle problem.
i have a CString from Edit Box (eg."aaa bbb ccc 7327373 d feaf 323 dvjiv
234") and i want to put the data (space separated) into an array.It shuld be
somethign like this.

array1[0]="aaa";
array1[1]="bbb";
...
array1[43653]="234";

The thing is i don`t have any clue how to do it. :|
Thx for help

Some suggestions -
1. CString is not a standard C++ class. Use std::string instead (#include
<string>).

string s="abcd efgh ...";

2. Use stringstreams
istringstream istr(s); // Initialize with your string

3. Then don't use arrays, use std::vector class. It's a dynamically growing
array.
vector<string> vec;

4. Extract strings from stringstream.
string temp;
istr >> temp; // temp now contains abcd
vec.push_back(temp); // Push into the vector
istr >> temp; // temp contains efgh
so on...

-Sharad
 
R

Robbie Hatley

Oskar said:
I`m new in cpp and i have a litlle problem.
i have a CString from Edit Box (eg."aaa bbb ccc 7327373 d feaf 323 dvjiv
234") and i want to put the data (space separated) into an array.It shuld be
somethign like this.

array1[0]="aaa";
array1[1]="bbb";
...
array1[43653]="234";

You haven't defined CString or array1.

So I'll assume:

typedef char CString[80];
CString array1[5];

You can use various tricks for separating-out
space-separated "words". My favorite is a stringstream.
Try something like this:

#include <iostream>
#include <cstring>
#include <sstream>

using std::istringstream;
using std::cout;
using std::endl;

int main()
{
typedef char CString[80];
CString array1[5];
CString string1 = "my red dog is handsome";
std::istringstream blat(string1);
char buffer[65];
for (int i=0 ; i<5; ++i)
{
blat >> buffer;
if (!blat) break;
strcpy(array1, buffer);
}
for (int i=0 ; i<5; ++i)
{
cout << array1 << endl;
}
return 0;
}


--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
 
J

JKop

Oskar posted:
Hi.
I`m new in cpp and i have a litlle problem.
i have a CString from Edit Box (eg."aaa bbb ccc 7327373 d feaf 323
dvjiv 234") and i want to put the data (space separated) into an
array.It shuld be somethign like this.

array1[0]="aaa";
array1[1]="bbb";
...
array1[43653]="234";

The thing is i don`t have any clue how to do it. :|
Thx for help


Why not just write a function?

First of all, find out how many spaces there are.
Then define an array via new. Then just take the addresses.


Untest code but you'll get the jist:


char* ReturnStringArray(const char* input_string)
{
size_t length = strlen(input_string);

size_t amount_spaces = 0;

for(size_t i = 0; i < length; ++i)
{
if (input_string == ' ') ++amount_spaces;
}

char* const p_stringarray = new char*[amount_spaces + 1];

p_stringarray[0] = input_string;

unsigned short index = 1;

for(size_t i = 0; i < length; ++i)
{
if (input_string == ' ')
{
p_stringarray[index++] = input_string[i+1];
}
}

p_stringarray[index] = 0;
}


int main()
{
char* p_blah = "aaaa bbbb cccc ffff kkkk";

char* p_stringarray = ReturnStringArray(p_blah);

//Work with it

delete [] p_stringarray;
}


-JKop
 
O

Old Wolf

JKop said:
Oskar posted:
Hi.
I`m new in cpp and i have a litlle problem.
i have a CString from Edit Box (eg."aaa bbb ccc 7327373 d feaf 323
dvjiv 234") and i want to put the data (space separated) into an
array.It shuld be somethign like this.

array1[0]="aaa";
array1[1]="bbb";
...
array1[43653]="234";

The thing is i don`t have any clue how to do it. :|
Thx for help

Why not just write a function?

First of all, find out how many spaces there are.
Then define an array via new. Then just take the addresses.

What an awful idea. Why not make a vector, or some other container.

[I have removed whitespace and unnecessary braces from the quoted text,
so that the message is shorter]
char* ReturnStringArray(const char* input_string)
{
size_t length = strlen(input_string);
size_t amount_spaces = 0;
for(size_t i = 0; i < length; ++i)
if (input_string == ' ') ++amount_spaces;

char* const p_stringarray = new char*[amount_spaces + 1];


"new char*" returns a char**
p_stringarray[0] = input_string;

p_stringarray[0] is a char, input_string is a char const *
unsigned short index = 1;
for(size_t i = 0; i < length; ++i)
if (input_string == ' ')
p_stringarray[index++] = input_string[i+1];


Will screw up when index overflows, use size_t for index
p_stringarray[index] = 0;

Undefined behaviour - writes past the end of p_stringarray[]

Undefined behaviour - no return value
(it isn't clear what you intended to return, either. p_stringarray
is the wrong type.)
int main()
{
char* p_blah = "aaaa bbbb cccc ffff kkkk";
char* p_stringarray = ReturnStringArray(p_blah);
//Work with it
delete [] p_stringarray;
}

You've returned the first member of an array of pointers to the original
string. In order to actually "work with it", you will have to parse
what each pointer points to again, to find the space that ends the word.
Also, this p_stringarray points to non-const char.

All of these problems would never have arisen if you had advised
the use of vector<string> instead.
 
J

JKop

Good analysis. But I wrote that code in about 2 mins, paying minimal
attention to detail. You may also note that I neglected to change the spaces
to '\0'. But then I'd have to make the inputed non-const, or alternatively I
could make a copy of it...



-JKop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top