May 20, 2011 at 9:10pm UTC
For some reason I keep on having trouble getting input from the stream using the get member of cin in particular. Here is the section the problem is occuring in:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
char selection[SHRTSTR];
for (bool contCom = true ; contCom;)
{
cout << "DEBUG INSIDE" ;
cin.get(selection, SHRTSTR);
if (!strcmp("/x" , selection)) contCom = false ;
else
{
game *displayedGame = games.req(selection);
if (displayedGame)
{
cout << endl;
cout << "Name: " << displayedGame->getName() << endl;
cout << "Genre: " << displayedGame->getGenre() << endl;
cout << "Description:\n" << displayedGame->getDescr() << endl << endl;
contCom = false ;
games.rel(selection);
}
}
}
What seems to happen is cin.get() is skipped and the console just outputs DEBUG INSIDE continously (yes I put that there to try and figure out what's going wrong). Why is this happening? I'd like to know if there is a more appropriate way of reading a certain length of string from input.
Last edited on May 20, 2011 at 9:11pm UTC
May 20, 2011 at 10:57pm UTC
I don't know much about error flags and cin, perhaps you could tell me the code to surround with to output the error state (nothing fancy).
May 21, 2011 at 2:55pm UTC
Right now I'm using >> but I'm not keen on only using the first word.
Does anyone know what stops cin.get() from runnning properly here?
May 21, 2011 at 3:54pm UTC
cin.get() gets unformatted data, if you want to use it with a string you need to append a null-character to the end of it i.e. '0'.
cin.get(3, s);
s now contains: {'a', 'b', 'c'}
it should contain: {'a', 'b', 'c', '\0'}
http://cplusplus.com/reference/iostream/istream/get
Last edited on May 21, 2011 at 3:54pm UTC
May 21, 2011 at 6:28pm UTC
No Mathhead it gives you the 0. In fact, it actually says so in that article.
And I was wondering why the command was being skipped, I didn't say anything about non-functioning strings.
Last edited on May 21, 2011 at 6:29pm UTC
May 21, 2011 at 10:21pm UTC
Mathhead, you know you can write 0 instead of '\0'.
May 21, 2011 at 11:17pm UTC
My mistake. I've never had a use for it when extracting char *
before.
May 21, 2011 at 11:50pm UTC
NP, all I really want is a function that extracts strings up to a maximum length (I don't want to go over the end of the array size). I tried using get because it specifies a length, but it doesn't work.
May 22, 2011 at 4:57am UTC
Maybe check that cin is not corrupted:
if ( cin.good() ) ...
http://cplusplus.com/reference/iostream/ios/good
Also you can just use strings and vectors if size limit is all your worried about?
I'm still not sure what your trying to do, or what the problem is exactly...
Last edited on May 22, 2011 at 4:58am UTC
May 22, 2011 at 11:32am UTC
If I enter too much information with >> then when it tries to enter it into my string it won't check the size or anything, it just tries to continue writing and crashes the program.
May 22, 2011 at 3:10pm UTC
Can anyone recommend a string input method?
May 22, 2011 at 3:34pm UTC
Then I think cin.get()
would be what you want here...
Last edited on May 22, 2011 at 3:35pm UTC
May 22, 2011 at 3:37pm UTC
Back to square one, well if cin.get() is the best method then does anyone have any ideas why it randomly skips input?
May 22, 2011 at 4:09pm UTC
Please post an example run including your input, output, and expected output. (Or something like this.)
Last edited on May 22, 2011 at 4:09pm UTC
May 22, 2011 at 5:47pm UTC
Yeah sure;
DEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDEDEBUG INSIDE
As you can see, the cin.get is being skipped.
May 24, 2011 at 8:45pm UTC
Nobody ever had cin being skipped before, then?
May 24, 2011 at 9:03pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
char selection[SHRTSTR];
for (bool contCom = true ; contCom;)
{
cout << "DEBUG INSIDE" ;
cin.get(selection, SHRTSTR);
if (!strcmp("/x" , selection)) contCom = false ;
else
{
game *displayedGame = games.req(selection);
if (displayedGame)
{
cout << endl;
cout << "Name: " << displayedGame->getName() << endl;
cout << "Genre: " << displayedGame->getGenre() << endl;
cout << "Description:\n" << displayedGame->getDescr() << endl << endl;
contCom = false ;
games.rel(selection);
}
}
cin.ignore() ; //ignore the carriage return <<=============
}
Last edited on May 24, 2011 at 9:03pm UTC