首页 > 解决方案 > 我正在尝试打印电话号码,但出现错误

问题描述

只是为了让你们都知道我在编程方面很新,我想我只是错过了一些小东西,但我不知道它是什么。当我编译我的代码时,我收到此错误“格式%d需要一个匹配的int参数 [ -Wformat=]”。我已经尝试了很多东西。我已经尝试将所有函数类型char都设置为int. 我得到类似的错误。

char *phoneInput(void)
{
    char phone[11];

    printf("Input the politicians phone number with no spaces: ");
    fgets(phone, 10, stdin);
    return 0;
}

char  printPhone(char phone[11])
{
    printf("- Phone Number: (%d%d%d)%d%d%d-%d%d%d%d", phone[0], phone[1], phone[2], 
              phone[3], phone[5], phone[6], phone[7], phone[8], phone[9]);

    return 0;
}

标签: carrays

解决方案


您的printf陈述缺少另一个论据。添加phone[4]到您的printf喜欢这个。

#include <iostream>

using namespace std;

char *phoneInput(void)
{
    char phone[11];

    printf("Input the politicians phone number with no spaces: ");
    fgets(phone, 10, stdin);
    return 0;
}

char  printPhone(char phone[11])
{
    printf("- Phone Number: (%d%d%d)%d%d%d-%d%d%d%d", phone[0], phone[1], phone[2],
              phone[3], phone[4], phone[5], phone[6], phone[7], phone[8], phone[9]);

    return 0;
}

但我不明白的是,为什么你所有的函数都有一个返回类型,char*或者char实际上来自两个函数,你只返回一个0


推荐阅读