Chat on IRC |
scanf question
Posted by buzgub [send private reply] at August 02, 2001, 02:55:41 AM
Hi folks,
I've been programming in VB for about 3 years, and am now trying to move to c. However, in my program, scanf doesn't wait for input to come in. I've pulled the scanf statement into a seperate program and it works fine there. Has anyone seen this before?
Posted by gian [send private reply] at August 02, 2001, 03:34:38 AM
It usually waits... I can't say I've seen an instance of it not working.
Are you using something like the following? char SomeString[80]; scanf("%s", &SomeString);
Posted by buzgub [send private reply] at August 02, 2001, 04:51:13 AM
It's actually an int....
int i, j; j = scanf("%d", &i); and, as I said, it works when it's just like that. It's in the overall program that it fails. The test program I use is: #include <stdio.h> void main( void ) { int j, i; printf ("Please type an int: "); j = scanf("%d", &i); printf ("you typed a %d\n", i); } and that compiles and runs perfectly. The scanf is in a loop which waits for valid input before continuing, and I get _fragments_ of the "that was not valid input" message. any other ideas?
Posted by Psion [send private reply] at August 02, 2001, 06:03:07 AM
Your description of your actual program isn't very helpful. Could you post the part that breaks? Also, are you aware that scanf returns the number of input items assigned, so that's what you put in j?
Posted by buzgub [send private reply] at August 02, 2001, 08:01:24 AM
I'm making a tic tac toe (noughts and crosses) game, as it's something that I think I should be able to use some more un-VBish things with... the printf's and scanf's are great for teaching pointers. I did know about scanf's return value, because I had consulted several documentation bits about scanf. It seems that some of that actually sunk in. anyway, I rebuilt the file by copying each of the functions from the old file into a new one, and checking each function. I seem to have fixed it. thanks to gian and psion for actually saying _something_. In addition, the bug of no waiting for input seems to occur only when I use the pre-copying file and feed it into the gnu C++ compiler. cygwin calls it c++, I think it's usually g++. anyway, when I use that on the old file, the bug occurs. When I use that on the new file, the bug does not occur. the bug _never_ occurs with straight gcc. If anyone wants to take a look at the buggy (old) one, I can make that available. Reading that might be an interesting experience, especially because the formatting of that thing is atrocius. I think the new one's better, though.
Now, I've got a whole new bug!! (programming's fun :) ) This is my routine to draw the current playing field: void DrawScreen(void){ int x; //keep track of the co-ords of the square being drawn int y; //enter draw loop for (x=0;x<(dmn*2)+1;x++){ //check what the line needs (if it is odd numbered) if ( (x+1)%2 == (1%2) ){ //if the line just needs a seperator, draw that for (y=0; y<dmn; y++){ printf ("--"); } printf ("-\n"); //draw the end of the row }else{ //draw the actual content for (y=0; y<dmn; y++){ printf ("|%s", &PlayField[x][y]); } printf ("|\n"); //draw the end of the row } } } The output of this is: ------- | | | | ------- | ||| ------- |||| ------- (put it in courier to make it align :( ) Where it should be: ------- | | | | ------- | | | | ------- | | | | ------- So, is there a glaringly obvious bug that I've missed? I think I had the routine working properly once, but I don't know what's changed since then. I should make daily archives :( I suppose there's probably a glaringly obvious error in there, but I sure as hell can't find it. Just as a note, I know the PlayField[x][y] stuff is all properly initialized, I've got a routine that does that and it uses a printf with everything the same to tell me whats in the variable it just set: printf ("The playfield had the following put into it: _%s_\n", &PlayField[i][j]); Does anyone have any suggestions? oh, and sorry about the length of the post. I _can_ easily post that dead file if anyone wants to try and crack _that_ tough nut. (that image is soooooooo gonna get me in trouble, I'm sure of it) This doesn't feel finished. Oh well, never mind. CYA!
Posted by Psion [send private reply] at August 02, 2001, 10:20:19 AM
What is the definition of PlayField?
Posted by Sarang [send private reply] at August 02, 2001, 02:31:43 PM
I think you should try fflush(stdin) to clear the previous input before getting any new one..
Posted by infryq [send private reply] at August 02, 2001, 02:56:54 PM
hey -- i think your problem lies in your math. when you set the for loop to loop *every* line of the board, you end up with six lines. but you've only got three values for x in PlayField -- for a 3x3 board that's 0,1, and 2 -- so you only get the values for x=1 when you try and print the board because the other rows don't exist in PlayField. i ran the code and got this:
------- | | | | ------- |||| ------- |||| ------- ...I'm not sure where your extra space came from in your example, maybe we initialized PlayField differently. but you've got to find some way of lining up board rows 1, 3, and 5 with PlayField rows 0, 1, and 2 to get it to work.
Posted by Psion [send private reply] at August 02, 2001, 03:01:38 PM
I don't see how you ran that without information on the type of PlayField. I am suspicious with using %s to print a part of it. Why would strings be used instead of just chars in this array?
Posted by infryq [send private reply] at August 02, 2001, 03:23:53 PM
i used
char PlayField[3][3]; and initialized it to all spaces... i don't know why he used strings either. that printf statement was a bit messed, don't know where the address-of operator came from: printf ("|%c", PlayField[x][y]); seemed to work OK. i do prefer lumping arrays like that together; having them multidimensional makes it annoying to deal with once you start trying to pass them to functions. But whatever's easier for him.
Posted by gian [send private reply] at August 02, 2001, 11:10:33 PM
Also... some compilers require you to initialize your variables... it can cause printf and scanf to explode sometimes.
Posted by buzgub [send private reply] at August 03, 2001, 12:18:13 AM
The address-of operator came from MS-VC++ help. The example I tore out of that to make sure that it wasn't a problem with the compiler had address-of operators preceding all of the arguments for scanf and I suppose I must have got a bit over-enthusiastic (oops). I'm declaring PlayField with:
char PlayField[2][2]; and using a loop to set all those to a space. It seems that the problem derived from the printf's and it all seems to be working now you've pointed out the problem. Thanks for your help. infryq: I have found a way of lining those up. You noted that the loop ran twice as many times as was needed; well, if it's on an even-numbered line it throws in a row seperator. Otherwise, it puts in the actual content. I hope that makes sense to you.... CHollman82: I know you want to reply to this about my over-entusiasm with the address-ofs. It was just a stupid mistake, kind of like overuse of goto or using non-ansi c++ stuff in a c++ tutorial. Sarang: Thanks for your suggestion on buffer flushing. I though there was something like that, and would have tried it if I could find the blasted function. Thanks again to all that helped.
Posted by CHollman82 [send private reply] at October 01, 2001, 10:09:58 PM
Posted by gian [send private reply] at August 03, 2001, 02:17:24 AM
buzgub: hehehehe
Posted by infryq [send private reply] at August 03, 2001, 02:31:07 AM
well... you've got exactly as many loops as you need :) since you end up with six rows. but if it works, you found the problem, so i'll shut up.
Posted by toasted [send private reply] at August 03, 2001, 06:04:19 AM
well...looking at your scanf problem, it may be just portability problems.
if you do this scanf("%d", &i); j = i; that will work more appropriately.. But I am only a beginner, so please don't flame me if I am wrong. laterz
Posted by Psion [send private reply] at August 03, 2001, 10:03:49 AM
buzzy, if your array is 3x3, it should be char[3][3], not char[2][2]. I think infryq's point is still right. You will go beyond your array bounds unless you use x/2 instead of x when you actually get the contents of a square. The mere fact that you only get as many values as you actually have doesn't prevent you from using bad indices. Assuming you removed those &'s and switched to %c instead of %s, then you should probably have an actual consistently correct program with these changes. :-)
Posted by buzgub [send private reply] at August 03, 2001, 09:17:08 PM
The array is 2*2 because arrays tend be zero-based
However, now that I look at the code I can see that it shouldn't work. I have no idea why it does, but I'll fix the "problem" which should exist but appears not to anyway.
Posted by CHollman82 [send private reply] at October 01, 2001, 10:10:21 PM
Posted by buzgub [send private reply] at August 04, 2001, 02:40:28 AM
OK. That makes sense... just.
Register as a new user | ||||||||||||||||||||||