By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Strings and for loops

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.