Weird Problems with stdio and iostream

Started by
4 comments, last by shatter 20 years, 5 months ago
Ok, I''ll show you the code first and maybe you can help out...

int checkCommand(char commandSeq, int sock){
	char guess[256];
	char sure;
	bool guessAgain = true;

	if (commandSeq==''/''){
	    while(guessAgain){ 	
		cout<<"Your Guess is:";
		//cin.get(guess,256);
		//cin.getline(guess, 256);
		scanf(guess);
		cout<<"Are you sure? - Y/N :";
		cin>>sure;
		
		if(sure==''Y''){
			send(sock,guess, strlen(guess)+1, 0);
			guessAgain=false;
		}
		else{
			cout<<"...guess again..."< 

Then, I call it in a piece of code that looks like this...


cout<<"------------------"<<try1<<"------------------"<>letterToGuess;
	send(sock,letterToGuess, 2, 0);			//send the guessed letter
	checkCommand(letterToGuess[0], sock);
	sleep(2);
  

Now the funny thing is that the scanf/cin.getline/gets/etc. never waits for my input...the proggy keeps plugging along...I am very confused as I have even copy and pasted the code out of "checkCommand" into another file.  It works as expected there and waits for my input before moving along...any idea''s at all?
   
ICQ 11133295AIM shatterstar98MSN shatter98@hotmail.com
Advertisement
thought I might show you a run as well to further illustrate my problem...anyway, here it is.
---Type ''/'' to guess the phrase-------Your hint is----Historical Event	------------------1----------------------The Phrase is----****** *** *****Enter Letter to guess: a------------------2----------------------The Phrase is----****** **A *A***Enter Letter to guess: /Your Guess is:Are you sure? - Y/N :N...guess again...Your Guess is:Are you sure? - Y/N :Y------------------3----------------------The Phrase is----****** **A *A***Enter Letter to guess: bash-2.05b$ exitexit 
ICQ 11133295AIM shatterstar98MSN shatter98@hotmail.com
I just glanced at the code but had a couple of suggestions.

When you use cin, you have to flush the input buffer or it won''t pause there. Check msdn.microsoft.com for more info - they have a sample of how to clear it.

when you use scanf, you need to give it the data type.


scanf ( "%s", guess );


that should work


hope that helped
Before you call checkCommand( ... ) you make a call "cin>>letterToGuess;"

Problem:
You need to be aware that you are perfroming FORMATTED input here - leading whitespace up to something that can be interpreted as (I assume) a char is ignored and everything after that also.

What has happened is that you are hitting ''Return'' after typing the letter and the newline is NOT REMOVED from the underlying buffer, and just lies there until the next time a call to cin comes along.

Solution:
Call istream& istream::ignore( streamsize n = 1, int delim = EOF ), which will discard one character from the underlying buffer (with default arguments).

I notice you are using bash - as a side note using gcc-3.1 I think you might notice some slightly odd behaviour a call to ignore (not seen on 2.95, 3.2+, msvc, as other compilers I have used). I can''t remember exactly what it is though ;-)
That solved my problem...thanks guys...when I get this networked wheel of fortune done I''ll be sure to post it for all to have fun with...
ICQ 11133295AIM shatterstar98MSN shatter98@hotmail.com
I think there is also a ::synchwithstdio() method for iostream. It''s been a while since I used both, so I don''t remember the exact use of the function (or the name for that matter), but sometimes you''ll have problems mixing the two if you aren''t careful. You may want to flush() the streams before you call the scanf and stuff.

This topic is closed to new replies.

Advertisement