hate to love

Having an issue with a project. I have to convert four letter words to love. THe issue is my program will let you enter in a sentence, however it has no output. I think it may have something to do with my while look because I am lost as to what is suppose to go there. At first I had it set to look for whitespace using 'isspace', but received an error. So I tried setting it so the the string for a the fourWord is not whitespace and I end up with no output. HEre is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main ( )
{
	string sentence; //user's input
	string fourWord; //find 4 letter word
	string loveSentence; //users sentence after adding the word love
	//string temp;

	cout << "This program will convert four letter words to 'love'." << endl;
	cout << endl;
	cout << "Enter A Sentence: ";
	getline (cin, sentence);

	while (fourWord != " ")
	{
		if (fourWord.length( ) == 4)
		{
			if (isupper (fourWord[0]))
			{
				loveSentence += "Love ";
			}
			else
			{
				loveSentence += "love ";
			}
		}
		else
		{
			loveSentence += fourWord + " ";
		}
	}

	cout << loveSentence << endl;

	return 0;
}
I would imagine it gets stuck in an infinite loop, repeatedly running lines 18 and 33.

Have you tried tracing it to see what the values of loveSentece are?
If undestood it correctly you need to go through each word in the sentence and if that word is 4 characters long replace it with the word "Love", then you need to do something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
getline(cin,sentence);
	//we need to extract each word from the sentence and 
	// check for its size, if it is a four letter word
	// then replace it with "Love"
	// ex: "I hate you cpp haha haha 1234
	//1. if some empty spces in the begining ignore it and
	// find the begining of sentence
	string::size_type pos1= sentence.find_first_not_of(" ",0);
	//2. find the first space from there
	string::size_type pos2=sentence.find_first_of(" ",pos1);
	// between these two pos you will get a word
	// 3. check for the size and replce it
	while(pos1 != string::npos || pos2 != string::npos)
	{
		fourWord= sentence.substr(pos1,pos2-pos1);
		cout << fourWord <<endl;
		string::size_type s=fourWord.size();
		if(s==4)
			sentence.replace(pos1,pos2-pos1,"Love");
		pos1= sentence.find_first_not_of(" ",pos2); // next word begin
		pos2=sentence.find_first_of(" ",pos1); // next word end
	}
	cout << sentence << endl;
You get input from the user and store it in sentence, but you never use or check sentence again?
Of course there is no output. The user types in his/her/it? sentence and it automatically checks the string fourWord without adding anything into fourWord. I don't know exactly how it would be done, but you would need to check the string sentence for four letter words to begin with before even moving on past line 18.
Topic archived. No new replies allowed.