| Pianokey09 (4) | |
|
I have to write a loop that reads in a collection of words and makes a sentence out of them by appending each new word to the string being formed. There are also a few exit condition which are no more than 10 words, 25 letters and a word that ends in a period. We are also supposed to weed out repeat words. I can't figure out how to get the words to attach to each other after they are inputted. Nor can I figure out how to get repeat words to be ignored. Also, after every input the string has to be displayed; For example if "this", "is" and "one." are entered the outputs would be "this", then "this is" and finally "this is one." Please Help! #include <iostream> #include <string> using namespace std; // Constant declarations const int WORD_COUNT = 10; const int LETTER_COUNT = 25; void main() { // Variable declarations string word, sentence; char termination; int wordCount, letterCount, length; while (true) { // program body sentence = ' '; wordCount = 0; letterCount = 0; cout << "Enter a word: "; cin >> word; length = word.length(); termination = word.at(length-1); do { wordCount++; letterCount += length; sentence = ' ' + word + ' '; cout << sentence << endl; cout << "Enter a word: "; cin >> word; }while (termination != '.' && wordCount < WORD_COUNT && letterCount < LETTER_COUNT); } } | |
|
|
|
| HoneyBoy (2) | |
|
to append words to a string just loop: cin >> word; sentence += word; http://www.cplusplus.com/reference/string/string/operator+=/ | |
|
|
|
| Pianokey09 (4) | |
| Thank you. that helps. Do you happen to know how I can test for repeat words and ignore them? | |
|
|
|
| Pianokey09 (4) | |
| Okay so I have gotten to the point where I can make the words append and everything else but the exit parameter for the '.' doesn't worka and I am unsure to make that happen. Any suggestions? | |
|
|
|
| Thumper (310) | |
You're not changing the value of termination anywhere in your while loop body, so termination will never equal '.'. If you want to use that as a while condition, you need to do something in the body to make sure that that condition will be met at some point.Perhaps you mean to check to see if word == '.' instead?
| |
|
|
|