Loop Problem

Started by
4 comments, last by Zahlman 15 years, 8 months ago
I have a question as to why I am getting a strange situation in a loop. The program does what it is suppose to do but because I am looping through the program again the getline() function somehow finds some input that is still floating around and I cant figure it out. This is a very simple problem just confusing to me because I can't figure it out. I would like the program to rerun and ask for the input before reading whether or not the user would like to continue. Code: #include <iostream> #include <string> #include <cctype> #include <cstdlib> using namespace std; /* Write a program that reasd in a line of text and replaces all four-letter words witht he word "love". * If the four-letter word starts with a capital letter, it should be replaced with "Love" not by "love". * A word is any string consisting of the letters of the alphabet and delimited at each end by a blank, endline, !letter * Program should repeat this action until the user says quit. */ void change_phrase(string& p); int main() { string phrase; bool stop = false; char ans; while(stop == false) { cout << "Enter in a phrase: \n"; getline(cin, phrase); change_phrase(phrase); cout << phrase << endl; cout << "Again? (yes/no): \n"; cin >> ans; if(ans == 'n' || ans == 'N') stop = true; } return 0; } void change_phrase(string& p) { string temp; int index = 0; int lc = 0; int wc = 0; bool spos = false; int pos; while(index < p.length()) { if(isalpha(p[index])) { if(spos == false) { pos = index; spos = true; } lc++; } if(isspace(p[index]) || ispunct(p[index])) { lc = 0; spos = false; pos = 0; } if(lc == 4 && !isalpha(p.at(index+1))) { if(islower(p[pos])) { p.erase(pos, 4); p.insert(pos, "love"); } else { p.erase(pos, 4); p.insert(pos, "Love"); } } index++; } } Output: Enter in a phrase: John will run home. Love love run love. Again? (yes/no): y Enter in a phrase: Again? (yes/no):
Advertisement
The only solution I could come up with is placing a :
fflush(stdin);
right after your getline() statement although I don't know if it works on all compilers or for that matter if it works with getline.
It's just a thought ... you should try it though ... it might work.
You have basically the same problem as in this thread.

Oh, and moved to For Beginners.
The flush didn't work and neither does cin.clear() or any other thing I have attempted to do. Any ideas about how to fix this? I would like when the program loops back to getline to be able to enter in a new phrase.
Found it! It's actually really simple but easy to miss, the buffer needs to be cleared before the call to getline(), by placing a cin.get(ch) to get rid of the annoying '\n' that is created from the input cin >> ans;. Thanks for the posts.
Did you read the thread SiCrane linked? If not, why post a question if you're not going to take advice?

The reason .clear() didn't do anything for you is that it's not for that purpose. It doesn't clear any data out of the stream, but simply resets error flags. fflush(), meanwhile, is for use with C-style IO, and won't mix well with iostreams. (Also, its behaviour on input FILE*'s is unspecified.)

Anyway, clearing out the newline by .get()ting one character is incredibly non-robust.

This topic is closed to new replies.

Advertisement