0

I'm trying to validate a 7 digit number C program . Where do i need to start for example if less than 7 digit entered or more than 7 entered it ask to re enter a valid 7 digit

Angelo
  • 39
  • 1
  • 1
  • 7

2 Answers2

1

Consider using scanf() to read in (limiting the number of characters to read in), and strnlen() to test for string length size:

char buf[10] = {0}; // Adding a little extra
do
{
  printf("Enter a phone number# ");
  scanf("%10s", buf);
  if(strnlen(buf, 10) != 7)
  {
    printf("Invalid phone number size provided!\n"); // Do some special error handling
  }
}while(strnlen(buf, ) != 7);
printf("I got a phone number now!\n");

Note, you might want to try testing for other error cases, like a mixture of letters and numbers, symbols, etc.

Previously, I answered this for C++ users (dunno what I was thinking, my bad...), but I think it's worth considering the options. In this case, onsider capturing the input as a std::string; this has a size() method. For example, you could have something like this:

std::string phoneInput = "";
do
{
  std::cout << "Enter phone number# " << std::endl;
  std::cin >> phoneInput;
  if(phoneInput.size() != 7)
  {
    std::cout << "Invalid phone number, please try again!" << std::endl;
  }
}
while(phoneInput.size() != 7);

The key difference here is that C++'s string library dynamically allocates and resizes the string, so there's little concern for potential memory overruns. In C, however, if we're not careful to check the input size, scanf() could easily overflow the buffer itself (without the security string).

jhill515
  • 894
  • 8
  • 26
  • 3
    I don’t know if the question originally had a C++ tag, but if it didn’t, a C++ answer isn’t much help. I don’t see signs of an edit from an iPhone and the app. – Jonathan Leffler Mar 30 '19 at 03:33
  • @Angelo — not my answer. You should remove the `@` bit to address jhill515. – Jonathan Leffler Mar 30 '19 at 14:16
  • //here is my attempt at it int PhoneNumber(){ int phoneInput ; printf( "Enter phone number# "); scanf("%s", &phoneInput); while(sizeof(phoneInput) != 7) { if(sizeof(phoneInput) != 7) { printf("Invalid phone number, please try again!"); scanf("%s", &phoneInput); } } if(sizeof(phoneInput) == 7 ){ printf("thanks"); } } – Angelo Mar 30 '19 at 14:25
  • @jhill515 thanks but this is C++ code you gave me . I need to be able to do it in C – Angelo Mar 30 '19 at 14:30
  • Whoops! It was a little late last night. I edited the answer, but preserved the C++ implementation as well. I hope you can appreciate the discussion at the end comparing both implementations. – jhill515 Mar 30 '19 at 18:25
  • @jhill515 thanks for the attempt it's still not working after entering a proper 7 digit number it keep looping and asking to enter a proper number :( – Angelo Apr 01 '19 at 23:41
  • The width specified in `scanf("%10s", buf);` is incorrect: for a 10 byte array, it should be `scanf("%9s", buf);`. No reason to use `strnlen` instead of `strlen` – chqrlie Apr 02 '19 at 13:29
  • All that you've done is made sure that `scanf()` is limited to reading 9 characters. There are other ways to overflow `buf`, so I still recommend using the more secure `strnlen()` over `strlen()`. – jhill515 Apr 02 '19 at 13:33
  • @jhill515: you can use `strnlen()` if you insist, but at least correct the format string, test the `scanf()` return value and fix `}while(strnlen(buf, ) != 7);` – chqrlie Apr 03 '19 at 03:13
1

Step 1, read a line of input

char buffer[100];
if (fgets(buffer, sizeof buffer, stdin)) {

2: parse it. Use "%n" to note offset of scan location. "%8[0-9]" to look for up to 7 + 1 digits.

  int n;
  n = 0;
  char number[9];
  sscanf(buffer, " %8[0-9] %n", number, &n);
  if (n == 0) return fail; // no digits
  if (buf[n]) return fail; // junk after the digits
  if (strlen(buf) != 7) return fail; // not 7 digits
  Success();
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256