Loop While String Length > 0

C

csmith8933

I'm trying to loop until the string length is 0, deleting one
character at a time. This doesnt work for obvious reasons I'm sure.

string s;
s = "0000011111";

while (s.length() > 0)
{
functionOne(s);
}

string functionOne(string str)
{
cout << str << endl;

if (str.length() > 0)
{
str.erase(0, 1);
return str;
}
}
 
D

DerekBaker

* (e-mail address removed):
I'm trying to loop until the string length is 0, deleting one
character at a time. This doesnt work for obvious reasons I'm sure.

string s;
s = "0000011111";

while (s.length() > 0)
{
functionOne(s);
}

string functionOne(string str)
{
cout << str << endl;

if (str.length() > 0)
{
str.erase(0, 1);
return str;
}
}

functionone returns a string, the calling program does not catch it.

s = functionone(s);

Or as you have it now, but use a reference:

string functionOne(string& str)


On a different point add a space to the signature separator: "-- " not "--".
 
C

csmith8933

Thanks Derek, but I'm still getting an endless loop. Do you see
anything else wrong with it?
 
R

Ron Natalie

I'm trying to loop until the string length is 0, deleting one
character at a time. This doesnt work for obvious reasons I'm sure.

string s;
s = "0000011111";

while (s.length() > 0)
{

In addition to the other mentioned deficiencies in your program,
it's probably a good habit to get into calling empty() rather
than comparing the size to zero on containers. On some containers
it takes more than constant time to compute the size.
 
T

terminator

I'm trying to loop until the string length is 0, deleting one
character at a time. This doesnt work for obvious reasons I'm sure.

string s;
s = "0000011111";

while (s.length() > 0)
{
functionOne(s);

}
the following function is passed a value and returns a value:
string functionOne(string str)
{
cout << str << endl;

if (str.length() > 0)
{
str.erase(0, 1);
return str;
}

}

I`d rather make it refrence for both the parameter and the return:

string& functionOne(string& str);

cheers,
FM.
 

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

Similar Threads

While Loop Freezing? 1
TF-IDF 1
Passing flask textbox value to an infinite while loop 0
string which embed '\0' char 12
handle \x0 character in a string 6
Crossword 14
Collect Excel Data from Website 5
ChatBot 4

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top