943,724 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1094
  • C++ RSS
May 8th, 2009
1

cin in do while loop

Expand Post »
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.

c++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 14
Solved Threads: 0
Newbie Poster
l3vi is offline Offline
4 posts
since May 2009
May 8th, 2009
0

Re: cin in do while loop

This may help--

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
May 8th, 2009
1

Re: cin in do while loop

>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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 8th, 2009
0

Re: cin in do while loop

Click to Expand / Collapse  Quote originally posted by tux4life ...
>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.

c++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
May 8th, 2009
0

Re: cin in do while loop

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
Reputation Points: 215
Solved Threads: 186
Veteran Poster
NathanOliver is offline Offline
1,066 posts
since Apr 2009
May 8th, 2009
0

Re: cin in do while loop

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/
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 9th, 2009
0

Re: cin in do while loop

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?
Reputation Points: 14
Solved Threads: 0
Newbie Poster
l3vi is offline Offline
4 posts
since May 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Right Angled Triangle C++
Next Thread in C++ Forum Timeline: char array and convert them to ints





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC