Problem with stream input

Pages: 12
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
So nobody knows?
Is cin in a error state?
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).
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?
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
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
Mathhead, you know you can write 0 instead of '\0'.
My mistake. I've never had a use for it when extracting char* before.
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.
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
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.
Can anyone recommend a string input method?
Then I think cin.get() would be what you want here...
Last edited on
Back to square one, well if cin.get() is the best method then does anyone have any ideas why it randomly skips input?
Please post an example run including your input, output, and expected output. (Or something like this.)
Last edited on
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.
Nobody ever had cin being skipped before, then?
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
I still think you need to make sure the stream isn't corrupted...
1
2
if( cin.good() )
  ... cin.get( ... );


Mathhead200 wrote:
... including your input, output, and expected output.
Pages: 12