Thread: Need Help with syntax and overall problems!

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    34

    Thumbs down Need Help with syntax and overall problems!

    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. 
    } 
    
    */

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Here's some comments to help you get started:

    Code:
    void menu (void)
    {
       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)
          {
             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
          {
             printf("Invalid input\n");
          }
          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);
       }
    }
    - you are scanning in integers. Therefore the condition
    while(choice != '0') will never be met. See code above for the correct version.

    - After running through your menu one time, you never prompt the user for another choice.

    - You called read_studentrecord twice. Also you returned the number of records read in. However, it'll be easier to keep a global count everytime you enter a new record. You can also use this value to see if it went over the MAX_STUDENTS value. You also need to decrement this count everytime you delete a record.

    - A simple way to delete records is to have a flag in your structure telling you if the data is valid or not. This way you can set it to 0 to show that it is deleted. You need to make minor modifcations to your other functions to handle this. For example, when you sort the records, do not swap with the ones that are deleted. Do not print records that are deleted. Quite simple.

    This should get you back on track. You're almost there.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

Popular pages Recent additions subscribe to a feed