| | |
cin in do while loop
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2009
Posts: 3
Reputation:
Solved Threads: 0
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.
2nd code test that works fine:
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)
#include <iostream> #include <string> #include <cstdlib> using namespace std; class customerClass { string masterArray[2][255]; public: void getActions(int); void saveInArray(int, string); string readArray(int,int); }; string customerClass::readArray(int x, int y) { cout << masterArray[x][y]; } void customerClass::saveInArray(int a, string sx) { switch(a) { case 1: masterArray[a][0] = sx; break; } } void customerClass::getActions(int actions) { string uInput; switch(actions) { case 1: cout << "Options: (2) Add Name, (3) Add Address, (0) Exit: "; break; case 2: cout << "Customer Name "; getline(cin, uInput); saveInArray(1,uInput); break; case 3: cout << "Customer Address: "; getline(cin,uInput); break; default: // exit(1); break; } } int main() { customerClass rect; int n; rect.getActions(1); do { cin >> n; rect.getActions(n); }while(n!=0); return 0; }
2nd code test that works fine:
C++ Syntax (Toggle Plain Text)
// number echoer #include <iostream> using namespace std; int main () { unsigned long n; do { cout << "Enter number (0 to end): "; cin >> n; cout << "You entered: " << n << "\n"; } while (n != 0); return 0; }
This may help--
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.
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <cstdlib> #include <sstream> using namespace std; class customerClass { string masterArray[2][255]; public: void getActions(int); void saveInArray(int, string); string readArray(int,int); }; string customerClass::readArray(int x, int y) { cout << masterArray[x][y]; } void customerClass::saveInArray(int a, string sx) { switch(a) { case 1: masterArray[a][0] = sx; break; } } void customerClass::getActions(int actions) { stringstream ss (stringstream::in | stringstream::out); string uInput; switch(actions) { case 1: cout << "Options: (2) Add Name, (3) Add Address, (0) Exit: "; break; case 2: cout << "Customer Name "; cin >> uInput; ss << uInput; saveInArray(1,ss.str()); break; case 3: cout << "Customer Address: "; cin >> uInput; ss << uInput; break; default: // exit(1); break; } } int main() { customerClass rect; int n; do { rect.getActions(1); cin >> n; if(cin.fail()){ cout << "bad input!" << endl; cin.clear(); cin.ignore(INT_MAX, '\n'); continue; } rect.getActions(n); }while(n!=0); return 0; }
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.
>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
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."
•
•
•
•
>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 thegetlinefunction, 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
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)
#include <iostream> #include <string> #include <cstdlib> using namespace std; class customerClass { string masterArray[2][255]; public: void getActions(int); void saveInArray(int, string); string readArray(int,int); }; string customerClass::readArray(int x, int y) { cout << masterArray[x][y]; } void customerClass::saveInArray(int a, string sx) { switch(a) { case 1: masterArray[a][0] = sx; break; } } void customerClass::getActions(int actions) { string uInput; switch(actions) { case 1: cout << "Options: (2) Add Name, (3) Add Address, (0) Exit: "; break; case 2: cout << "Customer Name "; cin.ignore(INT_MAX, '\n'); getline(cin, uInput, '\n'); saveInArray(1,uInput); break; case 3: cout << "Customer Address: "; cin.ignore(INT_MAX, '\n'); getline(cin, uInput, '\n'); break; default: // exit(1); break; } } int main() { customerClass rect; int n; do { rect.getActions(1); cin >> n; if(cin.fail()){ cout << "bad input!" << endl; cin.clear(); cin.ignore(INT_MAX, '\n'); continue; } rect.getActions(n); }while(n!=0); return 0; }
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
If your thread is solved please mark it as solved
using namespace std; you do not need to write std::something in your program.If your thread is solved please mark it as solved
•
•
•
•
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
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
![]() |
Similar Threads
- Loop counting odd and even numbers (C++)
- Help Me Solve My Infinite Loop (C++)
- Simple while loop in c++ (C++)
- cin.get issue (C++)
- Program runs in an infinite loop (C++)
- while loop in c++ help needed (C++)
Other Threads in the C++ Forum
- Previous Thread: Right Angled Triangle C++
- Next Thread: char array and convert them to ints
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






