Strings and for loops

T

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);


}
 
M

mlimber

Tom said:
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/how-to-post.html#faq-5.2

You might also find these helpful:

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

Cheers! --M
 
T

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 said:
// 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) {
 

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

Members online

Forum statistics

Threads
474,262
Messages
2,571,050
Members
48,769
Latest member
Clifft

Latest Threads

Top