Help, scanf isn't waiting for user input

This is a discussion on Help, scanf isn't waiting for user input within the C Programming forums, part of the General Programming Boards category; In my main function i have a scanf that should wait for user input, but instead the program just ends, ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    34

    Help, scanf isn't waiting for user input

    In my main function i have a scanf that should wait for user input, but instead the program just ends, this is the last bit of my main function.

    Code:
    printf("Would you like to focus on a specific player(y or n): ");
    		if(scanf("%s",answer)==1) {
    			if(strcmp(answer,"y")==0|strcmp(answer,"yes")==0) {
    				printf("Player Slot Number: ");
    				if((scanf("%d", &slot)==1)&&(slot<=10)&&(slot>=1)) {
    					printf("Check clicks until(mm:ss): ");
    					if(scanf("%2d:%2d",&minutes,&seconds)==2) {
            				c = check_clicks(argv[1],heroes,players,hero_names,minutes,
    								seconds,slot);
    					}
    					else if(scanf("%s",all)==1) {
    						if(strcmp(all,"all")==0) {
    							minutes = 99;
    							seconds = 99;
    							c = check_clicks(argv[1],heroes,players,hero_names,minutes,
    								seconds,slot);
    						}
    					}
    					else {
    						minutes = 2;
    						seconds = 15;
    						c = check_clicks(argv[1],heroes,players,hero_names,minutes,
    								seconds,slot);
    					}
    					printf("%d enemy clicks were detected before %02d:%02d replay time\n",c,minutes,seconds);
    				}
    			}
    		}
        }
        return 0
    }
    I have a scanf earlier on in the main function and its
    Code:
    if(scanf("%2d:%2d",&minutes,&seconds)==2) {
            	c = check_clicks(argv[1],heroes,players,hero_names,minutes,
    					seconds,0);
    		}
    and that works fine, it waits for user input.

    Any help much appreciated.

  2. #2
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    Hmm, as far as I know, scanf() can behave this way only in occasions when there are remaining data in the input buffer, such as this example:
    int a;
    char b;
    scanf("%d", &a);
    scanf("%c", &b);
    Here newline with which you end your input comes to the second scanf() immediately and it overall looks like there was only one scanf().
    To fix this issue, you can try to manually clear the buffer this way:
    Code:
    while (getchar() != '\n')
      continue;
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A simple way is to just get in the habit of putting a
    getchar();

    after scanf() lines. You don't need it for numbers, but it doesn't hurt. You DO need it before %c for a char.

    Otherwise, the scanf() takes the char, says "Got what I came here for", and takes off, seeming for all the world, to have skipped over the line of code, somehow.

  4. #4
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    >A simple way is to just get in the habit of putting a getchar();
    A single getchar() wouldn't help you much in situations when user types something like '23dsadg'.
    That's why it's always better to make a suggested loop instead.
    Last edited by GL.Sam; 05-07-2010 at 05:07 AM.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    nvm that, it works now, thanks for your help.
    Last edited by Blasz; 05-07-2010 at 05:30 AM.

  6. #6
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    >I tried the loop you suggested sam, but it didn't work.
    Post here the particular part of code you tried. The loop should've been inserted between any two scanf() statements.

    Also using a bitwise operator '|'
    >strcmp(answer,"y")==0|strcmp(answer,"yes")==0
    instead a logical '||' is asking for trouble.
    Last edited by GL.Sam; 05-07-2010 at 05:39 AM.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input using scanf()
    By kizyle502 in forum C Programming
    Replies: 5
    Last Post: 09-10-2009, 12:56 AM
  2. getting input without waiting for it(tetris)
    By Argentum in forum C++ Programming
    Replies: 13
    Last Post: 12-10-2005, 10:37 PM
  3. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 10:28 PM
  4. Keyboard input without waiting (Borland C++)
    By adamdalziel in forum Game Programming
    Replies: 6
    Last Post: 09-24-2001, 04:20 PM
  5. help with input and scanf
    By kpw in forum C Programming
    Replies: 9
    Last Post: 08-28-2001, 08:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21