Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C编程中验证电话号码_C - Fatal编程技术网

如何在C编程中验证电话号码

如何在C编程中验证电话号码,c,C,我正在验证一个7位数的C程序。我需要从哪里开始 例如,如果输入的数字少于7位或多于7位,则要求重新输入有效的7位,考虑使用以读入(限制读入的字符数),并测试字符串长度大小: char buf[10] = {0}; // Adding a little extra do { printf("Enter a phone number# "); scanf("%10s", buf); if(strnlen(buf, 10) != 7) { printf("Invalid pho

我正在验证一个7位数的C程序。我需要从哪里开始 例如,如果输入的数字少于7位或多于7位,则要求重新输入有效的7位,考虑使用以读入(限制读入的字符数),并测试字符串长度大小:

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");
注意,您可能希望尝试测试其他错误情况,例如字母和数字、符号等的混合

以前,我为C++用户回答了这个问题(我不知道我在想什么,我的坏…),但是我认为值得考虑的选项。在这种情况下,考虑将输入捕获为

std::string
;这有一个方法。例如,您可以有如下内容:

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);
std::string phoneInput=“”;
做
{
std::cout电话输入;
如果(phoneInput.size()!=7)
{
std::cout考虑使用来读取(限制要读取的字符数),以及测试字符串长度大小:

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");
注意,您可能希望尝试测试其他错误情况,例如字母和数字、符号等的混合

以前,我为C++用户回答了这个问题(我不知道我在想什么,我的坏……),但是我认为值得考虑的选项。在这种情况下,OnPipe捕获输入为<代码> STD::String ;这有一个方法。例如,你可以有这样的事情:

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);
std::string phoneInput=“”;
做
{
std::cout电话输入;
如果(phoneInput.size()!=7)
{
std::cout步骤1,读取一行输入

2:解析它。使用
%n”
记录扫描位置的偏移量。
%8[0-9]“
查找最多7+1位数字

  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();
}
步骤1,读取一行输入

2:解析它。使用
%n”
记录扫描位置的偏移量。
%8[0-9]“
查找最多7+1位数字

  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();
}

我不知道这个问题最初是否有一个C++标签,但是如果没有,C++的回答就没什么帮助了。我没有看到iPhone和应用程序的编辑痕迹。@安吉洛-不是我的答案。你应该删除<代码> @ /Cord>位来解决JHIL515。/这是我在int中的尝试。“输入电话号码#”);scanf(“%s”、&phoneInput);while(sizeof(phoneInput)!=7){if(sizeof(phoneInput)!=7){printf(“无效的电话号码,请重试!”);scanf(“%s”、&phoneInput);}if(sizeof(phoneInput)==7){printf(“谢谢”)}”JHIL515谢谢,但这是C++代码。我需要在CopOPS中完成它。昨晚有点晚了。我编辑了答案,但也保存了C++实现。我希望你能欣赏到最后讨论的两种实现方式。我不知道这个问题最初是否有C++标签,但是如果没有。T,C++的答案没有多大帮助。我没有看到iPhone和应用程序的编辑的迹象。@安吉洛-不是我的答案。你应该删除<代码> @ /Cord>位来处理JHIL515。/这里是我的尝试,在INT音素(){int Point输入;PrINTF(“输入电话号码”));SCANF(“%s”,和Point输入);(sisiof(Point Enter)!=7){if(sizeof(phoneInput)!=7){printf(“无效的电话号码,请重试!”);scanf(“%s”,&phoneInput);}}if(sizeof(phoneInput)==7){printf(“谢谢”);}JHIL515谢谢,但这是C++代码。我需要在CopOPS中完成它。昨晚有点晚了。我编辑了答案,但也保存了C++实现。我希望你能在最后讨论比较两个实现的讨论。