| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
#include "pch.h" 
#include <iostream> 
#include <stdlib.h> 
#include <malloc.h> 
 
struct student 
{ 
        int age; 
        char sex; 
        float score; 
        char name[];//字符数组 
}; 
 
int main() 
{ 
        int num; 
        struct student *pstu; 
         
        printf("请输入学生的人数:\n"); 
        printf("人数为 = "); 
        scanf("%d", &num); 
 
        pstu = (struct student *)malloc(num * sizeof(struct student)); 
 
        for (int i = 0; i < num; i++) 
        { 
                printf("请输入%d学生的信息:\n", i + 1); 
                printf("年龄:"); 
                scanf("%d", &pstu[i].age); 
                printf("\n性别:"); 
                scanf("%s", &pstu[i].sex); 
                printf("\n成绩:"); 
                scanf("%f", &pstu[i].score); 
                printf("\n姓名:"); 
                scanf("%s", pstu[i].name); 
        } 
        printf("输出的学生信息为:\n"); 
        for (int i = 0; i < num; i++) 
        { 
                printf("第%d个学生的信息为:", i+1); 
                printf("年龄为 : %d 性别为: %c 成绩为:%f 姓名为: %s\n", pstu[i].age 
                        , pstu[i].sex, pstu[i].score, pstu[i].name); 
        } 
 
        system("pause"); 
        return 0; 
} 
 
 
今天学习简单的数据结构体,这个简单的学生录入系统。每次执行都会有问题,有谁知道什么原因吗? 
性别只能出现一个字符,姓名 第一个不显示 
请指教一下,谢谢!!!! 
 |   
 
 
 
 |