-3

I was wondering if someone knows how to allow a user to input both string(char) and integers at the same time where each character of the input is verified. An example of the input would be "05JK1010". I’ve only started studying the C language last week so I only have this figured out

char studentID [20];

printf("Enter your student ID:\n");
        scanf("%s", &studentID );
David C. Rankin
  • 69,681
  • 6
  • 44
  • 72
  • characters are simply numbers that are represented by a letter or symbol. The character '0' has a decimal value of 48. Here is a [table](http://www.asciitable.com/) with all of the ascii characters and their numerical values. You can process the entire input as a string using this information. – Joshua Yonathan Mar 22 '20 at 01:21
  • 2
    Unclear: in what sense would input `05JK1010` be both string and `int` input? Maybe some additional information about intended usage would help. – ad absurdum Mar 22 '20 at 02:06
  • If you are wanting to verify each char or integer as it is being input, you would have to use a loop using a function like getch(), then compare that input against a saved key before continuing to the next value from the user. I don't believe getch() is portable to all platforms though. – dmaelect Mar 22 '20 at 02:08
  • Don't develop bad habits. Validate every input function used by ***checking the return***. Use a temporary buffer to read the entire line using `fgets()`, e.g. `#define MAXC 1024` then `char buf[MAXC]; fputs ("Enter ID: ", stdout); if (fgets (buf, MAXC, stdin) == NULL) { /* handle error */ }` then use `sscanf` to parse the values you need from `buf`, e.g. `if (sscanf (buf, "%19s", studentID) != 1) { /* handle error */ }; printf ("got ID: %s\n", studentID);`. You can also simply use a pair of pointers to work from the beginning to end of `buf` picking out what you need. Up to you. – David C. Rankin Mar 22 '20 at 02:42
  • 1
    See [Determine number or character in textfile C](https://stackoverflow.com/a/60276153/3422102) for an approach to handling mixed input similar to your ID. – David C. Rankin Mar 22 '20 at 02:56

2 Answers2

2

You're close but I think I have a better way of doing this. GNU libc's scanf accepts ranges with its %[ format, like this:

scanf("%19[0-9a-zA-Z]", studentID);

You should put a different size in place of 19 if you resize your array. This is in place to ensure that scanf doesn't overflow the buffer.

Also, you generally shouldn't take the address of one of scanf's parameters if the format specifier for that parameter is %s or %[.

If you require a portable solution, then it's a mouthful:

scanf("%19[0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]", studentID);

For more details about scanf, see my post What can I use for input conversion instead of scanf?

S.S. Anne
  • 13,819
  • 7
  • 31
  • 62
  • 1
    Proper use of the *field-width* modifier and explanation to prevent buffer overflow -- worth the UV on its own. Reading with `fgets()` and parsing with `sscanf()` would be worth 2 if I could give both. – David C. Rankin Mar 22 '20 at 02:45
0

C doesn't have a facility to allow integers and chars to be input at the same time. Like Joshua commented above characters are just integers represented as ASCII symbols.

If you need the numbers entered to be in numerical integer form, you would have to parse them from the string and use various methods to convert them to there numerical value.

C does have a function that will convert ASCII to integer atoi()

http://www.cplusplus.com/reference/cstdlib/atoi/

PLEASE note, atoi() dose not have error checking, this is just to give you ideas.

dmaelect
  • 141
  • 1
  • 10
  • I'm not sure about this. Just `%[0-9a-zA-Z]` to `scanf` should work. – S.S. Anne Mar 22 '20 at 01:53
  • @S.S.Anne, that just masks the input to only be numbers 0 - 9, chars a - z or A -Z. It will still be stored in a char array as the ascii values. – dmaelect Mar 22 '20 at 01:57
  • I think that's the intention. The OP just doesn't know that integers can be stored in characters. – S.S. Anne Mar 22 '20 at 02:12
  • 1
    @S.S.Anne, Yeah, that could be, the part that throws me is his specific request -- "input both string(char) and integers at the same time where **each character of the input is verified**". Whether it is an int or a char, each "char" can be verified. – dmaelect Mar 22 '20 at 02:20
  • 2
    OOOF! never recommend `atoi()` it provides zero error checking or diagnostic. You could do `int i = atoi ("cow");` and it would silently return zero as the integer value. Either read into a buffer and use `sscanf()` or declare a temporary `long` and use `strtol()` -- which provides complete validation capability. C does allow you to enter as many mixed digits and alpha-characters as you like. Parsing them into integers and strings is up to you. All can be done. – David C. Rankin Mar 22 '20 at 02:49