C的新手,在多个功能中使用用户输入

时间:2015-02-05 02:57:06

标签: c input

我正在尝试使用相同的用户输入创建多个函数,我从小数开始,所以我有一个输入函数和输出

接下来我要做十六进制/ 8位二进制等等,但我认为我的语法有误。我很困惑,我以为你从main调用了用户输入,但我不认为这是正确的。

有人有任何建议可以提供帮助吗?它说变量是冲突的

  #include <stdio.h>
 int main (void)
 {
   int x;
    scanf("%d", &x);

    in_decimal();
   out_decimal(x);

     }

    int in_decimal(void) {

     printf(" Please type in a decimal:");

     }


  void out_decimal(int x;){
    printf("%d",&x);
    }

1 个答案:

答案 0 :(得分:0)

尝试这个未完成的代码:

#include <stdio.h>
#include <ctype.h>

unsigned int in_decimal();
void out_decimal(unsigned int x);

int main (void)
{
    unsigned int x;
    x = in_decimal();
    out_decimal(x);

}

unsigned int in_decimal() 
{
    printf("Please type in a decimal: ");
    int ch;
    unsigned int x = 0;
    do
    {
        ch = getchar();
        if( ch < '0' || ch > '1')
        {
            break;
        }
        else
        {
            x *= 2;
            x += ch - '0';
        }
    }
    while( ch != '\n'); 
    return x;
}


void out_decimal(unsigned int x){
    if(x > 0)
    {
        out_decimal(x/2);
    }
    putchar('0' + x%2);
}

现在out_decimal()是递归函数,并且总是在大于零的数字的开头输出0。想想你可以改变什么: - )