-
Help Needed for Program.
Code:
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <boolean.h>
#include <database.h>
#define STRICMP_ERROR -2
#define TEMP_BUF 256
#define STRICMP_LESS -1
#define STRICMP_EQUAL 0
#define STRICMP_GREATER 1
#define TRUE 0
#define FALSE -1
#define HUND 100
typedef struct
{
char number[17];
char name[65];
int credit;
int lab;
COURSE* next;
COURSE* previous;
} COURSE;
void initialize_database(void); // allows the user to populate a new record
void add_course(COURSE* course[HUND]); // inserts a new course record into the linked list
void delete_course(COURSE* course[HUND]); // removes a course record from the linked list
void search_course(COURSE* course[HUND]); // searches the linked list for a specific COURSE.number
void display_course(COURSE* course[HUND]); // displays the contents of a record
void sort(COURSE* course[HUND], unsigned size); // alphabetizes linked list by numbers
void save_database(COURSE* course[HUND]); // writes a record to a text file
void read_database(COURSE* course[HUND]); // opens a linked list
void close_database(FILE* dbase); // closes a linked list
int stricmp(char* string1, char*string2); // compares two strings
BOOLEAN order(char* string1, char* string2); // alphabetizes linked list
int main(void)
{
COURSE course[100];
int i=0;
int j=0;
int t=0;
int choice;
char junk;
// user input
do{
printf("Which of the following actions would you like to perform, hit the number then hit enter?\n 1) Open a Database\n 2) Search a Database\n 3) Add a New Course\n 4) Delete a Course\n 5) Initialize Database\n 6) Save Database\n 7) Read Database\n 8) Close Database\n 9) Quit the System: \n");
scanf("%d", &choice);
scanf("%c", &junk);
switch(choice)
{
case 1:
open_database;
break;
case 2:
search_course;
break;
case 3:
add_course;
break;
case 4:
delete_course;
break;
case 5:
initialize_database;
break;
case 6:
save_database;
break;
case 7:
read_database;
break;
case 8:
close_database;
break;
case 9:
t = -99;
break;
default:
printf("Invalid Input\n");
break;
}
}
while(t != -99);
return(0);
}
/*****************************************************************************************************************************************/
void open_database(void){
FILE* inp;
COURSE* buffer[HUND];
fgets("buffer", HUND, inp);
if(inp = fopen(buffer, "rt") != NULL){
}
else{
printf("\n\aCan't open file\n");
}
sort(course, HUND);
save_database(buffer[HUND]);
return;
}
/*****************************************************************************************************************************************/
void add_course(COURSE* course[HUND])
{
int case_status;
char ch;
int i=0;
int j=0;
int k=0;
while(course[i].number != -99)
{
i++;
}
printf("Enter the name of the course: \n");
while((ch = getchar()) != '\n')
{
course[i].name[j] = ch;
j++;
}
course[i].name[j] = '\0';
j = 0;
printf("Enter the course number: \n");
while((ch = getchar()) != '\n')
{
course[i].number[k] = ch;
k++;
}
course[i].number[j] = '\0' ;
k = 0;
printf("\n");
printf("Does the course hvae a Lab:\n"
"1)Yes or\n"
"2) No" );
scanf("%d", &case_status);
if (case_status == 1)
{
course[i].credit = 4.00;
course[i].lab = TRUE;
}
else if (case_status == 2)
{
course[i].credit = 3.00;
course[i].lab = FALSE;
}
else
{
printf("Not a valid option\n");
}
printf("Course %s was added to the database\n\n", course[i].name[j]);
save_database(course[HUND]);
return;
}
/************************************************************************************************************************************************/
void delete_course(COURSE* course[])
{
int i;
int k;
int j;
printf("Enter the course number: ");
scanf("%d", &k);
for(i=0;i<100;i++)
{
if (k == course[i].number)
{
course[i] = '\0';
printf("The course %d was successfully deteled from the database\n\n", course[i].number);
}else{
ptintf("\nThere is no course with that number. Please try again.\n");
}
}
return;
}
/***************************************************************************************************************************************************/
void search_course(COURSE* course[])
{
int num;
int i;
int j;
printf("Enter the course number of the course you would like to search for: ");
scanf("%d", &num);
for(i = 0; i < 100; i++)
if(num == course[i].number)
{
printf("Name: ");
for(j=0;j<10;j++)
{
printf("%c", course[i].name[j]);
}
printf("\n");
printf("Number: %d.\n", course[i].number);
}
if(course[i].lab == 4.00)
{
printf("Lab: Yes\n");
}
else
{
printf("Lab: No\n");
}
return(course[HUND]);
}
/******************************************************************************************************************************************************/
BOOLEAN order(char* string1, char* string2)
{
int result;
BOOLEAN compare;
result = stricmp(string1, string2);
if (result > 0)
{
compare = -1;
}
else
{
compare = 0;
}
return(compare);
}
/********************************************************************************************************************************************************/
void sort(COURSE* course[HUND], unsigned size){
int i;
int j;
int n;
for(j = 0; j < size; j++)
{
for(n = 0; n < size-1; n++)
{
i = order(course[n].name, course[n+1].name);
if (i == -1)
{
course[n].next = course[n+1].next;
course[n+1].next = course[n-1].next;
course[n+1].previous = course[n].previous;
course[n].previous = course[n+2].previous;
}
}
}
return;
}
/************************************************************************************************************************************************/
int stricmp(char* string1, char* string2){
char char1;
char char2;
int i = 0;
int ans = STRICMP_ERROR;
if(string1 && string2){
while(ans == STRICMP_ERROR){
char1 = tolower(string1[i]);
char2 = tolower(string2[i]);
if(char1 < char2)
ans = STRICMP_LESS;
else if(char1 > char2)
ans = STRICMP_GREATER;
else if(!char1 && !char2)
ans = STRICMP_EQUAL;
else i++;
}
}
return(ans);
}
/********************************************************************************************************************************************************/
void close_database(COURSE* course[HUND]){
FILE *outp;
outp = save_database(course[HUND]);
fclose(outp);
return;
}
/********************************************************************************************************************************************************/
void save_database(COURSE* course[HUND]){
int status;
FILE* outp;
COURSE* buffer[HUND];
printf("\nWould you like to save?\n");
printf("1) Yes or\n");
printf("2) No");
scanf("%d", &status);
buffer[HUND] = course[HUND];
if (status == 1)
{
fgets(buffer, HUND, outp);
if(outp = fopen(buffer, "wt") != NULL){
}
else{
printf("\n\aCan't open file\n");
}
}
else
{
printf("Not a valid option\n");
}
return;
}
/*******************************************************************************************/
void close_database(FILE* dbase)
{
char ans1;
char ans2;
printf("Are you sure you want close the database?(y/n) ");
ans1 = getchar();
if (ans1 == 'y' || ans1 == 'Y');
{
printf("Would you like to save any changes you've made?(y/n) ");
ans2 = getchar();
if (ans2 == 'y' || ans2 == 'Y')
{
save_database(dbase);
}
fclose(dbase);
}
return;
}
-
Is there a question hidden in there someplace?
We're not going to copy, compile and debug your code for you... that's your job.
That said...
All the function calls in your switch() statement starting on line 56 are incorrect...