cin in do while loop

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2009
Posts: 3
Reputation: l3vi is an unknown quantity at this point 
Solved Threads: 0
l3vi l3vi is offline Offline
Newbie Poster

cin in do while loop

 
1
  #1
May 8th, 2009
Been developing for years under web based services like PHP and SQL, and I thought it would be a good idea that I come back and learn C++. Been enjoying it, but I got this one thing that is just driving me nuts.

In the do while loop after the cin is set on the first loop it goes into a death spin. In the 2nd example posted below it works just fine. Its as if the cin does not take a 2nd input after the first loop and just assumes its the same as the first input.

  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. class customerClass {
  8. string masterArray[2][255];
  9.  
  10. public:
  11. void getActions(int);
  12. void saveInArray(int, string);
  13. string readArray(int,int);
  14. };
  15. string customerClass::readArray(int x, int y) {
  16. cout << masterArray[x][y];
  17. }
  18. void customerClass::saveInArray(int a, string sx) {
  19. switch(a) {
  20. case 1:
  21. masterArray[a][0] = sx;
  22. break;
  23. }
  24. }
  25. void customerClass::getActions(int actions) {
  26. string uInput;
  27. switch(actions) {
  28. case 1:
  29. cout << "Options: (2) Add Name, (3) Add Address, (0) Exit: ";
  30. break;
  31. case 2:
  32. cout << "Customer Name ";
  33. getline(cin, uInput);
  34. saveInArray(1,uInput);
  35. break;
  36. case 3:
  37. cout << "Customer Address: ";
  38. getline(cin,uInput);
  39. break;
  40. default:
  41. // exit(1);
  42. break;
  43. }
  44. }
  45. int main() {
  46. customerClass rect;
  47. int n;
  48. rect.getActions(1);
  49. do {
  50. cin >> n;
  51. rect.getActions(n);
  52. }while(n!=0);
  53. return 0;
  54. }

2nd code test that works fine:
  1. // number echoer
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int main ()
  7. {
  8. unsigned long n;
  9. do {
  10. cout << "Enter number (0 to end): ";
  11. cin >> n;
  12. cout << "You entered: " << n << "\n";
  13. } while (n != 0);
  14. return 0;
  15. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: cin in do while loop

 
0
  #2
May 8th, 2009
This may help--

  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8. class customerClass {
  9. string masterArray[2][255];
  10.  
  11. public:
  12. void getActions(int);
  13. void saveInArray(int, string);
  14. string readArray(int,int);
  15. };
  16. string customerClass::readArray(int x, int y) {
  17. cout << masterArray[x][y];
  18. }
  19. void customerClass::saveInArray(int a, string sx) {
  20. switch(a) {
  21. case 1:
  22. masterArray[a][0] = sx;
  23. break;
  24. }
  25. }
  26. void customerClass::getActions(int actions) {
  27. stringstream ss (stringstream::in | stringstream::out);
  28. string uInput;
  29. switch(actions) {
  30. case 1:
  31. cout << "Options: (2) Add Name, (3) Add Address, (0) Exit: ";
  32. break;
  33. case 2:
  34. cout << "Customer Name ";
  35. cin >> uInput;
  36. ss << uInput;
  37. saveInArray(1,ss.str());
  38. break;
  39. case 3:
  40. cout << "Customer Address: ";
  41. cin >> uInput;
  42. ss << uInput;
  43. break;
  44. default:
  45. // exit(1);
  46. break;
  47. }
  48. }
  49. int main() {
  50. customerClass rect;
  51. int n;
  52.  
  53. do {
  54. rect.getActions(1);
  55. cin >> n;
  56.  
  57. if(cin.fail()){
  58. cout << "bad input!" << endl;
  59. cin.clear();
  60. cin.ignore(INT_MAX, '\n');
  61. continue;
  62. }
  63.  
  64. rect.getActions(n);
  65. }while(n!=0);
  66. return 0;
  67. }

You're probably wondering why I bothered with the sstream?

Consider the potential problem with bad input for the input buffer, cin.

You can pass information from cin to another buffer. That way if
you encounter extraction problems, its no longer cin's responsibility
to handle it - its that of the buffer that you are performing extraction on!

I personally, never like messing with cin directly. Using temporary buffers
can simplify problems with the standard input buffer at the cost of space
in your program.
Last edited by Alex Edwards; May 8th, 2009 at 4:21 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: cin in do while loop

 
1
  #3
May 8th, 2009
>This may help
Yeah, Alex, it will surely help him, but you don't explain him what you've changed to make his code work ...

To the OP:
It's because of the getline function, so you'll have to flush the input stream each time to make your code work as expected, there's a thread about flushing the input stream, you can find it here
Last edited by tux4life; May 8th, 2009 at 11:51 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: cin in do while loop

 
0
  #4
May 8th, 2009
Originally Posted by tux4life View Post
>This may help
Yeah, Alex, it will surely help him, but you don't explain him what you've changed to make his code work ...

To the OP:
It's because of the getline function, so you'll have to flush the input stream each time to make your code work as expected, there's a thread about flushing the input stream, you can find it here
Yeah, after realizing how tired I was I also realized that I never answered the original question.

the getline function is accepting characters for your string but it doesn't seem like you're specifying a delimeter for the getline (by default it should be the newline char) nor are you flushing the input buffer after you push characters in it, so whan cin>>n is processed, characters are still in the buffer and then cin gets placed in a fail state.

Actually, Narue's post about this is far more intuitive than I could possibly explain. It wouldn't hurt to take the time to read it.

In this portion of code, after getting input from the user (such as the number for the option) I immediately ignore any characters that were put in the buffer then make a request to get characters in the input buffer until the newline delimiter is met then the characters are stored in the string.

  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. class customerClass {
  8. string masterArray[2][255];
  9.  
  10. public:
  11. void getActions(int);
  12. void saveInArray(int, string);
  13. string readArray(int,int);
  14. };
  15. string customerClass::readArray(int x, int y) {
  16. cout << masterArray[x][y];
  17. }
  18. void customerClass::saveInArray(int a, string sx) {
  19. switch(a) {
  20. case 1:
  21. masterArray[a][0] = sx;
  22. break;
  23. }
  24. }
  25. void customerClass::getActions(int actions) {
  26.  
  27. string uInput;
  28. switch(actions) {
  29. case 1:
  30. cout << "Options: (2) Add Name, (3) Add Address, (0) Exit: ";
  31. break;
  32. case 2:
  33. cout << "Customer Name ";
  34. cin.ignore(INT_MAX, '\n');
  35. getline(cin, uInput, '\n');
  36. saveInArray(1,uInput);
  37. break;
  38. case 3:
  39. cout << "Customer Address: ";
  40. cin.ignore(INT_MAX, '\n');
  41. getline(cin, uInput, '\n');
  42. break;
  43. default:
  44. // exit(1);
  45. break;
  46. }
  47. }
  48. int main() {
  49. customerClass rect;
  50. int n;
  51.  
  52. do {
  53. rect.getActions(1);
  54. cin >> n;
  55.  
  56. if(cin.fail()){
  57. cout << "bad input!" << endl;
  58. cin.clear();
  59. cin.ignore(INT_MAX, '\n');
  60. continue;
  61. }
  62.  
  63. rect.getActions(n);
  64. }while(n!=0);
  65. return 0;
  66. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 219
Reputation: NathanOliver is an unknown quantity at this point 
Solved Threads: 37
NathanOliver's Avatar
NathanOliver NathanOliver is offline Offline
Posting Whiz in Training

Re: cin in do while loop

 
0
  #5
May 8th, 2009
just for your information you do not need to implicitly define the delimiter as a newline in getline() it is already set there and if you want to change it the you must specify what you want. at least that is according to the msdn for visual c++ 6.0 professional
if you write using namespace std; you do not need to write std::something in your program.
If your thread is solved please mark it as solved
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: cin in do while loop

 
0
  #6
May 8th, 2009
Originally Posted by NathanOliver View Post
just for your information you do not need to implicitly define the delimiter as a newline in getline() it is already set there and if you want to change it the you must specify what you want. at least that is according to the msdn for visual c++ 6.0 professional
Yup, that's true: http://www.cplusplus.com/reference/iostream/istream/getline/
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 3
Reputation: l3vi is an unknown quantity at this point 
Solved Threads: 0
l3vi l3vi is offline Offline
Newbie Poster

Re: cin in do while loop

 
0
  #7
May 9th, 2009
Thanks for the info and link. Alex; if I'm fallowing you right, using stringstream and passing uInput into ss it's moving the data out of the cin buffer into a buffer in ss stringstream?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC