试图弄清楚如何在c中输出电话号码?

时间:2019-02-01 15:29:20

标签: c arrays

我正在尝试输入电话号码并以(888)999-1111格式打印出来,但是当我尝试将其打印出来时,我得到了一些奇怪的输出,而这并不是我希望重新获得的输出。我正在输入中和打印功能bt中打印出电话的值,它们是不同的。输入功能中的一个是正确的,但在打印功能中则不正确。预先感谢您的帮助。

int phoneInput(void)
{
    long int phone = 0;

    printf("Input the politicians phone number with no speaces: ");
    scanf("%ld", &phone);
    printf("test : %ld", phone);
return phone;
}

int printPhone(long int phone)
{
    int i = 10; //the number of digits in the phone
    char output[11];

    printf("Test: %ld", phone);

    for (i = 0; i < 10; i ++)
    {
    while (phone > 0)
    {
            output[i] = phone % 10;
            phone /= 10;
    }
    }

    printf("- Phone number: (");
    i = 0;
    for (i = 0; i < 3; i++)
    {
            printf("%d", output[i]);
    }

    printf(")");
    i = 3;
    for(i = 3; i < 6; i++)
    {
            printf("%d", output[i]);
    }
    i = 6;
    printf("-");
    for(i = 6; i < 10; i++)
    {
            printf("%d", output[i]);
    }

return 0;
}

6 个答案:

答案 0 :(得分:9)

您需要将值存储为intlong int或其他数字类型的唯一原因是您必须对其进行算术运算(除非作业需要这样做)规格)。不要被这样一个事实:电话号码是由数字所迷惑 - 它最有意义存储为一个字符串

如果您可以将电话号码存储为字符串,则应该:

char *phoneInput(void)
{
    static char phone[100];
    printf("Input the politicians phone number with no speaces: ");
    fgets(phone, 100, stdin);
    return phone;
}

有了它,对它执行字符串操作会更容易:

printf("(%.3s)%.3s-%.4s\n", phone, phone + 3, phone + 6);

答案 1 :(得分:3)

在您现有的代码中,问题出在循环中:

for (i = 0; i < 10; i ++)
{
while (phone > 0)
{
        output[i] = phone % 10;
        phone /= 10;
}
}

您的内部循环将继续执行而不会递增i,从而导致一次又一次地写入数组中的相同位置。

i = 9;
do
{
        output[i] = (phone % 10) + '0';
        phone /= 10;
        i--;
}while((phone > 0) && (i >= 0));

在打印"%c"时,请在printf中使用output


  

OP发布的代码中的问题列表。这些只是   OP发布的代码。逻辑可以通过许多其他方式完成,但是,   答案仅是按原样关注OP代码中的问题:

     
      
  • phoneInput函数应返回long int而不是int
  •   
  • for and while loop-不需要两个循环。这是主要错误。这将导致每次重写数组中的相同位置。
  •   
  • 数组位置的循环计数器从0开始,导致数字以相反的顺序写入数组
  •   
  • 因为您使用的是char数组,所以存储并打印char而不是int。
  •   

已完成问题修复的完整代码(按照OP的原始代码):

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

long int phoneInput(void)
{
    long int phone = 0;

    printf("Input the politicians phone number with no spaces: ");
    scanf("%ld", &phone);
    printf("test : %ld\n", phone);

    return phone;
}

int printPhone(long int phone)
{
    int i = 10; //the number of digits in the phone
    char output[11];

    memset(output, '0', sizeof(output));

    printf("Test: %ld\n", phone);

    i = 9;
    do
    {
       output[i] = (phone % 10) + '0';
       phone /= 10;
       i--;

    }while( (phone > 0) && (i >= 0));


    printf("- Phone number: (");
    i = 0;
    for (i = 0; i < 3; i++)
    {
            printf("%c", output[i]);
    }


    printf(")");
    i = 3;
    for(i = 3; i < 6; i++)
    {
            printf("%c", output[i]);
    }
    i = 6;
    printf("-");
    for(i = 6; i < 10; i++)
    {
            printf("%c", output[i]);
    }

return 0;
}

void main()
{
  long int phone;

  phone = phoneInput();
  printPhone(phone);

}

输出:

Input the politicians phone number with no spaces: 8889991111
test : 8889991111
Test: 8889991111
- Phone number: (888)999-1111

答案 2 :(得分:3)

电话号码应存储为countryareasubscriber

您给出的示例为1 888 9991111

请注意,国家/地区代码的任何前导00实际上是指示交换机拨打国际号码的本地方法。在西班牙,过去很长一段时间都是9。现在,国际号码拨号的通用标识为+

另请参阅https://www.internationalcitizens.com/international-calling-codes/

答案 3 :(得分:2)

萨拉,首先,我将电话号码扫描为字符串,这样做更容易。将其扫描为字符串long int的另一个原因可能不够,在某些平台上,长度为32位。但是,如果您需要“ long int”,则最好的方法是:

 printf ("(%ld)", number / 10000000); // prints area code (xxx)
 printf (" %ld-", (number / 1000) % 1000); // prints xxx-
 printf ("%ld\n", number % 10000); // prints xxxx\n

另一种方式是:

fscanf( stdin, " %3s%3s%4s", areacode, zone, number);
printf("(%s) %s-%s\n", areacode, zone, number);

答案 4 :(得分:2)

我对您的代码进行了一些更改。请看看。

long long int phoneInput(void)
{
    long long int phone = 0;

    printf("Input the politicians phone number with no speaces: ");
    scanf("%lld", &phone);
    printf("test : %lld", phone);
return phone;
}

int printPhone(long long int phone)
{
    int i = 10; //the number of digits in the phone
    char output[11];

    printf("Test: %lld", phone);

    i = 9;     // removed for loop that was unnecessary.
    while (phone > 0)
    {
            output[i] = phone % 10;
            phone /= 10;
            i--;   
    }

    printf("- Phone number: (");
    i = 0;
    for (i = 0; i < 3; i++)
    {
            printf("%d", output[i]);
    }

    printf(")");
    i = 3;
    for(i = 3; i < 6; i++)
    {
            printf("%d", output[i]);
    }
    i = 6;
    printf("-");
    for(i = 6; i < 10; i++)
    {
            printf("%d", output[i]);
    }

return 0;
}
int main(int argc, char const *argv[])
{
    long long int n = phoneInput();
    printPhone(n);
    return 0;
}

答案 5 :(得分:0)

while (phone > 0)
{
        output[i++] = (phone % 10) + '0';
        phone /= 10;
}

printf("%c", output[i]);

是在代码中进行的更改才能使其正常工作。

第一个更改(+ '0')是从int转换为char。 第二个原因是因为您要打印一个字符而不是一个整数。

我建议您阅读printfascii table的手册以更好地理解。