Thread: Multiple scanf not reading input

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    6

    Multiple scanf not reading input

    Hello,

    my program is supposed to take 2 numbers separated by a comma and then run them through a loop to determine the GCD, but every time I run the program the scanf isn't reading the inputs and just keeps displaying white space like I haven't entered anything. It won't execute the code after scanf.

    Here's the code snippet.
    Code:
    int user1;
    int user2;
    int GCDcheck;
    printf ("Enter the two (non-zero) integers: ");
    scanf ("%d,%d", &user1, &user2);
    GCDcheck = user1 * user2;
    it never executes the GCDcheck = user1 * user2;

    I've looked around and can't find something similar to this problem, any help or any insight would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Lillith!

    I tried your code in this little snippet of a program, and it prints them out properly. Are you entering the comma and a space between the two integers?

    Code:
    #include <stdio.h>
    
    int main(void) {
    
       int user1;
       int user2;
       int GCDcheck;
       printf ("Enter the two (non-zero) integers: ");
       scanf ("%d,%d", &user1, &user2);
       printf("user1: %d   user2: %d \n",user1,user2);
       GCDcheck = user1 * user2;
    
    
       return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    6
    Yes, I am. I thought that I was coding it correctly. What could be causing it to not work? This is so frustrating!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This snippet also works for me.
    Code:
    #include <stdio.h>
    
    int main(void) {
    
       char comma;
       int user1;
       int user2;
       int GCDcheck;
       printf ("Enter the two (non-zero) integers: ");
       scanf ("%d%c %d", &user1,&comma, &user2);
       printf("user1: %d   user2: %d \n",user1,user2);
       GCDcheck = user1 * user2;
    
       return 0;
    }
    Usually, it's a good idea to leave comma's out of the scanf() format string.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    It would help if you show the exact input (by copying in whole cmd line output into the post together with code)

    For example using code from post #2 I got the following
    Code:
    Enter the two (non-zero) integers: 4,5
    user1: 4   user2: 5
    Press any key to continue . . .
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading multiple lines Scanf?
    By dexstar in forum C Programming
    Replies: 13
    Last Post: 08-01-2013, 08:57 PM
  2. input while/scanf
    By Tigertan in forum C Programming
    Replies: 1
    Last Post: 08-25-2012, 05:50 AM
  3. reading an input line using scanf() with %c
    By linucksrox in forum C Programming
    Replies: 6
    Last Post: 04-04-2004, 03:10 PM
  4. Reading input with scanf
    By dat in forum C Programming
    Replies: 3
    Last Post: 05-29-2003, 03:54 PM
  5. Reading multiple integers with scanf
    By Argentina in forum C Programming
    Replies: 0
    Last Post: 02-18-2002, 10:34 AM

Tags for this Thread