Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Weird Problems with stdio and iostream

This topic is 5476 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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==''/''){
		cout<<"Your Guess is:";
		//cin.getline(guess, 256);
		cout<<"Are you sure? - Y/N :";
			send(sock,guess, strlen(guess)+1, 0);
			cout<<"...guess again..."< 

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

	send(sock,letterToGuess, 2, 0);			//send the guessed letter
	checkCommand(letterToGuess[0], sock);
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?

Share this post

Link to post
Share on other sites
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
----The Phrase is----
****** *** *****

Enter Letter to guess: a
----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
----The Phrase is----
****** **A *A***

Enter Letter to guess:
bash-2.05b$ exit

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post

Link to post
Share on other sites
Before you call checkCommand( ... ) you make a call "cin>>letterToGuess;"

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.

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 ;-)

Share this post

Link to post
Share on other sites
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...

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!