c - 如何不覆盖字符串并将其放入 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 输入了不同的
解决方案
以下建议的代码:
- 干净地编译
- 执行所需的功能
- 正确检查 I/O 错误
- 正确地将错误消息输出到“stderr”
- 分开,以提高灵活性。'typedef' 中的结构定义
- 处理任何不可恢复的错误后退出
- 由于学生人数不能 <0,因此学生人数使用无符号值
- 因为年龄不能<0,所以使用无符号年龄
- 为了便于阅读和理解,包含适当的水平和垂直间距
- 使用 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 );
}
}
推荐阅读
- javascript - 奇怪的 *ngFor 行为
- javascript - 如何修复在我的 GET URL 中更改的日期格式
- c++ - char *x = "geeksquiz" 是什么意思?
- ios - 添加到 UITabBar 中的 UIWindow 视图层次结构
- c - 使用 (*ptr)[5] 代替 *ptr 的目的是什么?
- java - 使用 Thread.sleep 与 CountDownLatch 保持主线程运行
- php - 重新加载后 Ajax 结果消失了
- html - 如何修复指向子页面标签的超链接?引导程序 4
- android - GCM 是否会在弃用 GCM 后为使用 GCM 的旧客户端提供新令牌
- vb.net - 如何调整与包含它们的窗口相关的控件的大小?