468,456 Members | 1,639 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,456 developers. It's quick & easy.

Strings and for loops

Tom
Please help I'm busy with my assigment and I'm stuck on the following:
I must enter a sentence and then replace words in the sentence, this
works fine but I must repeat it in a for loop. the first time the loop
starts it asks for the sentence and then the word it must look for and
then replace it with antoher. The problem is on the second time of the
loop it does not pause at the first question, it shows it but continue
to the last.
please see code below:

int n;
string Sentence, Sub1, Sub2;
int Position;
// We'll do this for 3 strings
for (n = 0; (n < 5); ++n)
{
// Input a sentence and two words
cout << "Enter a sentence: ";
cin >Sentence;
cout << endl;
cout << "Enter a word to search for: ";
cin >Sub1;
cout << "Enter a word to replace it with: ";
cin >Sub2;
// Search for the Sub1 and replace all occurrences
// of it with the Sub2
Position = Sentence.find(Sub1);
while (Position != -1) {
Sentence.erase(Position, Sub1.size());
Sentence.insert(Position, Sub2);
Position = Sentence.find(Sub1);
}

Jul 5 '06 #1
2 1676
TB
Tom skrev:
Please help I'm busy with my assigment and I'm stuck on the following:
I must enter a sentence and then replace words in the sentence, this
works fine but I must repeat it in a for loop. the first time the loop
starts it asks for the sentence and then the word it must look for and
then replace it with antoher. The problem is on the second time of the
loop it does not pause at the first question, it shows it but continue
to the last.
please see code below:

int n;
string Sentence, Sub1, Sub2;
int Position;
// We'll do this for 3 strings
for (n = 0; (n < 5); ++n)
{
// Input a sentence and two words
cout << "Enter a sentence: ";
cin >Sentence;
cout << endl;
cout << "Enter a word to search for: ";
cin >Sub1;
cout << "Enter a word to replace it with: ";
cin >Sub2;
Say that you enter "foxy Brown jumped big doggystyle", then
on the first iteration you get:

Sentence = "foxy"
Sub1 = "brown";
Sub2 = "jumped";

On the second iteration, there is still input data available
so it continues without waiting for more:

Sentence = "big"
Sub1 = "doggystyle"

Look up std::getline() in <string>.
>
// Search for the Sub1 and replace all occurrences
// of it with the Sub2
Position = Sentence.find(Sub1);
while (Position != -1) {
while(Position != std::string::npos) {
>

Sentence.erase(Position, Sub1.size());
Sentence.insert(Position, Sub2);
Position = Sentence.find(Sub1);
}
--
TB @ SWEDEN
Jul 5 '06 #2
Tom wrote:
Please help I'm busy with my assigment and I'm stuck on the following:
I must enter a sentence and then replace words in the sentence, this
works fine
I question this assertion. See below.
but I must repeat it in a for loop. the first time the loop
starts it asks for the sentence and then the word it must look for and
then replace it with antoher. The problem is on the second time of the
loop it does not pause at the first question, it shows it but continue
to the last.
please see code below:

int n;
string Sentence, Sub1, Sub2;
int Position;
Suggestion: Don't declare variables until you need them and can
initialize them, and declare them in as small a scope as possible. This
will prevent some errors and make the code easier to follow. For
instance, the for-loop below could be better written as:

for( int n = 0; n < 5; ++n )

so the n would only exist in the loop itself. You should also put your
string declarations inside the for-loop braces (unless you need them
outside the loop).
// We'll do this for 3 strings
for (n = 0; (n < 5); ++n)
Is it three or five?
{
// Input a sentence and two words
cout << "Enter a sentence: ";
cin >Sentence;
cout << endl;
cout << "Enter a word to search for: ";
cin >Sub1;
cout << "Enter a word to replace it with: ";
cin >Sub2;
As it stands, if you entered "This is fun", your variables would be as
follows:

Sentence = "This"
Sub1 = "is"
Sub2 = "fun"

which is not what you want. What you do want is something like:

getline( Sentence, cin );

(The other lines can stay the same.) Fix that, get the first iteration
working, and then ask a specific question again. But take note of this
FAQ:

http://parashift.com/c++-faq-lite/ho...t.html#faq-5.2

You might also find these helpful:

http://www.parashift.com/c++-faq-lite/input-output.html

Cheers! --M

Jul 5 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by Ravi | last post: by
17 posts views Thread by Noen | last post: by
3 posts views Thread by Edg Bamyasi | last post: by
5 posts views Thread by Colin Savage | last post: by
8 posts views Thread by vonclausowitz | last post: by
3 posts views Thread by Daniel Weinand | last post: by
52 posts views Thread by Paddy | last post: by
95 posts views Thread by hstagni | last post: by
4 posts views Thread by dhinakar_ve | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.