|
From: robert4 on 14 Jan 2008 01:25 I am working through the exercises in a book and have hit a roadblock. In the following code I want the user to input 2 different things. The integer is fine, but the program skips right past the string input. I can't figure out why. Any help or hints would be apreciated. // A first programm #include <iostream> using namespace std; int main () { int user1 = 0; char name[15]; for(;;) { cout << "Enter a non-zero integer or enter zero to quit: "; // User enters the integer cin >> user1; // Program reads the integer if (user1 == 0) break; // Checks to see if the user entered zero to quit cout << endl << "Enter a string less than 15 characters long: "; cin.getline(name, 15, '\n'); cout << endl << "The number you entered was: " << user1 << " ...and the string was: " << name << endl << endl; } return 0; }
From: Jerry Coffin on 14 Jan 2008 01:35 In article <Xns9A25468EEABDhomeofthedallasstars(a)216.168.3.44>, bobor(a)hotmail.com says... [ ... ] > cin >> user1; > // Program reads the integer This reads the digits from the input stream, but anything after the digits (including a newline character, if you hit the enter key) is left in the stream. > if (user1 == 0) break; > // Checks to see if the user entered zero to quit > > cout << endl << "Enter a string less than 15 characters long: "; > cin.getline(name, 15, '\n'); This then tries to read from the input stream up to (and including) a newline character. If you hit enter before, all it finds is a new-line character, so it reads that and stops. You can test this by running the program, and at the first prompt entering something like "1234 Bill" (without the quotes) then hit the enter key. Assuming your compiler (and library) work right, your output should look like this: The number you entered was: 1234 ...and the string was: Bill Bottom line: making interactive input work well is a pain. It's much better to avoid it when you can... -- Later, Jerry. The universe is a figment of its own imagination.
From: Ulrich Eckhardt on 14 Jan 2008 01:44 robert4 wrote: > I am working through the exercises in a book and have hit a roadblock. In > the following code I want the user to input 2 different things. The > integer is fine, but the program skips right past the string input. I > can't figure out why. > cin >> user1; [...] > cin.getline(name, 15, '\n'); The first input operation removes characters from the input buffer until it reaches one that can't be parsed as part of the number. The second one extracts characters until it hits the separator '\n'. If you enter "42\ntext\n", the first '\n' also terminates the second input operation. Suggestions: 1. Use line-based input exclusively. Only use getline to read input from the user and then parse it into a number if necessary. For parsing, I would suggest Boost.LexicalCast (an external library) or std::stringstream. 2. Use std::string and the free function getline() instead of istream's memberfunction. These are just so much better than using char pointers or arrays for handling strings, because they can be resized as necessary and relieve you from handling allocated storage explicitly. In fact you can forget about the fact that char arrays and pointers can be used for handling strings. 3. Where you are supposed to enter a number, enter something that can't be parsed as a number. Observe the behaviour and fix it. Note that this almost automatically fixes itself if you use getline() exclusively, but you should nonetheless understand what's going on. You fault here is basically that you didn't check and handle errors. Uli
From: robert4 on 14 Jan 2008 01:51 Jerry Coffin <jcoffin(a)taeus.com> wrote in news:MPG.21f4b089a5d434ff989b24(a)news.sunsite.dk: > In article <Xns9A25468EEABDhomeofthedallasstars(a)216.168.3.44>, > bobor(a)hotmail.com says... > > [ ... ] > >> cin >> user1; >> >> // Program reads the integer > > This reads the digits from the input stream, but anything after the > digits (including a newline character, if you hit the enter key) is > left in the stream. > >> if (user1 == 0) break; >> >> // Checks to see if the user entered zero to quit >> >> cout << endl << "Enter a string less than 15 characters long: "; >> cin.getline(name, 15, '\n'); > > This then tries to read from the input stream up to (and including) a > newline character. If you hit enter before, all it finds is a new-line > character, so it reads that and stops. > > You can test this by running the program, and at the first prompt > entering something like "1234 Bill" (without the quotes) then hit the > enter key. Assuming your compiler (and library) work right, your > output should look like this: > > The number you entered was: 1234 ...and the string was: Bill > > Bottom line: making interactive input work well is a pain. It's much > better to avoid it when you can... hehe, yep, it did what you said. Is there a command to flush the stream before the next input? Thanks
From: Daniel T. on 14 Jan 2008 06:12
robert4 <bobor(a)hotmail.com> wrote: > I am working through the exercises in a book and have hit a roadblock. In > the following code I want the user to input 2 different things. The > integer is fine, but the program skips right past the string input. I > can't figure out why. Any help or hints would be apreciated. > > // A first programm > > #include <iostream> > using namespace std; > > int main () > { > int user1 = 0; > char name[15]; > > for(;;) > { > > cout << "Enter a non-zero integer or enter zero to quit: "; > // User enters the integer > cin >> user1; > // Program reads the integer > if (user1 == 0) break; > // Checks to see if the user entered zero to quit > > cout << endl << "Enter a string less than 15 characters long: "; > cin.getline(name, 15, '\n'); > > > > cout << endl << "The number you entered was: " << user1 > << " ...and the string was: " << name << endl << endl; > > > } > > return 0; > } Just a quick comment. If you are near the beginning of the book and it has you using a char array instead of a string object, throw the book away and get a new one. |