PDA

View Full Version : arrays and scanf

rure
11-03-2007, 04:43 AM
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.

abduraooft
11-03-2007, 08:39 AM
How can we know without seeing your code?

rure
11-03-2007, 10:14 AM
Well what I am using at the moment is

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.

abduraooft
11-03-2007, 12:17 PM
scanf("%d", &puzzle[row][col]);

rure
11-05-2007, 04:20 AM
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].

oracleguy
11-05-2007, 05:08 AM
scanf("%d", &puzzle[row][col]);

That'd probably work with chars but I don't think it would work with integers.

abduraooft
11-05-2007, 07:29 AM
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].

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.

rure
11-05-2007, 10:05 PM
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.

oracleguy
11-05-2007, 11:13 PM
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.