char和int不能同一个函数?

时间:2014-04-26 16:51:14

标签: c

我正在学习C,我的任务之一就是制作一个程序,输入你的姓名,街道名称和身份证号码,然后打印出这些信息。每当程序进入身份证号码部分时,它就会疯狂。"为了记录,同一个函数中有一个intchar,也许这就是问题的根源?

#include<stdio.h>

char a,b,c,d;
char e,f,g,h,i;
int j,k,l,m,n,o,p,q;
int main()
{
    printf("\nwrite your name (4 letter's only) ");
    scanf("%c%c%c%c",&a,&b,&c,&d);
    printf("\nwrite your street name (5 letter's only')");
    scanf("%c%c%c%c%c",&e,&f,&g,&h,&i);
    printf("\nwrite your id number (8 number's only')");
    scanf("%d%d%d%d%d%d%d%d",&j,&k,&l,&m,&n,&o,&p);
    printf("your name is %c%c%c%c your street name is %c%c%c%c%c and your id number is %d%d%d%d%d%d%d%d ",a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q);
    return 0;
}

2 个答案:

答案 0 :(得分:4)

%d读取整数而不是一位数。因此,第一个%d将整个id号读入j。您不需要所有这些int变量 - 只使用一个。您也可以考虑使用%s和char数组来读取街道名称和用户名。

答案 1 :(得分:-1)

作为如何编写程序的示例。

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

int main() {
    char name[100];
    char street[100];
    int id;

    printf("Enter your name: ");
    fgets(name, sizeof name, stdin);
    strtok(name, "\n"); // remove newline from end of name

    printf("Enter your street name: ");
    fgets(street, sizeof street, stdin);
    strtok(street, "\n"); // remove newline from end of street

    printf("Enter your id number: ");
    scanf("%d", &id);

    printf("Your name is %s, your street name is %s "
           "and your id number is %d\n", name, street, id);

    return 0;
}