scanf with two inputs acting funny

This is a discussion on scanf with two inputs acting funny within the C Programming forums, part of the General Programming Boards category; I'm doing this exercise where I'm supposed to emulate logowriter. Basically it's a turtle that starts in the center and ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    scanf with two inputs acting funny

    I'm doing this exercise where I'm supposed to emulate logowriter. Basically it's a turtle that starts in the center and moves in different directions based on the commands you input. My problem is all the commands but one take only one value as an input. The other command takes two command takes two variables, one variable teling the program the turtle needs to be moved forward and the second variable telling how far it should move. Well this seems to be throwing of my whole program. For one entering command 6 which is supposed to print all the movements made doesn't work(although it works when run on its own). And when I try to exit by pressing 9 I have to enter it like 3 times.

    My book didn't do a very good job explaining the ins and outs of scanf, I was hoping maybe someone over here can help me out.

    Code:
    #include <stdio.h>
    
    void moveFor(int, int, int[][ 50 ], int);
    void printArray( int[][ 50 ] );
    
    int positionV = 25, positionH = 25;
    
    int main()
    {
       int input = 0, spaceInput = 0, direction = 0, pen = 0;
       int floor[ 50 ][ 50 ] = { { 0 }, { 0 } };
    
       printArray( floor );
    
    
    
       while (input != 9) {
          scanf("%d %d", &input, &spaceInput);
    
          if (input == 9)
             continue;
    /*
          if (input == 3)
             direction -= 90;
          if (input == 4)
             direction += 90;
          direction = direction % 360;
    */
          if (input == 1)
             pen = 0;
          if (input == 2)
             pen = 1;
    
          if (input == 5 && spaceInput != 0) {
             moveFor(direction, spaceInput, floor, pen);
             spaceInput = 0;
    
          if (input == 6)
             printArray( floor );
          }
       }
    
    return 0;
    }
    
    
    
    
    void moveFor(int d, int spaces, int table[][ 50 ], int pStatus)
    {
       int counter;
    
       for (counter = 1; counter <= spaces; ++counter) {
          if (d == 0 && positionH != 49) {
             if (pStatus == 1)
                table[++positionH][positionV] = 1;
             if (pStatus == 0)
                ++positionH;
          }
    
          if (d == 90 && positionV != 49) {
             if (pStatus == 1)
                table[positionH][--positionV] = 1;
             if (pStatus == 0)
                --positionV;
          }
    
    
          if (d == 180 && positionH != 0) {
             if (pStatus == 1)
                table[--positionH][positionV] = 1;
             if (pStatus == 0)
                --positionH;
          }
    
    
          if (d == 270 && positionV != 0) {
             if (pStatus == 1)
                table[positionH][++positionV] = 1;
             if (pStatus == 0)
                ++positionV;
          }
       }
    
       return;
    }
    
    
    
    
    void printArray( int array[][ 50 ] )
    {
    int i, j;
    
       for (i = 0; i <= 49; ++i) {
          for (j = 0; j <= 49; ++j) {
             if (array[ i ][ j ] == 0)
                printf(" ");
             else if (array[ i ][ j ] == 1)
                printf("*");
          }
          printf("\n");
       }
    
    return;
    }

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    For getting multiple inputs from a line I prefer using fgets() to get the input as a string. Then scan the input using sscanf().

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    There's no way for a simple scanf to recognise the difference between a 1 parameter and 2 parameter user input.
    If you only type in 1 number, it will simply stick around until you type another number or some junk.

    Code:
    while ( fgets( buff, sizeof buff, stdin ) != NULL ) {
      int p1, p2;
      int res = sscanf( buff, "&#37;d %d", &p1, &p2 );
      // at this point, res will be EOF, 0, 1, 2 depending on how good the input was
      // use it to decide how much of p1 and/or p2 is valid
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Control loop with scanf
    By sandeep156 in forum C Programming
    Replies: 5
    Last Post: 05-02-2009, 02:06 PM
  2. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 03:39 PM
  3. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  4. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM
  5. Replies: 9
    Last Post: 11-27-2001, 08:25 AM

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