473,387 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Need help with string splitter.

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
5 3844
"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

"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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tim Streater | last post by:
I have this: splitter = //; dateItems = dateString.split (splitter, 3); where dateString might contain such as 3.4.5 or 3/4/5 or 3-4-5. But it might also be nullstring or any junk the user...
7
by: John | last post by:
Hi, I have a group of controls in a panel on the right-hand side of a splitter and a treeview on the left-hand side. The only configuration that I can find to make the right-hand side resize...
1
by: Krish | last post by:
This is my first try with C# windows gui devlopment, is it correct to say that gui development in C# is windows forms. I would like to create a form with two sections, 2 datagrids, I would like...
4
by: BBM | last post by:
I'm trying to set up a form with two panels divided by a Splitter control. I can make the Splitter work in the situations described in the documentation (Listbox or TreeView on left w/Dock set to...
1
by: B | last post by:
Hi, I would like to split up an mdi form using a splitter where a datagrid would be on the left side and child forms on the right side. So far, I have placed a splitter on the form, dragged it...
5
by: Sam | last post by:
Hi, I have a panel docked to the bottom of my form. This panel can be expanded vertically by clicking on a button. When the user click on the button again, the panel is then collapsed. The panel...
1
by: JDeats | last post by:
I have a simple WinForm with a WinForms splitter down the middle. I would like to make it so when the user clicks on a button inside the left portion of the screen (the panel to the splitters left)...
1
by: MrQ | last post by:
I have a problem with tree control and i don't know how to solve it, maybe someone can help me. What i'm trying to do is display widgets in a staticbox plus a help text when i select different items...
2
by: BjornB | last post by:
Hi! I'm totally new to Python and I'm jus nw trying to create my first application with wxPython, exciting! However, I have a problem: I'm creating a Frame with a menu and buttonpanel...
11
by: askalottaqs | last post by:
i created a function which u send a string, and it tokenizes according to spaces, so i send it a string for it to return me a string array, but on the declaration of the function it says error...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.