Need help with string splitter.

C

Chris Schumacher

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 = str.substr(strBegin,strLngth);
strBegin = strLocale + 1;
strCount++;
i++;
};

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

}

-==Kensu==-
 
V

Victor Bazarov

Chris Schumacher said:
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 = str.substr(strBegin,strLngth);
strBegin = strLocale + 1;
strCount++;
i++;
};

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

}

-==Kensu==-
 
R

Ron Natalie

Chris Schumacher said:
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);
 
R

Ron Natalie

Chris Schumacher said:
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.
 
S

Simon Saunders

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 = str.substr(strBegin,strLngth); strBegin = strLocale + 1;
strCount++;
i++;
};

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


}
-==Kensu==-
 
C

Chris Schumacher

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==-
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top