# Thread: Can a scanf function see a number input "1234" as 4 separate numbers?

1. ## Can a scanf function see a number input "1234" as 4 separate numbers?

I am doing an ISBN digit check program. I'm not actually in school, but it's on the curriculum in a lot of C classes. Naturally want to be able to do it.

I am trying to figure it all out, having tried twice and failed. My biggest hurdle/question is:

Can you have a "scanf" function that sees the user input/ISBN like this: 1234567890.

With no spaces or dashes, and have the program not treat the ISBN input as one number? Have the program understand that it should take each digit and place each one in a slot on an array? So, using the above number, it would get: isbn[0] = 1, isbn[1] = 2, etc.

I have no program to show because I have scrapped them all, seeing as how i was going about it in completely the wrong way. I want to start fresh with new knowledge.

2. Read it as a string instead. It's not a number in the sense that you wouldn't do arithmetic with an ISBN. What would that mean? Not all numbers are numeric data, some are only IDs.

3. I was under the impression that you would need to take those ISBN numbers, and multiply them with the their placement number, then add up all of those numbers. Is that still possible if it reads as a string?

Placement number as in would multiply like this: ISBN 7364956740, 1x7, 2x3, 3x6, 4x4, 5x9, etc.

4. See even if you talk about doing math with an ISBN it's meaningless.

Read it as a string. Then, copy the string to an array of ints one character at a time.

5. Provide the width to the control string format specifier ie "%1d" which tells scanf() to pick only one digit at a time from the input.

6. The check digit of an ISBN can also be X.

I suppose it would also be nice to be able to handle separators like spaces or hyphens, since ISBNs normally come grouped.

Hint: to convert chars '0'-'9' into numbers 0-9, subtract '0'.

7. Thanks for all your help so far. Here's what I have. Don't mind the printf checks. As you can see I am really new at this, and I need to see that even easy things like scans worked, by printing them.

Code:
```#include<stdio.h>
#include <windows.h>

int main()
#define MAX 10

{
/*  fill the first aray with their space numbers,
then print it to make sure*/
char  isbn[10];
int a, b, c, space[10];

for (a=1; a <= 10; a++)
{
space[a]=a;
printf("%d\n",space[a]);
}
/*  Enter isbn */
scanf("%s",isbn);
printf("\nChecking that the scanf string worked: %s",isbn);

/* So, right now, isbn is not an array of ints yet, its a
string.  It needs to be copied 1 by 1 into an array of ints*/

"%1d", b=isbn;

for (b=0; b<10; ++b)
{
printf("\n%d", isbn[0]);
}

getch();
return 0;
}```
This is where I am stuck. I want to have something like ""%1d", isbn[b],=isbn", thinking that it would assign the first digit of the isbn that the user typed in. And convert it to b, which was declared as a int, therefore making the isbn digit an integer. I wasn't worried about replicating this for the otehr digits, I just wanted to see if this would work.

Problem is I don't know if the "%1d" I have is having that effect. When I print I get an array of the same number, usually 48. So I'm afraid I'm on the wrong track now.

8. string "12345" into arrayStr[5]

now ..i want to convert this into decimal ..

STRLEN arrayStr gives 5

arrayInt[5] = arrayStr[5] - 48

LOOP AGAIN

arrayInt[4] = arrayStr[4] - 48

LOOP AGAIN

.....

till

array
arrayInt[0] = arrayStr[0] - 48

Now arrayInt[5] = 1,2,3,4,5

I think thats the logic ..if not i must be higher than a kite

9. ## Simply the simplest solution

First remember my friend its about algorithm and not the syntax of the language

Here is a standalone program run it to understand what is my solution
I printed every number-every entry of the array on a new line to prove my point
you have to edit it to match your needs , but the key idea is here

Code:
```#include<stdio.h>
#include<string.h>
void isbn();
main()
{
isbn();
}
void isbn()
{
int length;
int i=0;
char isbn[10]={'\0'};
int i_isbn[10];
printf("Enter ISBN ");
scanf("%s", isbn);
length=strlen(isbn);
for(i==0; i<length; i++)
{
i_isbn[i]=(((int)isbn[i])-48);
printf("\n%d", i_isbn[i]);
}
}```

10. Originally Posted by 8.brahim
First remember my friend its about algorithm and not the syntax of the language

Here is a standalone program run it to understand what is my solution
I printed every number-every entry of the array on a new line to prove my point
you have to edit it to match your needs , but the key idea is here

Code:
```#include<stdio.h>
#include<string.h>
void isbn();
main()
{
isbn();
}
void isbn()
{
int length;
int i=0;
char isbn[10]={'\0'};
int i_isbn[10];
printf("Enter ISBN ");
scanf("%s", isbn);
length=strlen(isbn);
for(i==0; i<length; i++)
{
i_isbn[i]=(((int)isbn[i])-48);
printf("\n%d", i_isbn[i]);
}
}```
No... it really isn't.
• Don't use implicit main()
• 48 is a rather magical number
• You have a buffer overrun if the user enters more than 9 characters -- odd considering ISBNs are of length 10.
• for(i == 0 ... is "wrong"

Sure, there is a lazy way
Code:
```[19:12:13] zac@neux:code (0) \$ cat lazy.c
#include <stdio.h>

int main(void)
{
char isbn[11] = {0};

if(scanf("%10[0-9]", isbn) == 1)
printf("isbn = %s\n", isbn);
else
printf("Invalid isbn\n");

return 0;
}```
But I wouldn't suggest doing it that way. But it does answer your original query.

11. Originally Posted by zacs7
No... it really isn't.
• Don't use implicit main()
• 48 is a rather magical number
• You have a buffer overrun if the user enters more than 9 characters -- odd considering ISBNs are of length 10.
• for(i == 0 ... is "wrong"

Sure, there is a lazy way
Code:
```[19:12:13] zac@neux:code (0) \$ cat lazy.c
#include <stdio.h>

int main(void)
{
char isbn[11] = {0};

if(scanf("%10[0-9]", isbn) == 1)
printf("isbn = %s\n", isbn);
else
printf("Invalid isbn\n");

return 0;
}```
But I wouldn't suggest doing it that way. But it does answer your original query.
hey many dont get that excited i builded and run it without a single error
and he just want a key idea to transfer a string of numbers to an array of numers
so let the one who asked decide what is the best for him
whats wrong with i==0 !!!!!!!!!!!!!
are you sure you are talking about C ?

12. i == 0 is comparing i with 0, as in:

Code:
```if(i == 0)
//do something```
What you want for assignment, is just one equal sign:

Code:
```for(i = 0; i < someNumber; i++)
//do something```

i == 0 is comparing i with 0, as in:

Code:
```if(i == 0)
//do something```
What you want for assignment, is just one equal sign:

Code:
```for(i = 0; i < someNumber; i++)
//do something```
hey yes i know i want to compare it because i already initialized it (review the code)
I told you i built the program and run it without an error why do you insist

14. Originally Posted by 8.brahim
hey yes i know i want to compare it because i already initialized it
It does not make sense to compare it in that context because that expression has no net effect. In fact, it just confuses the reader.

Originally Posted by 8.brahim
I told you i built the program and run it without an error why do you insist
Because:
Originally Posted by Abelson and Sussman
Programs must be written for people to read, and only incidentally for machines to execute.
And of course just because your program runs without an error does not mean it is correct.

15. Originally Posted by laserlight
It does not make sense to compare it in that context because that expression has no net effect. In fact, it just confuses the reader.

Because:

And of course just because your program runs without an error does not mean it is correct.
yes i know but he was talking about syntax errors and by the way your sense doesn't mean whats true and whats not
i mean try running it
it just put string of number into an array on numbers