how do i stop this error.....,
Members - Reputation: 122
Posted 25 January 2002 - 06:36 AM
Members - Reputation: 220
Posted 25 January 2002 - 08:07 AM
You can specify the maximum number of characters to allow with getline. You could set the max number to 1 character then you can convert that to an int from there. Maybe check that it isn''t an alpha character, and as long as it''s a number 1-9 you can convert to int using atoi(ascii to int). There may be other ways, but that''s the way I use cin when I need to parse the input for something specific. I don''t know if there''s a cin method that you can specify formatting to or not.
Members - Reputation: 2076
Posted 27 January 2002 - 06:01 AM
Anyway, here''s how to use getline. It''s in the new C++ Standard Library, so you''ll need to use the new-style standard headers as well as the std namespace:
using namespace std;
getline(cin, inp); // default, gets everything up to newline
getline(cin, inp, 4); // gets up to 4 characters
You might also want to look into the string class, as it makes text manipulation much easier.
[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!