1

My aim is to accept 4-digit numbers, and 4-character strings (string should not contain digits or special characters)

If an invalid input is given the program should not terminate and it must allow the user to enter the details and continue until he wish to terminate.

I am able to find whether the input is a digit.

if(scanf("%d",&input)!=1)
{
    printf("enter the number please");
    ... // I have option to re enter using while and flags
}
else
{
    // I continue my work
    ...
}

To check it is four digits I have tried using the commands

i=0;
num = input;
while(num>0)
{
    i = i+1;
    num = num/10;
}
if(i==4){
    ...//I continue
}
else
    printf("please enter four digit");

I have no idea of checking the same for characters. (I know how to check its length using strlen())

Please help me with the code in C. (Also help me to reduce/optimize the above logic to check whether the input is a 4-digit number)

phuclv
  • 37,963
  • 15
  • 156
  • 475
MELWIN
  • 1,093
  • 4
  • 12
  • 19

5 Answers5

2

I believe you want 2 inputs a number and a string. You can do that as

int number= 0;
char string[10] = { 0 };

do {
    printf("please enter four digit");
    scanf("%d", &number);
    if(number >=1000 && number<= 9999)
       break;
} while(1);

do {
    printf("please enter four character string");
    fgets(string, sizeof(string), stdin);
    if(strlen(string) == 4)
       break;
} while(1);
Rohan
  • 52,392
  • 12
  • 90
  • 87
  • for string its not working. can you show a complete prgm. the prgm should display "invalid please re enter" if its not correct. else let it continue till i press x. the code above never terminates. even if i enter a vaild or invalid input. please edit the answer or give new answer. thank you – MELWIN Jan 17 '14 at 06:41
0

To check it is four digit number you can simply put a check whether the number lies between 1000 and 9999. (I am assuming you don't want the number to start with 0.)

Sarwan
  • 595
  • 3
  • 8
  • 21
0
char input[16];
int ok = 1, k = 0;
if (scanf("%s", input) > 0 && strlen(input) == 4) {
    // check if it's a word
    for (; k < 4; k++)
    if (!isalpha(input[k])) { 
        // check if it's a number
        for (int k = 0; k < 4; k++)
           if (!isdigit(input[k])) 
               ok = 0;
        break;
    }
}
else ok = 0;
if (!ok) 
    printf("invalid input, please enter a 4-digit number or 4-letter word");
else {
    printf("valid input");
    ...
}
Bogdan
  • 51
  • 4
0

You can use gets()1 fgets() to get the whole line and check line length. If the first character is between '0' and '9' then check the remaining if they are 3 numbers too. If the first character is a valid character in string then check the 3 remaining chars if it's also valid in string.


1See Why is the gets function so dangerous that it should not be used?

phuclv
  • 37,963
  • 15
  • 156
  • 475
0

strtol can help:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    char s[32], *p;
    int x;

    fgets(s, sizeof(s), stdin);
    if ((p = strchr(s, '\n')) != NULL)
        *p = '\0';
    x = (int)strtol(s, &p, 10);
    if ((p - s) == 4) {
        printf("%d\n", x);  
    } else {
        printf("Please enter four digit\n");
    }
    return 0;
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94