簡體   English   中英

在 c 中使用嵌套結構、數組和循環

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

我的程序“總分”顯示不正確? 為什么?

這是我的程序...

#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;

}

假設,我的程序的 output

輸入

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

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 應該是一個字符數組(因為您使用gets(std[i].date);在您的代碼中):

char date[30]; 

您必須初始化std[1].total的值:

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

正如@Barmar 的評論,不要使用gets ,而是使用fgets 請參閱為什么獲取 function 如此危險以至於不應該使用它?

並使用: scanf("%29s",std[i].add.name); 而不是scanf("%s",std[i].add.name); . 請參閱scanf 的缺點

我在您的代碼中進行了一些更改(請參閱代碼注釋):

#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;

}

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