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==- 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==-
"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);
"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.
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==-
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==- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
|
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,...
|
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...
| |