Sign in to follow this  
toogreat4u

Loop Problem

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this