简体   繁体   English

在 c 中使用嵌套结构、数组和循环

[英]Using nested struct, array and loop in c

My program "total marks" is not showing correctly?我的程序“总分”显示不正确? Why?为什么?

This is my program...这是我的程序...

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

struct student
{
    char name[30];
    int marks[5];
};
struct std_date
{
    int date;
    int total;
    struct student add;
};
int main()
{
    struct std_date std[10];
    int i;
    int j;
    for(i=0;i<2;i++)
    {
        printf("Please enter the name of the student:\n");
        scanf("%s",std[i].add.name);
        fflush(stdin);
        printf("Please enter the marks of the student:\n");
        for(j=0;j<5;j++)
        {
            scanf("%d",&std[i].add.marks[j]);

        }
        fflush(stdin);
        printf("Please enter the result date of the student:\n");
        gets(std[i].date);
        fflush(stdin);
        printf("\n");
    }
    std[0].total=0;
    for(i=0;i<2;i++)
    {
        for(j=0;j<5;j++)
        {
            std[i].total+=std[i].add.marks[j];
        }
    }
    printf("\n\n");
    for(i=0;i<2;i++)
    {
        printf("This information for %s :\n", std[i].add.name);
        printf("Total marks: %d\n", std[i].total);
        printf("Result date:\n");
        puts(std[i].date);
        printf("\n");
    }
    return 0;

}

Suppose, output of my program假设,我的程序的 output

Input输入

Please enter the name of the student:
Nihan ahmed
Please enter the marks of the student:
1
2
3
4
5

Please enter the result date of the student:
22/5/2020 

Please enter the name of the student:
Marop hossain
Please enter the marks of the student:
2
3
4
5
6

Please enter the reault date of the student:
23/5/2020

output output

This information for Nihan ahmed:
Total marks: 80
Result date: 22/5/2020

This information for Marop hossain:
Total marks: -130
Result date: 23/5/2020       
int date;

date should be an character array (because you use gets(std[i].date); in your code): date 应该是一个字符数组(因为您使用gets(std[i].date);在您的代码中):

char date[30]; 

You have to initialize value of std[1].total :您必须初始化std[1].total的值:

std[0].total=0;
std[1].total=0;

As the comment of @Barmar, do not use gets , use fgets instead.正如@Barmar 的评论,不要使用gets ,而是使用fgets See Why is the gets function so dangerous that it should not be used?请参阅为什么获取 function 如此危险以至于不应该使用它?

And use: scanf("%29s",std[i].add.name);并使用: scanf("%29s",std[i].add.name); instead of scanf("%s",std[i].add.name);而不是scanf("%s",std[i].add.name); . . See Disadvantages of scanf请参阅scanf 的缺点

I changed a bit in your code (see comment of code):我在您的代码中进行了一些更改(请参阅代码注释):

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

struct student
{
    char name[30];
    int marks[5];
};
struct std_date
{
    char date[30]; // max length of string ups to 29
    int total;
    struct student add;
};
int main()
{
    struct std_date std[10];
    int i;
    int j;
    for(i=0;i<2;i++)
    {
        printf("Please enter the name of the student:\n");
        scanf("%29s",std[i].add.name);
        printf("Please enter the marks of the student:\n");
        for(j=0;j<5;j++)
        {
            scanf("%d",&std[i].add.marks[j]);

        }

        printf("Please enter the result date of the student:\n");
        fgets(std[i].date, 30, stdin); // using fgets instead of gets
        printf("\n");
    }
    std[0].total=0;
    std[1].total=0; // init total = 0 of second student

    for(i=0;i<2;i++)
    {
        for(j=0;j<5;j++)
        {
            std[i].total+=std[i].add.marks[j];
        }
    }
    printf("\n\n");
    for(i=0;i<2;i++)
    {
        printf("This information for %s :\n", std[i].add.name);
        printf("Total marks: %d\n", std[i].total);
        printf("Result date:\n");
        puts(std[i].date);
        printf("\n");
    }
    return 0;

}

The output: output:

Please enter the name of the student:                                                                                     
std1                                                                                                                      
Please enter the marks of the student:                                                                                    
1                                                                                                                         
2                                                                                                                         
3                                                                                                                         
4                                                                                                                         
5                                                                                                                         
Please enter the result date of the student:                                                                              

Please enter the name of the student:                                                                                     
std2                                                                                                                      
Please enter the marks of the student:                                                                                    
3                                                                                                                         
2                                                                                                                         
4                                                                                                                         
5                                                                                                                         
1                                                                                                                         
Please enter the result date of the student:                                                                              



This information for std1 :                                                                                               
Total marks: 15                                                                                                           
Result date:                                                                                                              



This information for std2 :                                                                                               
Total marks: 15                                                                                                           
Result date: 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM