从 CSV 文件存储课程记录,结构存储不正确
Storing course records from a CSV file, structures not storing properly
所以我有这个程序,它应该接受一个 CSV 文件来读取输入,如此给出。
CSV:
programming, 03, 60, 141, 1, 30, 0, W, 2015
algebra, 03, 62, 102, 2, 0, 0, S, 2013
religion, 08, 98, 938, 1, 30, 90, W, 2015
我们应该读入我们的结构,从那里会打开一个 switch/case
菜单,允许您添加课程数据、搜索课程并在 table 中显示课程。现在我在将每个读取输入存储到其给定的结构变量中时遇到问题。
我已经分配了 char []
值来暂时存储这些值,如您所见,我在获得它们后立即将它们打印出来。问题是当我尝试使用 strcpy()
函数将每个给定变量加载到 struct
时,字符串值不会复制。我试过查看多个不同的例子,感觉自己走到了死胡同。我以前在另一个程序中使用过这个逻辑。我猜最大的区别是 buff char
指针具有 malloc
大小 struct
。截至目前,courseID
和 courseSections
仅打印出来,这让我相信这是存储字符串的问题。我现在不知道如何解决这个问题。
//Assignment 3
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define filename "courseInfo.csv"
struct CourseInfo{
int courseID;
char courseName[40];
char courseCode[10];
char Term [6];
int courseSections[3];
};
typedef struct CourseInfo courseInfo; //optional
courseInfo courseList[3];
void loadCourseInfo() //To read all data from the input file (courseInfo.csv) and format and store
//them in an array of strutcures
{
/* FileStream for the Library File */
FILE *fptr;
/* allocation of the buffer for every line in the File */
char *buf = malloc(sizeof(courseInfo) + 1);
char *tmp;
/* if the space could not be allocaed, return an error */
if (buf == NULL) {
printf ("No memory\n");
}
if ( ( fptr = fopen(filename, "r" ) ) == NULL ) //Reading a file
{
printf( "File could not be opened.\n" );
}
int i = 0;
char nameX[40];
char facX[2];
char subX[2];
char levX[3];
int sec1, sec2, sec3;
char semX[1];
char yearX[4];
//char courseCodeX[10];
//char TermX[6];
while (fgets(buf, 100, fptr) != NULL)
{
tmp = strtok(buf, ",");
strcpy(nameX, tmp);
printf("%s ", nameX);
tmp = strtok(NULL, ",");
strcpy(facX, tmp);
printf("%s ", facX);
tmp = strtok(NULL, ",");
strcpy(subX, tmp);
printf("%s ", subX);
tmp = strtok(NULL, ",");
strcpy(levX, tmp);
printf("%s ", levX);
tmp = strtok(NULL, ",");
sec1 = atoi(tmp);
printf("%i ", sec1);
tmp = strtok(NULL, ",");
sec2 = atoi(tmp);
printf("%i ", sec2);
tmp = strtok(NULL, ",");
sec3 = atoi(tmp);
printf("%i ", sec3);
tmp = strtok(NULL, ",");
strcpy(semX, tmp);
printf("%s ", semX);
tmp = strtok(NULL, ",");
strcpy(yearX, tmp);
printf("%s ", yearX);
printf("\n");
//strcat(courseCodeX, facX);
//strcat(courseCodeX, subX);
//strcat(courseCodeX, "-");
//strcat(courseCodeX, levX);
//printf("%s ", courseCodeX);
//strcat(TermX, semX);
//strcat(TermX, yearX);
//printf("%s ", TermX);
courseList[i].courseID = i + 1;
strcpy(courseList[i].courseName, nameX);
strcpy(courseList[i].courseCode, facX);
strcpy(courseList[i].Term, semX);
courseList[i].courseSections[0] = sec1;
courseList[i].courseSections[1] = sec2;
courseList[i].courseSections[2] = sec3;
i++;
}
//free(buf);
fclose(fptr);
}
/*void addCourseInfo(courseInfo courseList, int arraySize)To add a new course
a) CourseID should be unique and generated automatically by your program (last courseID + 1)
b) CourseCode is also unique and you can't have 2 courses with the same courCode. So before
adding a course, search for the courseCode to be sure that you have not had it previously
c) Number of sections cannot be more than 3. Also, sections numbers for each course must be unique
{
courseInfo tempcourse[arraySize + 1];
for(int i = 0; i < arraySize; i++)
{
strcpy(tempcourse[i].courseName, courseList[i].courseName);
strcpy(tempcourse[i].faculty, courseList[i].faculty);
strcpy(tempcourse[i].subject, courseList[i].subject);
strcpy(tempcourse[i].level, courseList[i].level);
strcpy(tempcourse[i].courseCode, courseList[i].courseCode);
strcpy(tempcourse[i].semester, courseList[i].semester);
strcpy(tempcourse[i].year, courseList[i].year);
strcpy(tempcourse[i].Term, courseList[i].Term);
tempcourse[i].sections[0] = courseList[i].sections[0];
tempcourse[i].sections[1] = courseList[i].sections[1];
tempcourse[i].sections[2] = courseList[i].sections[2];
tempcourse[i].courseID = courseList[i].courseID;
}
printf("Enter course name: ");
scanf("%s", tempcourse[arraySize].courseName);
printf("\nEnter faculty code (format xx): ");
scanf("%s", tempcourse[arraySize].faculty);
printf("\nEnter subject code (format xx): ");
scanf("%s", tempcourse[arraySize].subject);
printf("\nEnter level code (format xx): ");
scanf("%s", tempcourse[arraySize].level);
//This is where you need to put all three of the above together
//complete a strcmp throughout all the current records of temp course
//maybe call that in a separate function and return that in main somehow
int section;
printf("\nEnter number of sections: ");
scanf("%i", §ion);
if(section < 1 && section > 3)
{
printf("Invalid Input!\n");
printf("Enter number of sections: ");
scanf("%i", §ion);
}
else
{
for(int i = 0; i < 3; i++)
{
printf("\nEnter section code %i:". i + 1);
scanf("%s", tempcourse[arraySize].section[i]);
if(tempcourse[arraySize].section[i] == tempcourse[arraySize].section[i - 1]);
{
printf("Invalid: Repeated section code!\n");
printf("Enter section code %i:". i);//KEEP AN EYE OUT FOR THIS BUGGER
scanf("%s", tempcourse[arraySize].section[i]);
}
}
}
printf("\nEnter semester (S, F, or W): ");
scanf("%c", tempcourse[arraySize].semester);
printf("\nEnter year (format yyyy): ");
scanf("%s", tempcourse[arraySize].year);
printf("Course Added Succesfully!");
}
*/
void displayCourseInfo() //Print a table indicating course information
{
printf("ID Name Code Term Sections\n");
printf("------------------------------------------------\n");
for(int i = 0; i < 3; i++)
{
printf("%i %s %s %s %i,%i,%i \n", courseList[i].courseID, courseList[i].courseName, courseList[i].courseCode, courseList[i].Term, courseList[i].courseSections[0], courseList[i].courseSections[1], courseList[i].courseSections[2]);
}
}
/*
searchCourseID() //To search a course information using courseID and print the course information
//if the course information exists
searchCourseName()//Search for course information using courseName and print the course information
//if it the course infoamtion exists
searchCourseTerm()//Search ALL course information using Term (semester and year) and print the list
//of all course information if it exists
saveCourseInfo() //To save course information from the array of structures to a CSV file (courseInfo.csv)
*/
void switchCaseMenu(int selection)
{
int menuActive = 1;
while(menuActive)
{
printf("Assignment 3\n");
printf("------------\n");
printf("1. Add a new course\n");
printf("2. Seearch for a course ID\n");
printf("3. Search for a course by course Name\n");
printf("4. Search for all courses by term\n");
printf("5. Display course data\n");
printf("6. Save course data\n");
printf("7. Exit\n");
printf("Please enter a selection: ");
scanf("%i", &selection);
switch(selection)
{
case 7:
menuActive = 0;
break;
case 6:
printf("Selecteed - Save Course Data\n");
break;
case 5:
printf("Selected - Display Course Data\n");
displayCourseInfo();
break;
case 4:
printf("Selected - Searfh for All Courses By Term\n");
break;
case 3:
printf("Selected - Search for Course by Course Name\n");
break;
case 2:
printf("Selected - Search for Course ID\n");
break;
case 1:
printf("Selected - Add a New Course\n");
//addCourseInfo(courseInfo, );
break;
default:
printf("Invalid Input!\n");
}
}
printf("You Have Quit!\n");
}
int main()
{
loadCourseInfo();
int select = 0;
switchCaseMenu(select);
return 0;
}
使用sscanf
函数看起来像这样。
void loadCourseInfo() //To read all data from the input file (courseInfo.csv) and format and store
//them in an array of strutcures
{
/* FileStream for the Library File */
FILE *fptr;
char buf[100] = "";
if ( ( fptr = fopen(filename, "r" ) ) == NULL ) //Reading a file
{
printf( "File could not be opened.\n" );
return;
}
int i = 0;
char nameX[40];
char facX[3];
char subX[3];
char levX[4];
int sec1, sec2, sec3;
char semX[2];
char yearX[6];
while ( i < 3 && fgets(buf, 100, fptr) != NULL)
{
if ( 9 == sscanf ( buf, " %39[^,], %2[^,], %2[^,], %3[^,], %d, %d, %d, %1[^,], %4s"
, nameX, facX, subX, levX, &sec1, &sec2, &sec3, semX, yearX)) {
printf("%s ", nameX);
printf("%s ", facX);
printf("%s ", subX);
printf("%s ", levX);
printf("%d ", sec1);
printf("%d ", sec2);
printf("%d ", sec3);
printf("%s ", semX);
printf("%s ", yearX);
printf("\n");
courseList[i].courseID = i + 1;
strcpy(courseList[i].courseName, nameX);
strcpy(courseList[i].courseCode, facX);
strcpy(courseList[i].Term, semX);
courseList[i].courseSections[0] = sec1;
courseList[i].courseSections[1] = sec2;
courseList[i].courseSections[2] = sec3;
i++;
}
}
fclose(fptr);
}
这可能无法解决所有问题。
就目前而言,这是可行的...
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define filename "courseInfo.csv"
struct CourseInfo{
int courseID;
char courseName[40];
char courseCode[10];
char Term [6];
int courseSections[3];
};
typedef struct CourseInfo courseInfo; //optional
courseInfo courseList[3];
void loadCourseInfo() //To read all data from the input file (courseInfo.csv) and format and store
//them in an array of strutcures
{
/* FileStream for the Library File */
FILE *fptr;
char buf[100] = "";
char courseCodeX[10];
char TermX[6];
if ( ( fptr = fopen(filename, "r" ) ) == NULL ) //Reading a file
{
printf( "File could not be opened.\n" );
return;
}
int i = 0;
char nameX[40];
char facX[3];
char subX[3];
char levX[4];
int sec1, sec2, sec3;
char semX[2];
char yearX[6];
while ( i < 3 && fgets(buf, 100, fptr) != NULL)
{
if ( 9 == sscanf ( buf, " %39[^,], %2[^,], %2[^,], %3[^,], %d, %d, %d, %1[^,], %4s"
, nameX, facX, subX, levX, &sec1, &sec2, &sec3, semX, yearX)) {
printf("%s ", nameX);
printf("%s ", facX);
printf("%s ", subX);
printf("%s ", levX);
printf("%d ", sec1);
printf("%d ", sec2);
printf("%d ", sec3);
printf("%s ", semX);
printf("%s ", yearX);
printf("\n");
strcpy(courseCodeX, facX);
strcat(courseCodeX, subX);
strcat(courseCodeX, "-");
strcat(courseCodeX, levX);
printf("%s\n", courseCodeX);
strcpy(TermX, semX);
strcat(TermX, yearX);
printf("%s\n", TermX);
courseList[i].courseID = i + 1;
strcpy(courseList[i].courseName, nameX);
strcpy(courseList[i].courseCode, facX);
strcpy(courseList[i].Term, semX);
courseList[i].courseSections[0] = sec1;
courseList[i].courseSections[1] = sec2;
courseList[i].courseSections[2] = sec3;
i++;
}
}
fclose(fptr);
}
void displayCourseInfo() //Print a table indicating course information
{
printf("ID Name Code Term Sections\n");
printf("--------------------------------------------------------------\n");
for(int i = 0; i < 3; i++)
{
printf("%2d %39s %3s %4s %2d,%2d,%2d \n", courseList[i].courseID, courseList[i].courseName, courseList[i].courseCode, courseList[i].Term, courseList[i].courseSections[0], courseList[i].courseSections[1], courseList[i].courseSections[2]);
}
}
void switchCaseMenu(int selection)
{
int menuActive = 1;
while(menuActive)
{
printf("Assignment 3\n");
printf("------------\n");
printf("1. Add a new course\n");
printf("2. Seearch for a course ID\n");
printf("3. Search for a course by course Name\n");
printf("4. Search for all courses by term\n");
printf("5. Display course data\n");
printf("6. Save course data\n");
printf("7. Exit\n");
printf("Please enter a selection: ");
scanf("%i", &selection);
switch(selection)
{
case 7:
menuActive = 0;
break;
case 6:
printf("Selecteed - Save Course Data\n");
break;
case 5:
printf("Selected - Display Course Data\n");
displayCourseInfo();
break;
case 4:
printf("Selected - Searfh for All Courses By Term\n");
break;
case 3:
printf("Selected - Search for Course by Course Name\n");
break;
case 2:
printf("Selected - Search for Course ID\n");
break;
case 1:
printf("Selected - Add a New Course\n");
//addCourseInfo(courseInfo, );
break;
default:
printf("Invalid Input!\n");
}
}
printf("You Have Quit!\n");
}
int main()
{
loadCourseInfo();
int select = 0;
switchCaseMenu(select);
return 0;
}
所以我有这个程序,它应该接受一个 CSV 文件来读取输入,如此给出。
CSV:
programming, 03, 60, 141, 1, 30, 0, W, 2015 algebra, 03, 62, 102, 2, 0, 0, S, 2013 religion, 08, 98, 938, 1, 30, 90, W, 2015
我们应该读入我们的结构,从那里会打开一个 switch/case
菜单,允许您添加课程数据、搜索课程并在 table 中显示课程。现在我在将每个读取输入存储到其给定的结构变量中时遇到问题。
我已经分配了 char []
值来暂时存储这些值,如您所见,我在获得它们后立即将它们打印出来。问题是当我尝试使用 strcpy()
函数将每个给定变量加载到 struct
时,字符串值不会复制。我试过查看多个不同的例子,感觉自己走到了死胡同。我以前在另一个程序中使用过这个逻辑。我猜最大的区别是 buff char
指针具有 malloc
大小 struct
。截至目前,courseID
和 courseSections
仅打印出来,这让我相信这是存储字符串的问题。我现在不知道如何解决这个问题。
//Assignment 3
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define filename "courseInfo.csv"
struct CourseInfo{
int courseID;
char courseName[40];
char courseCode[10];
char Term [6];
int courseSections[3];
};
typedef struct CourseInfo courseInfo; //optional
courseInfo courseList[3];
void loadCourseInfo() //To read all data from the input file (courseInfo.csv) and format and store
//them in an array of strutcures
{
/* FileStream for the Library File */
FILE *fptr;
/* allocation of the buffer for every line in the File */
char *buf = malloc(sizeof(courseInfo) + 1);
char *tmp;
/* if the space could not be allocaed, return an error */
if (buf == NULL) {
printf ("No memory\n");
}
if ( ( fptr = fopen(filename, "r" ) ) == NULL ) //Reading a file
{
printf( "File could not be opened.\n" );
}
int i = 0;
char nameX[40];
char facX[2];
char subX[2];
char levX[3];
int sec1, sec2, sec3;
char semX[1];
char yearX[4];
//char courseCodeX[10];
//char TermX[6];
while (fgets(buf, 100, fptr) != NULL)
{
tmp = strtok(buf, ",");
strcpy(nameX, tmp);
printf("%s ", nameX);
tmp = strtok(NULL, ",");
strcpy(facX, tmp);
printf("%s ", facX);
tmp = strtok(NULL, ",");
strcpy(subX, tmp);
printf("%s ", subX);
tmp = strtok(NULL, ",");
strcpy(levX, tmp);
printf("%s ", levX);
tmp = strtok(NULL, ",");
sec1 = atoi(tmp);
printf("%i ", sec1);
tmp = strtok(NULL, ",");
sec2 = atoi(tmp);
printf("%i ", sec2);
tmp = strtok(NULL, ",");
sec3 = atoi(tmp);
printf("%i ", sec3);
tmp = strtok(NULL, ",");
strcpy(semX, tmp);
printf("%s ", semX);
tmp = strtok(NULL, ",");
strcpy(yearX, tmp);
printf("%s ", yearX);
printf("\n");
//strcat(courseCodeX, facX);
//strcat(courseCodeX, subX);
//strcat(courseCodeX, "-");
//strcat(courseCodeX, levX);
//printf("%s ", courseCodeX);
//strcat(TermX, semX);
//strcat(TermX, yearX);
//printf("%s ", TermX);
courseList[i].courseID = i + 1;
strcpy(courseList[i].courseName, nameX);
strcpy(courseList[i].courseCode, facX);
strcpy(courseList[i].Term, semX);
courseList[i].courseSections[0] = sec1;
courseList[i].courseSections[1] = sec2;
courseList[i].courseSections[2] = sec3;
i++;
}
//free(buf);
fclose(fptr);
}
/*void addCourseInfo(courseInfo courseList, int arraySize)To add a new course
a) CourseID should be unique and generated automatically by your program (last courseID + 1)
b) CourseCode is also unique and you can't have 2 courses with the same courCode. So before
adding a course, search for the courseCode to be sure that you have not had it previously
c) Number of sections cannot be more than 3. Also, sections numbers for each course must be unique
{
courseInfo tempcourse[arraySize + 1];
for(int i = 0; i < arraySize; i++)
{
strcpy(tempcourse[i].courseName, courseList[i].courseName);
strcpy(tempcourse[i].faculty, courseList[i].faculty);
strcpy(tempcourse[i].subject, courseList[i].subject);
strcpy(tempcourse[i].level, courseList[i].level);
strcpy(tempcourse[i].courseCode, courseList[i].courseCode);
strcpy(tempcourse[i].semester, courseList[i].semester);
strcpy(tempcourse[i].year, courseList[i].year);
strcpy(tempcourse[i].Term, courseList[i].Term);
tempcourse[i].sections[0] = courseList[i].sections[0];
tempcourse[i].sections[1] = courseList[i].sections[1];
tempcourse[i].sections[2] = courseList[i].sections[2];
tempcourse[i].courseID = courseList[i].courseID;
}
printf("Enter course name: ");
scanf("%s", tempcourse[arraySize].courseName);
printf("\nEnter faculty code (format xx): ");
scanf("%s", tempcourse[arraySize].faculty);
printf("\nEnter subject code (format xx): ");
scanf("%s", tempcourse[arraySize].subject);
printf("\nEnter level code (format xx): ");
scanf("%s", tempcourse[arraySize].level);
//This is where you need to put all three of the above together
//complete a strcmp throughout all the current records of temp course
//maybe call that in a separate function and return that in main somehow
int section;
printf("\nEnter number of sections: ");
scanf("%i", §ion);
if(section < 1 && section > 3)
{
printf("Invalid Input!\n");
printf("Enter number of sections: ");
scanf("%i", §ion);
}
else
{
for(int i = 0; i < 3; i++)
{
printf("\nEnter section code %i:". i + 1);
scanf("%s", tempcourse[arraySize].section[i]);
if(tempcourse[arraySize].section[i] == tempcourse[arraySize].section[i - 1]);
{
printf("Invalid: Repeated section code!\n");
printf("Enter section code %i:". i);//KEEP AN EYE OUT FOR THIS BUGGER
scanf("%s", tempcourse[arraySize].section[i]);
}
}
}
printf("\nEnter semester (S, F, or W): ");
scanf("%c", tempcourse[arraySize].semester);
printf("\nEnter year (format yyyy): ");
scanf("%s", tempcourse[arraySize].year);
printf("Course Added Succesfully!");
}
*/
void displayCourseInfo() //Print a table indicating course information
{
printf("ID Name Code Term Sections\n");
printf("------------------------------------------------\n");
for(int i = 0; i < 3; i++)
{
printf("%i %s %s %s %i,%i,%i \n", courseList[i].courseID, courseList[i].courseName, courseList[i].courseCode, courseList[i].Term, courseList[i].courseSections[0], courseList[i].courseSections[1], courseList[i].courseSections[2]);
}
}
/*
searchCourseID() //To search a course information using courseID and print the course information
//if the course information exists
searchCourseName()//Search for course information using courseName and print the course information
//if it the course infoamtion exists
searchCourseTerm()//Search ALL course information using Term (semester and year) and print the list
//of all course information if it exists
saveCourseInfo() //To save course information from the array of structures to a CSV file (courseInfo.csv)
*/
void switchCaseMenu(int selection)
{
int menuActive = 1;
while(menuActive)
{
printf("Assignment 3\n");
printf("------------\n");
printf("1. Add a new course\n");
printf("2. Seearch for a course ID\n");
printf("3. Search for a course by course Name\n");
printf("4. Search for all courses by term\n");
printf("5. Display course data\n");
printf("6. Save course data\n");
printf("7. Exit\n");
printf("Please enter a selection: ");
scanf("%i", &selection);
switch(selection)
{
case 7:
menuActive = 0;
break;
case 6:
printf("Selecteed - Save Course Data\n");
break;
case 5:
printf("Selected - Display Course Data\n");
displayCourseInfo();
break;
case 4:
printf("Selected - Searfh for All Courses By Term\n");
break;
case 3:
printf("Selected - Search for Course by Course Name\n");
break;
case 2:
printf("Selected - Search for Course ID\n");
break;
case 1:
printf("Selected - Add a New Course\n");
//addCourseInfo(courseInfo, );
break;
default:
printf("Invalid Input!\n");
}
}
printf("You Have Quit!\n");
}
int main()
{
loadCourseInfo();
int select = 0;
switchCaseMenu(select);
return 0;
}
使用sscanf
函数看起来像这样。
void loadCourseInfo() //To read all data from the input file (courseInfo.csv) and format and store
//them in an array of strutcures
{
/* FileStream for the Library File */
FILE *fptr;
char buf[100] = "";
if ( ( fptr = fopen(filename, "r" ) ) == NULL ) //Reading a file
{
printf( "File could not be opened.\n" );
return;
}
int i = 0;
char nameX[40];
char facX[3];
char subX[3];
char levX[4];
int sec1, sec2, sec3;
char semX[2];
char yearX[6];
while ( i < 3 && fgets(buf, 100, fptr) != NULL)
{
if ( 9 == sscanf ( buf, " %39[^,], %2[^,], %2[^,], %3[^,], %d, %d, %d, %1[^,], %4s"
, nameX, facX, subX, levX, &sec1, &sec2, &sec3, semX, yearX)) {
printf("%s ", nameX);
printf("%s ", facX);
printf("%s ", subX);
printf("%s ", levX);
printf("%d ", sec1);
printf("%d ", sec2);
printf("%d ", sec3);
printf("%s ", semX);
printf("%s ", yearX);
printf("\n");
courseList[i].courseID = i + 1;
strcpy(courseList[i].courseName, nameX);
strcpy(courseList[i].courseCode, facX);
strcpy(courseList[i].Term, semX);
courseList[i].courseSections[0] = sec1;
courseList[i].courseSections[1] = sec2;
courseList[i].courseSections[2] = sec3;
i++;
}
}
fclose(fptr);
}
这可能无法解决所有问题。
就目前而言,这是可行的...
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define filename "courseInfo.csv"
struct CourseInfo{
int courseID;
char courseName[40];
char courseCode[10];
char Term [6];
int courseSections[3];
};
typedef struct CourseInfo courseInfo; //optional
courseInfo courseList[3];
void loadCourseInfo() //To read all data from the input file (courseInfo.csv) and format and store
//them in an array of strutcures
{
/* FileStream for the Library File */
FILE *fptr;
char buf[100] = "";
char courseCodeX[10];
char TermX[6];
if ( ( fptr = fopen(filename, "r" ) ) == NULL ) //Reading a file
{
printf( "File could not be opened.\n" );
return;
}
int i = 0;
char nameX[40];
char facX[3];
char subX[3];
char levX[4];
int sec1, sec2, sec3;
char semX[2];
char yearX[6];
while ( i < 3 && fgets(buf, 100, fptr) != NULL)
{
if ( 9 == sscanf ( buf, " %39[^,], %2[^,], %2[^,], %3[^,], %d, %d, %d, %1[^,], %4s"
, nameX, facX, subX, levX, &sec1, &sec2, &sec3, semX, yearX)) {
printf("%s ", nameX);
printf("%s ", facX);
printf("%s ", subX);
printf("%s ", levX);
printf("%d ", sec1);
printf("%d ", sec2);
printf("%d ", sec3);
printf("%s ", semX);
printf("%s ", yearX);
printf("\n");
strcpy(courseCodeX, facX);
strcat(courseCodeX, subX);
strcat(courseCodeX, "-");
strcat(courseCodeX, levX);
printf("%s\n", courseCodeX);
strcpy(TermX, semX);
strcat(TermX, yearX);
printf("%s\n", TermX);
courseList[i].courseID = i + 1;
strcpy(courseList[i].courseName, nameX);
strcpy(courseList[i].courseCode, facX);
strcpy(courseList[i].Term, semX);
courseList[i].courseSections[0] = sec1;
courseList[i].courseSections[1] = sec2;
courseList[i].courseSections[2] = sec3;
i++;
}
}
fclose(fptr);
}
void displayCourseInfo() //Print a table indicating course information
{
printf("ID Name Code Term Sections\n");
printf("--------------------------------------------------------------\n");
for(int i = 0; i < 3; i++)
{
printf("%2d %39s %3s %4s %2d,%2d,%2d \n", courseList[i].courseID, courseList[i].courseName, courseList[i].courseCode, courseList[i].Term, courseList[i].courseSections[0], courseList[i].courseSections[1], courseList[i].courseSections[2]);
}
}
void switchCaseMenu(int selection)
{
int menuActive = 1;
while(menuActive)
{
printf("Assignment 3\n");
printf("------------\n");
printf("1. Add a new course\n");
printf("2. Seearch for a course ID\n");
printf("3. Search for a course by course Name\n");
printf("4. Search for all courses by term\n");
printf("5. Display course data\n");
printf("6. Save course data\n");
printf("7. Exit\n");
printf("Please enter a selection: ");
scanf("%i", &selection);
switch(selection)
{
case 7:
menuActive = 0;
break;
case 6:
printf("Selecteed - Save Course Data\n");
break;
case 5:
printf("Selected - Display Course Data\n");
displayCourseInfo();
break;
case 4:
printf("Selected - Searfh for All Courses By Term\n");
break;
case 3:
printf("Selected - Search for Course by Course Name\n");
break;
case 2:
printf("Selected - Search for Course ID\n");
break;
case 1:
printf("Selected - Add a New Course\n");
//addCourseInfo(courseInfo, );
break;
default:
printf("Invalid Input!\n");
}
}
printf("You Have Quit!\n");
}
int main()
{
loadCourseInfo();
int select = 0;
switchCaseMenu(select);
return 0;
}