cin causes infinite loop in my program
-
I am writing this program where I read in a line of text. I am able to enter the text just fine and it stores okay, but then when I go to prompt user for another entry (such as an integer) I get an infinite loop because it is waiting for a valid entry. example: cout << "please enter a string of characters."; cin.getline(tempstringgoeshere, BUFFER); int selection = 0; while(selection < 1 || selection > 5) { cout << "Please make a selection:"; cin >> selection; // DOES NOT LET ME ENTER ANYTHING HERE - GOES PAST } Suggestions? Thanks, G
-
I am writing this program where I read in a line of text. I am able to enter the text just fine and it stores okay, but then when I go to prompt user for another entry (such as an integer) I get an infinite loop because it is waiting for a valid entry. example: cout << "please enter a string of characters."; cin.getline(tempstringgoeshere, BUFFER); int selection = 0; while(selection < 1 || selection > 5) { cout << "Please make a selection:"; cin >> selection; // DOES NOT LET ME ENTER ANYTHING HERE - GOES PAST } Suggestions? Thanks, G
Its probably because in the first getline the BUFFER is too small or the user is entering a string whose length in bigger than BUFFER.
« Superman »
-
I am writing this program where I read in a line of text. I am able to enter the text just fine and it stores okay, but then when I go to prompt user for another entry (such as an integer) I get an infinite loop because it is waiting for a valid entry. example: cout << "please enter a string of characters."; cin.getline(tempstringgoeshere, BUFFER); int selection = 0; while(selection < 1 || selection > 5) { cout << "Please make a selection:"; cin >> selection; // DOES NOT LET ME ENTER ANYTHING HERE - GOES PAST } Suggestions? Thanks, G
I think. In this case, it will go in to loop for characters only. Might be problem with overload of
operator >>
withint
parameter
. It is not handlingchar
properly.Prasad Notifier using ATL | Operator new[],delete[][^]
-
I am writing this program where I read in a line of text. I am able to enter the text just fine and it stores okay, but then when I go to prompt user for another entry (such as an integer) I get an infinite loop because it is waiting for a valid entry. example: cout << "please enter a string of characters."; cin.getline(tempstringgoeshere, BUFFER); int selection = 0; while(selection < 1 || selection > 5) { cout << "Please make a selection:"; cin >> selection; // DOES NOT LET ME ENTER ANYTHING HERE - GOES PAST } Suggestions? Thanks, G
-
I am writing this program where I read in a line of text. I am able to enter the text just fine and it stores okay, but then when I go to prompt user for another entry (such as an integer) I get an infinite loop because it is waiting for a valid entry. example: cout << "please enter a string of characters."; cin.getline(tempstringgoeshere, BUFFER); int selection = 0; while(selection < 1 || selection > 5) { cout << "Please make a selection:"; cin >> selection; // DOES NOT LET ME ENTER ANYTHING HERE - GOES PAST } Suggestions? Thanks, G
Spherelin wrote:
cin.getline(tempstringgoeshere, BUFFER);
What is the value of
BUFFER
and how many characters are you entering? You'll quickly find thattempstringgoeshere
is not large enough. Use astring
type instead of achar
type.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
I am writing this program where I read in a line of text. I am able to enter the text just fine and it stores okay, but then when I go to prompt user for another entry (such as an integer) I get an infinite loop because it is waiting for a valid entry. example: cout << "please enter a string of characters."; cin.getline(tempstringgoeshere, BUFFER); int selection = 0; while(selection < 1 || selection > 5) { cout << "Please make a selection:"; cin >> selection; // DOES NOT LET ME ENTER ANYTHING HERE - GOES PAST } Suggestions? Thanks, G
Unless your input is an array of strings then use cin>>tempstring otherwise use cin.getline(tempstring, MAXBUFFERSIZE, '\n'); this function takes 3 parameters, and the last one is the delimiter i.e. when to signal the function to return.