Hey guys, i've been working on this program and finally i am trying to implement a menu.
I've written the code, but now i'm having heaps of problems with things such as returning to the menu, and the overall functionality of the program. It hardly even works now.
It is all because of the menu and calling the functions from within and i need it to be able to accept a choice from the user, enter that function, and then return back to the menu when the user has finished input, or when the user decides to.
However i have been trying for ages and i'm absolutely stumped!, The delete function is commented because it doesnt work either.
If anyone here would be able to give a hand in fixing what is wrong with the program it would be great.
Just run it and navigate through it and you will see what i am trying to do.
Thanks again everyone.
Code:
//This program will invite the user to enter a student record consisting of student number,
//student name and telephone number, repeatedly and store the records in a structure array.
//It will then be able to sort the records in order of student name
//print the records in sorted order
//give the user the option to search for a name
//and give the user the option to delete a record.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define MAX_STUDENTS 10
typedef struct {
char studentNum[20];
char lastName[ 15 ];
char firstName[ 10 ];
char phoneNum[ 15 ];
} studentData;
void menu (void);
int read_studentRecord( studentData [] );
void print_studentRecord( studentData [] );
void sort_studentRecord( studentData [], int );
void search_studentRecord( studentData [], int );
//int delete_studentRecord(studentData details[], int);
// **************************************************
//****************************************
int main()
{
menu();
return 0;
}
// **************************************************
//***************************************
void menu (void)//problems in this function
{
int n;
int choice;
studentData details [MAX_STUDENTS]; //define a structure array of size MAX_STUDENTS
printf("Press 1 to Enter Records\n\n");
printf("Press 2 to View records in sorted form\n\n");
printf("Press 3 to Search for a record\n\n");
//printf("Press 4 to Delete a record\n\n");
printf("Press 0 to exit\n\n");
scanf("%d", &choice);
while (choice != '0')
{
if (choice == 1){
read_studentRecord( details );
n = read_studentRecord( details );
}
else if (choice == 2){
sort_studentRecord( details, n );
print_studentRecord( details );
}
else if (choice == 3){
search_studentRecord( details, n );
}
/*else if (choice == 4){
delete_studentRecord( details, n );
}
*/
else if (choice == 0){
break;
}
}
}
// **************************************************
//****************************************
int read_studentRecord ( studentData details[] )
{
int i = 0;
printf( "Enter Student Number (e.g. S00054321, 0 to end input)\n" );
scanf( "%s", details[i].studentNum );
while ( details[i].studentNum[0] != '0' )
{
printf (" Enter Last Name:\n?");
scanf ( "%s", details[i].lastName );
printf (" Enter First Name:\n?");
scanf ( "%s", details[i].firstName );
printf (" Enter Phone Number:\n?");
scanf ( "%s", &details[i].phoneNum );
i++;
printf ( "Enter Student Number\n?");
scanf ( "%s", details[i].studentNum);
}
return i;
menu();
}
//***************************************************
//***************************************
void sort_studentRecord ( studentData details[], int n )
{
studentData hold;
int i, pass;
for ( pass = 1; pass <= n - 1; pass++ )//number of passes
for ( i = 0; i <= n - pass -1; i++ )//single pass
if ( strcmp( details[i].lastName, details[i+1].lastName ) == 1) {//sort in order of last name
hold = details[i];
details[i] = details[i+1];//swap array elements if statement is true
details[i+1] = hold;
}
}
// **************************************************
//***************************************
void print_studentRecord( studentData details[] )
{
int i=0;
system("cls");
printf("Sorted by Last Name:\n\n");
if (details[0].studentNum != 0)
printf ("\n%-10s%-16s%-11s%10s\n\n", "Student #", "Last Name", "First Name", "Phone #");
while ( details[i].studentNum[0] != '0' )
{
printf("%-10s%-16s%-11s%10s\n", details[i].studentNum, details[i].lastName, details[i].firstName, details[i].phoneNum);
i++;
}
printf("\n\n");
printf("Press any key to search for an entry\n");
getch();
}
// **************************************************
//***************************************
void search_studentRecord( studentData details[], int numOfRec )
{
int low, high, mid;
char searchkey[10];
int flag = 0;
printf("Enter a Last Name to search for.\n");
scanf("%s", searchkey);
low = 0;
high = numOfRec;
while (low <= high) {
mid = (low+high)/2;
if (strcmp(searchkey, details[mid].lastName) < 0)
high = mid-1;
else if (strcmp(searchkey, details[mid].lastName) > 0)
low = mid + 1;
else if (strcmp(searchkey, details[mid].lastName) == 0) // found match
{
system("cls");
printf("Student I.D: %s\nPhone Number: %s\n", details[mid].studentNum, details[mid].phoneNum);
flag = 1;
break;
}
}
if (!flag) printf("Search Key not found\n");
}
//***********************************************************************************************
/*int delete_studentRecord(studentData details[], int NumOfRec)
{
int i;
int x;//variable to hold the value of the student number to delete
int max = sizeof(details);
details[max] = 0;//Mark the last record by 0.
printf("Enter a student number of the record you would like to delete:\n");
scanf("&s", x);
for (i = 0; i < max; i++) if (details[i].studentNum == x) break;
if (i == max) return -1; // The record not found.
for ( ; i < max; i++) details[i] = details[i + 1];
max--;
return 0; // Successfully deleted record.
}
*/