By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

Need help with string splitter.

P: n/a
I'm working on a program which splits a long string into a series of
smaller ones. It is supposed to work as follows;
1. You enter a string into variable "str"
2. The program searches for the space character and cuts everything
between the start-point and that space into a segment of the "thread"
array.
3. The program prints out the string again by printing out each
occupied segment of the array, split up by a space.

As of this moment the program returns only the first word and shuts
down. I think it might be a problem with the While loop, but I don't
see what's wrong, or really any other way to make it work.
[Note: the problem is in the While loop, I can assure you, I dummied
the For loop and checked out the array with a series of cout
statements, and it stops copying data into the array after the first
entry]

Complete code follows, any help is greatly appreciated.

#include <iostream>
#include <string>
using namespace std;

int main()
{
int strBegin=0, strLocale=0, i=1, strCount=0, j, strLngth ;
string str, thread[100];
cout << "Pull the string!";
cin >> str;
while(strLocale != string::npos)
{
strLocale = str.find(" ",strBegin);
strLngth = strLocale-strBegin;
thread[i] = str.substr(strBegin,strLngth);
strBegin = strLocale + 1;
strCount++;
i++;
};

for (j = 1; j <= strCount; j++)
cout << thread[j] << " ";
return 0;

}

-==Kensu==-
Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Chris Schumacher" <ke*****@hotmail.com> wrote...
I'm working on a program which splits a long string into a series of
smaller ones. It is supposed to work as follows;
1. You enter a string into variable "str"
2. The program searches for the space character and cuts everything
between the start-point and that space into a segment of the "thread"
array.
3. The program prints out the string again by printing out each
occupied segment of the array, split up by a space.

As of this moment the program returns only the first word and shuts
down. I think it might be a problem with the While loop, but I don't
see what's wrong, or really any other way to make it work.
[Note: the problem is in the While loop, I can assure you, I dummied
the For loop and checked out the array with a series of cout
statements, and it stops copying data into the array after the first
entry]

Complete code follows, any help is greatly appreciated.

#include <iostream>
#include <string>
using namespace std;

int main()
{
int strBegin=0, strLocale=0, i=1, strCount=0, j, strLngth ;
string str, thread[100];
cout << "Pull the string!";
cin >> str;
Check what you get in 'str' here. Hint: cin stops reading
when encounters a space.

To fix: read about 'std::getline' function.
while(strLocale != string::npos)
{
strLocale = str.find(" ",strBegin);
strLngth = strLocale-strBegin;
thread[i] = str.substr(strBegin,strLngth);
strBegin = strLocale + 1;
strCount++;
i++;
};

for (j = 1; j <= strCount; j++)
cout << thread[j] << " ";
return 0;

}

-==Kensu==-

Jul 19 '05 #2

P: n/a

"Chris Schumacher" <ke*****@hotmail.com> wrote in message news:ok********************************@4ax.com...
cout << "Pull the string!";
cin >> str;

This doesn't read a line, it reads one word (stops at the first whitespace).
Try
getline(cin, str);
Jul 19 '05 #3

P: n/a

"Chris Schumacher" <ke*****@hotmail.com> wrote in message news:ok********************************@4ax.com...
while(strLocale != string::npos)
{ };

By the way, the ; here is spurious. It doesn't terminate the while, it's an extra
null statment. Get out of this habit, you'll get unexpected results if your control
structure was a bit more complex.
Jul 19 '05 #4

P: n/a
On Thu, 13 Nov 2003 20:41:35 +0000, Chris Schumacher wrote:
I'm working on a program which splits a long string into a series of
smaller ones. It is supposed to work as follows; 1. You enter a string
into variable "str" 2. The program searches for the space character and
cuts everything between the start-point and that space into a segment of
the "thread" array.
3. The program prints out the string again by printing out each occupied
segment of the array, split up by a space.

As of this moment the program returns only the first word and shuts
down. I think it might be a problem with the While loop, but I don't see
what's wrong, or really any other way to make it work. [Note: the
problem is in the While loop, I can assure you, I dummied the For loop
and checked out the array with a series of cout statements, and it stops
copying data into the array after the first entry]

Complete code follows, any help is greatly appreciated.

#include <iostream>
#include <string>
using namespace std;

int main()
{
int strBegin=0, strLocale=0, i=1, strCount=0, j, strLngth ; string str,
thread[100];
cout << "Pull the string!";
cin >> str;
The problem is actually in the above line, nothing to do with the while
loop. Try printing out str at this point and you'll see what I mean. The
extraction operator stops when it sees whitespace. Try using getline(cin,
str) instead.
while(strLocale != string::npos)
{
strLocale = str.find(" ",strBegin);
strLngth = strLocale-strBegin;
thread[i] = str.substr(strBegin,strLngth); strBegin = strLocale + 1;
strCount++;
i++;
};

for (j = 1; j <= strCount; j++)
cout << thread[j] << " ";
return 0;
}
-==Kensu==-

Jul 19 '05 #5

P: n/a
On Thu, 13 Nov 2003 21:09:16 GMT, "Victor Bazarov"
<v.********@comAcast.net> wrote:
Check what you get in 'str' here. Hint: cin stops reading
when encounters a space.

To fix: read about 'std::getline' function.


Yikes! Geez, this is embarassing.
Thanks!
(goes and hides)
-==Kensu==-
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.