联合阅读字符串出错了

时间:2018-04-21 15:35:32

标签: c unions

我没有获得所需的regd_num和名称我希望regd_num为567,但它显示67和类似的名字我想要raj,但它显示C你能帮我如何在联盟中正确阅读字符串

#include<stdio.h>

union student{
    int reg;
    char name[20];
    int marks[10];

};

int main()
{
    union student s;
    int i;
    float avg,sum;

    printf("Enter the register number:");
    scanf("%d",&s.reg);
    printf("\nEnter the name:");
    scanf("%s",s.name);
    printf("\nEnter the marks:\n");
    for(i=0;i<3;i++)
    {
        scanf("\n%d",&s.marks[i]);
    }
    sum=s.marks[0]+s.marks[1]+s.marks[2];
    if(s.marks[0]>=0&&s.marks[1]>=0&&s.marks[2]>=0)
    {
        if(s.marks[0]>=40&&s.marks[1]>=40&&s.marks[2]>=40)
        {
            avg=sum/3;
            printf("\nRegd.No:%d",s.reg);
            printf("\nName:%s",s.name);
            printf("\nPercentage: %0.2f",avg);
        }
        else
            printf("Grade is Failed");
    }
    else
        printf("Invalid mark. Mark should between 0-100");
}

0 个答案:

没有答案