![](/img/trans.png)
[英]accepting user inputs from the standard input where the amount of variable inputs could vary
[英]How do I create a table using inputs from a user, the amount of which changes depending on a previously specified input from the user?
我不允許使用 arrays。 我需要列出用戶在第一次 scanf_s 調用中指定的所有課程代碼、日期和時間。 我不知道如何在不使用 arrays 的情況下繼續進行。 任何幫助提示/幫助將不勝感激。
printf("Please enter the number of courses you'd like to take: ");
int numOfCourses;
scanf_s("%d", &numOfCourses);
int courseCode;
int courseDay;
int courseTime;
int i = 0;
while (i < numOfCourses) {
printf("Please enter the code of the course: ");
scanf_s("%d", &courseCode);
printf("Please enter the day of the course: ");
scanf_s("%d", &courseDay);
printf("Please enter the time of the course: ");
scanf_s("%d", &courseTime);
i++;
}
在鏈表中使用動態 memory 分配。 看看malloc
。 您的列表結構可能如下所示:
typedef struct COURSES {
int courseCode;
int courseDay;
int courseTime;
struct COURSES *next;
} t_Courses;
您按如下方式分配列表元素:
t_Courses *pCourse= malloc(sizeof(t_Courses));
然后像現在一樣讀取數據,例如:
scanf_s("%d", &pCourse->courseCode);
管理一個鏈表並不簡單。 我把它留給你作為家庭作業的一部分。 互聯網和 Stack Exchange 上有很多示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.