?
Solved

program problem

Posted on 2004-11-20
3
Medium Priority
?
274 Views
Last Modified: 2010-04-15
Here is the database:

32
Name SID Phone Grade
dick,arrea 00001 301-888-9000 90
lawrence,aaron 00002 333-111-2222 85
allen,abbey 01238 353-680-7559 78
alexander,abbi 19086 396-999-6670 99
allison,abe 99534 395-585-9950 50
milk,abigail 89745 301-879-2204 32
aro,absalom 99134 333-124-9978 45
smith,acacia 03021 456-456-3354 89
jackson,crawford 12456 890-909-5432 0
frank,cree 99324 997-376-7445 11
conrad,creghton 87095 898-980-7865 33
cressidia 88451 958-907-2007 1
crystal,crispian 04656 889-995-9023 2
skycaptain,curt 90746 907-989-8876 3
google,curtis 89745 917-990-9917 99
stewart,cuthbert 90775 996-913-9765 99
taylor,daley 89756 145-225-2012 90
kok,ebony 99134 301-888-9988 78
stanley,ed 49321 777-357-9910 54
lee,gae 23451 790-758-8090 4
lee,gaenor 12355 780-905-7918 23
glenn,korey 59478 765-117-8891 5
gilbert,kori 88902 759-896-9928 60
a,kurtis 09234 898-750-9875 61
gordon,kyler 13457 909-987-7890 64
b,kym 89075 123-345-8876 65
neo,megan 09156 112-775-3346 77
neil,meghan 99017 257-434-2254 2
grace,mel 89744 907-879-9912 99
curtis,toni 09114 890-905-6789 97
newton,topaz 99165 991-663-4678 15
oliver,zula 78451 898-224-1544 40

This the program that I have started:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "p3FileRead.h"

#define NUM_FIELDS 4
#define MAX_FIELD_LENGTH 20

typedef struct{
  char name[20], phone[12];
  int SID, grade;
}Student_t;


int NUM_STUDENTS ;
int FILE_SIZE ;
int choice;



int main()
{
    char* file ;
 file = enee114_readfile() ; //read inputfile
                                // you may use file as a character array
                                // of length FILE_SIZE.
    printf("%s", file) ;

  //***********************************************************
  NUM_STUDENTS = atoi(file) ;
  FILE_SIZE = strlen(file) ;

  printf("There are %d student. The filesize is %d bytes\n",
         NUM_STUDENTS, FILE_SIZE) ;

  Student_t **StudentList = NULL; //Array of student records
  int count = 0, i= 24, j = 0;    //Number of records in the StudentList array
  char arr[21];

  //scanf("%d",&NUM_STUDENTS);
  while(file[i] != ' ')
  printf("%c", file[i++]);

  printf(" ***Main*** \n ");   //print main menu
  printf("1. Search \n");
  printf("2. Sort\n");
  printf("3. Advance Search\n");
  printf("4. Exit\n");

  scanf("%d", & choice);
  if(choice == 1)
    {
    printf(" ***Search***"\n "); // print search menu
    printf(" 1.Name  \n");
    printf(" 2.SID \n");
    printf(" 3.Phone \n");
    printf(" 4.Grade \n");
    printf(" 5.Exit \n");
      scanf("%d", & choice);
      if(choice == 1)
        {
          for(int i= 0; i<FILE_SIZE; i++)
            {
              printf("%c", file[i]);
            }
        }
    }
 return 0 ;
}





 This program  is suppost  to allow the user to be be able to search, sort or advance search in the database by either name SID, phone # or grade. It is a good start I guess. I tried to run part of the first have but it is not working properly. Is there any suggestion that somebody can give me so I can fix this problem and be able to continue on it with no problem??

0
Comment
Question by:Ofakile1
3 Comments
 
LVL 11

Expert Comment

by:griessh
ID: 12636245
Hi Ofakile1,
> but it is not working properly
What is not working?

Your fist problem seems to be in:
 char* file ;
 file = enee114_readfile() ; //read inputfile
 printf("%s", file) ;
 NUM_STUDENTS = atoi(file) ;
 FILE_SIZE = strlen(file) ;


What does enee114_readfile() do? Does it allocate for a char array and return the pointer of that char array?
What do you expect  printf("%s", file) does?
Why do you think you can convert a file descriptor to an int?
Why do you think that 'file' is a string?

=====
Werner
0
 
LVL 3

Expert Comment

by:baboo_
ID: 12637009
Hey there -

Post the code (or preferably just the method signature and description) for enee114_readfile()  and we can answer your question.

baboo_
0
 

Accepted Solution

by:
charukalanidhi earned 1000 total points
ID: 12663376
Hi there,

If the database is in a file, i really dont understand how you are trying to access the file. As far as i know, i can give a small code which will work for your first problem.

suppose the database is present in a file called "details.txt". this is how i would end up programing.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* Make sure you enter the structure in the same order as the data in the database */

typedef struct{
  char name[20];
  int SID;
  char phone[12];
  int grade;
}Student_t;



main()
{
      FILE *fp;
      int choice,serch_id;
      char search_string;
      
      Student_t namelist;
      
      if((fp=fopen("details.txt","r")) ==NULL)
      {
            puts("cannot open file");
            exit();
      }

      printf(" ***Search***"\n "); // print search menu
      printf(" 1.Name  \n");
      printf(" 2.SID \n");
      printf(" 3.Phone \n");
      printf(" 4.Grade \n");
      printf(" 5.Exit \n");
      scanf("%d", &choice);
      
      switch(choice)
      {
      case 1:
      {
            printf("Enter the name:");
            scanf("%s",search_string);
            printf("The details of the person you searched for are:\n");
            while(fread(&namelist,sizeof(Student_t),1,fp) ==1)
            {
                  if(strcmp(namelist.name,search_string)==0)
                  {
                        printf("%s,%d,%s,%d\n ",namelist.name, namelist.SID, namelist.phone,namelist.grade);
                  }
            }
      }
      break;
      case2:
      {
            printf("Enter the SID:");
            scanf("%d",search_id);
            printf("The details of the person you searched for are:\n");
            while(fread(&namelist,sizeof(Student_t),1,fp) ==1)
            {
                  if(namelist.SID == search_id)
                  {

                        printf("%s,%d,%s,%d\n ",namelist.name, namelist.SID, namelist.phone,namelist.grade);
                  }
            }

      }
      break;
      
      case 3:
      {
            printf("Enter the phone:");
            scanf("%s",search_string);
            printf("The details of the person you searched for are:\n");
            while(fread(&namelist,sizeof(Student_t),1,fp) ==1)
            {
                  if(strcmp(namelist.phone,search_string)==0)
                  {
                        printf("%s,%d,%s,%d\n ",namelist.name, namelist.SID, namelist.phone,namelist.grade);                        
                  }
            }
      }
      break;
      case4:
      {
            printf("Enter the grade:");
            scanf("%d",search_id);
            printf("The details of the person you searched for are:\n");
            while(fread(&namelist,sizeof(Student_t),1,fp) ==1)
            {
                  if(namelist.grade == search_id)
                  {
                              printf("%s,%d,%s,%d\n ",namelist.name, namelist.SID, namelist.phone,namelist.grade);            
                  }
            }

      }
      break;
      default:
            fclose(fp);
            exit();
      }
}


Hope this helps with what you want.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question