Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Nov 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    arrays and scanf

    I am working making a 9x9 sudoku solver and I need to input the numbers for each line, 0's being blanked.

    In the sample program, the user inputs for example:

    "
    Line 1: 100200035
    Line 2: 031045067

    etc.

    Now what I am wondering is when the user inputs the numbers, why doesn't it read "100200035" as one integer but as 9 different integers. And what is the proper code for this to happen?

    I don't see how this happens in arrays, I've seen similar for strings and char inputs.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    How can we know without seeing your code?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New to the CF scene
    Join Date
    Nov 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well what I am using at the moment is

    PHP Code:
    scanf("%d"puzzle[row][col]); 
    but as you can see, when I input something like "120000430" it takes in everything instead of storing 1 to the first element, 2 to the second, 0 to the third, etc.

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Code:
    scanf("%d", &puzzle[row][col]);
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    New to the CF scene
    Join Date
    Nov 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That doesn't work though. I want it to be able so that when I type a nine digit number, for example 123456789, I want the 1 to go into the first element puzzle[x][1], 2 into the second element puzzle[x][2], 3 into the third element puzzle[x][4], etc. But what my program is doing right is putting "123456789" all into the first element puzzle[x][1].

  • #6
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by abduraooft View Post
    Code:
    scanf("%d", &puzzle[row][col]);
    That'd probably work with chars but I don't think it would work with integers.
    OracleGuy

  • #7
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Quote Originally Posted by rure View Post
    That doesn't work though. I want it to be able so that when I type a nine digit number, for example 123456789, I want the 1 to go into the first element puzzle[x][1], 2 into the second element puzzle[x][2], 3 into the third element puzzle[x][4], etc. But what my program is doing right is putting "123456789" all into the first element puzzle[x][1].
    Code:
    char puzzle[10][10];
    int row=0;
    scanf("%s", puzzle[row]);
    This will read the number as a string in to the first row.
    But you may have to convert the number in to integer wherever required.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #8
    New to the CF scene
    Join Date
    Nov 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It has to work because when demoing my professor's program, all you have to type is the nine-digit line and it would seperate the numbers instead of reading it whole as one.

  • #9
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by rure View Post
    It has to work because when demoing my professor's program, all you have to type is the nine-digit line and it would seperate the numbers instead of reading it whole as one.
    Well you could do it abduraooft's way and then once you read the values in, translate them into your int array using a for loop.
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •