首页 > 解决方案 > 如何不覆盖字符串并将其放入 typedef 函数

问题描述

当我注册时,我的输出给出了相同的名称和 au-ID。
所以我认为,当用户为第二个学生提供输入时,它会覆盖姓名和 au-ID。

我尝试调用数组 [i] 而不是 [20] 并且我尝试使用 malloc 来制作不同的数组并为它们腾出空间。

typedef struct student
{
    char*auID;
    char* name;
    int age;
}student;

int main(void)
{
    //allocate space for student
    int enrolledment = get_int("enrolledment:");
    student students[enrolledment];

    //promt student for name and age and auID
    for(int i=0; i<enrolledment; i++)
    {

         //promt for au-ID
         char getauID[20];
         printf("Enter au-ID: ");
         scanf("%s", getauID);
         students[i].auID=getauID;

        //promt for name
         char getName[20];
         printf("Enter first name: ");
         scanf("%s", getName);
         students[i].name=getName;

        //promt for age
        int getAge;
        printf("Enter age:");
        scanf("%i",&getAge);
        students[i].age=getAge;

    }


    //print students name and age and auID
     for(int i=0; i<enrolledment; i++)
     {
         printf("\n %s is %i and has this au-ID number: %s.\n",students[i].name, students[i].age, students[i].auID);
     }

}

当我将注册设置为 2 时,我希望输出到不同的名称和 auID,但实际输出只是相同的名称和 auID,即使 a 输入了不同的

标签: c

解决方案


以下建议的代码:

  1. 干净地编译
  2. 执行所需的功能
  3. 正确检查 I/O 错误
  4. 正确地将错误消息输出到“stderr”
  5. 分开,以提高灵活性。'typedef' 中的结构定义
  6. 处理任何不可恢复的错误后退出
  7. 由于学生人数不能 <0,因此学生人数使用无符号值
  8. 因为年龄不能<0,所以使用无符号年龄
  9. 为了便于阅读和理解,包含适当的水平和垂直间距
  10. 使用 C 的 VLA(可变长度数组)特性来声明 'struct student' 条目数组的长度

现在,建议的代码:

#include <stdio.h>   // printf(), fprintf(), scanf()
#include <stdlib.h>  // exit(), EXIT_FAILURE

#define MAX_FIELD_LEN 20

struct student
{
    char auID[ MAX_FIELD_LEN ];
    char name[ MAX_FIELD_LEN ];
    unsigned age;
};
typedef struct student STUDENT;


int main( void )
{
    //allocate space for students
    unsigned enrolledment;
    if( scanf( "%u", &enrolledment ) != 1 )
    {
        fprintf( stderr, "scanf for number of students failed\n" );
        exit( EXIT_FAILURE );
    }
    // implied else, scanf for number of students successful

    STUDENT students[enrolledment];

    //prompt for  student name and age and auID
    for( unsigned i=0; i<enrolledment; i++ )
    {
        // --> input fields directly into the array entry
        //prompt for au-ID
        printf( "Enter au-ID: " );
        // --> always check for I/O errors and
        // --> limit char array input length
        // --> to one less than the input buffer length
        // --> because '%s' always appends a NUL byte
        if( scanf("%19s", students[i].auID ) != 1 )
        {
            // --> output error messages to 'stderr'
            fprintf( stderr, "scanf for auID failed\n" );
            exit( EXIT_FAILURE );
        }
        // implied else, scanf for student ID successful

        //prompt for name
        printf( "Enter first name: " );
        if( scanf("%19s", students[i].name ) != 1 )
        {
            fprintf( stderr,  "scanf for name failed\n" );
            exit( EXIT_FAILURE );
        }
        // implied else, scanf for student name successful

        //prompt for age
        printf( "Enter age:" );
        if( scanf( "%u", &students[i].age ) !=1 )
        {
            fprintf( stderr, "scanf for age failed\n" );
            exit( EXIT_FAILURE );
        }
        // implied else, scanf for student age successful
    }


    //print students name and age and auID
    for( unsigned i=0; i<enrolledment; i++ )
    {
        // --> honor the right page margin
        printf( "\n %s is %u and has this au-ID number: %s.\n",
                students[i].name, 
                students[i].age, 
                students[i].auID );
    }
}

推荐阅读