Thread: Ok guys, I need help with 1 final program. PLEASE help!

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    38

    Ok guys, I need help with 1 final program. PLEASE help!

    Ok hopefully I wont get flamed or nothing for asking for help. Im sure you all saw my Pig Latin program (Thanks for the help Salem) and my recent one about factors. Well I got this one big final program to do. Here are the details:

    Code:
    FAMILY DATA
    Design 7: write the algorithm and draw a picture of your structure
    
    Write a C program that assigns family information to structures:  
    	struct family      {
    				char name [50]; 		/* person's name */
    				char street [50]; 		/* street address */ 
    				char csz [50];		/* city, state, zip */
    				char relation [30];		/* relation to you */
    				char birthday [20];		/* mm-dd-yy */
    			         };
    	struct family people[5];
    or more if you have more immediate family members.
    Have 5 members (include yourself; make someone up if you have to).
    Read in the data from the file   a:\family.in   Create it in notepad to look like:
    	Joe Redfield
    	6220 Culebra
    	San Antonio, TX 78228
    	husband
    	10-03-56					(and so on for each member) 
    
    Output for each person the following information to the screen and to the file   a:\family.out   formatted as below:
    	Carol L. Redfield				Relation: 	self
    	609 Ridge View				Birthday:  	July 19, 1958
    	San Antonio, TX 78253			Age:        	36
    Now I understand the basic concept of it and how to "read" or "write" infomation to a specific place. But in class all we did as examples were numbers. I was practicing today and I cant do sentences at all. I just dont get it. I know I gotta write all my 5 family members information to a single Notepad but I dont get how the program will get individual information for the individual people.

    I really, really hope someone can help me. Now Im not trying to get people to do my work for me. But I need some help with this. I already read about *pointers and it didnt help much at all. I hope someone can help. Even if its a simple little comment. Everything here is vital.

    Please help. It would be very much appreciated.

    Thanks
    Last edited by Desperado; 11-29-2001 at 07:26 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    38

    Well....

    I actually figured out how to do sentences. For some odd reason I gotta have something in there thats: "feof" I was looking around and I dont know what the hell that is. I looked in my notes and everything. It was in one of the examples I did and it made my program work, so for some reason I have to have it in order to get an output of a sentence.

    Well thats progress. Hope theres some people here who can help out.

    Thanks again.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    53

    feof

    Feof tests for the end of file (eof) on a file stream.

    RESULT = feof( STREAM_NAME );

    RESULT is 0 if not at end and 1 if at end.


    Almosthere

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    38
    Ahhh. Thanks for the info man. Its appreciated. Ill be working on this all night (what a way to spend Friday night huh?) so if youor anyone else has any more info, please post it! Thanks

  5. #5
    Unregistered
    Guest
    use sizeof(family) in your expressions to move from one record to another in the file. I'm not sure how much C differs from C++, so can't get too precise for you. I just did a similar program with fewer fields in the struct, but still all char[]'s. It'll initialize, enter, update, delete and print to screen, but is in C++, not C. If you have a specific question, post that, I'll try to help.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    38

    Thanks man, its very cool of you.

    Well Ive just been practicing things to try and get things started. I have a few basic questions really.

    If I save the individual family information (all 5 people) to one notepad, how will C know what info to get for each individual person? Is there a special way I have to label them or something?

    Thanks for your help man. I cant say how much I aprreciate your help. Everyones help really.

    Its appreciated.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    Here's how I initialized 100 records of struct person.
    Code:
    struct person
    {
    	char lastName[15];
    	char firstName[15];
    	char age[4];
    };
    .
    .
    .
    initialize("nameAge.dat");
    .
    .
    .
    void initialize(const string& filename)
    {
    	ofstream personOut(filename.c_str(), ios::binary);	
    	if (!personOut)
    	{
    		cerr << "\nCan't open output file \"" << filename << "\"" << endl;
    		exit(EXIT_FAILURE);
    	}
    // id is an instance of struct person, initialized to values in brackets
    	person id = {"unassigned", "", "0"}; 
    	for (int i = 0; i < 100; i++)
    		personOut.write((char *)&id, sizeof(person));
    }
    This writes records to a file. Here's code that reads valid records to the screen. If you want all records printed, ignore the if test. This isn't formatted, but you should be able to do that.
    Code:
    void printName(const string& filename)
    {
    	ifstream personPrint(filename.c_str());
    	person blank;
    	int k = 1;
    	personPrint.read((char *)&blank, sizeof(person));
    	while (!personPrint.eof())
        {
    		if (!strcmp(blank.lastName, "unassigned\0") == 0)
            cout << k << ": " << blank.lastName  <<  " "<< blank.firstName << " "<< blank.age << endl;
    		personPrint.read((char *)&blank, sizeof(person));
    		k++;
        }
    Hope that helps some.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    38

    Unhappy God, this is hard....

    Ok, get this,My cousin and I spent from 8:00 PM last night to 6:00 AM this morning! This sucks. WE cant get crap to wrok. I looked at your guys suggestions and could only do so much. Heres my program below so hopefully you guys can critique it and help me out. Its really appreciated.

    Code:
    /* C program by C.Redfield for CS1410
     * show an example of using structures
     */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
     struct family {	    
                    char name [50]; 		/* person's name */
                    char street [50]; 		/* street address */ 
                    char csz [50];		        /* city, state, zip */
                    char relation [50];		/* relation to you */
                    char birthday [50];		/* mm-dd-yy */
                    char age [50];                  /*person's age */
                   };
    
    struct family PEOPLE[5];
    struct family *field;
      
    void main (void)
    {
    char linein[50];  
    
      FILE *in, *out;
    
      in = fopen ("a:\\family.in", "r");
    /*  out = fopen ("a:\\family.out", "w"); */
      while ( !feof(in) )
         {
         fgets(linein, 50, in);
         
         printf("the line: %s", linein);
         }
    
    /*  fgets(buffer, sizeof(buffer), in);  */
    /*	sscanf(buffer, "%s %s %s %s %s %s"), *member->name, *member->street, *member->csz, *member->relation, *member->birthday, *member->age);*/
    
    /* printf("%s\tRelation: %s\n%s\tBirthday: %s\n%s\tAge: %s\n\n",
           *member->name, *member->street, *member->csz, *member->relation, *member->birthday, *member->age); */
     fclose(in);
     fclose(out);
    
    }
    Last edited by Desperado; 12-02-2001 at 07:18 PM.

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    I don't know enough C to help you, but it looks like half your program is commented out. I also don't see where you use the two structs you declared, PEOPLE and *field. Where does *member come from? Maybe that's C stuff.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Grading Program Error
    By eun-jin in forum C++ Programming
    Replies: 9
    Last Post: 05-09-2006, 07:45 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. Replies: 1
    Last Post: 01-24-2005, 02:07 PM
  4. Need Delimiter Program helpful hints.
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 02-16-2002, 06:27 PM