Subject: Programming assignment.
Posted By: Neeko Post Date: 12/14/2005 12:45:19 PM
Thank youall for helping. Assignment complete.

Reply By: Ankur_Verma Reply Date: 12/14/2005 1:19:23 PM
Nicolas, A few minutes ago another student, presumably, from your class only,
posted the same query, presenting it differenly.
This is the link to that post

http://p2p.wrox.com/topic.asp?TOPIC_ID=37617

May be, you two can help each other out.

Regards
Ankur Verma
Reply By: fadiyounes Reply Date: 12/14/2005 6:59:43 PM
yes but we both need help on this, weve tried helping each other with no luck. if anyone can help it would be much appreciated.

Reply By: Neeko Reply Date: 12/15/2005 1:56:41 PM
My collegue and I are having a few problems with mainly the first half of the assignment and must meet deadline soon. Any kind of help would be appreciated... Thank you for viewing... Nicolas.

Reply By: vector Reply Date: 12/15/2005 2:05:28 PM
Post ur code where u r having problems

Reply By: Neeko Reply Date: 12/15/2005 5:49:10 PM
The problem we are having is actually begining the assignment, we are not sure which enumerations or structs we need to apply to it, we have had difficulty understanding them, i will try to post what i have done so far tomorow as i do not have the information on my home P.C.
Thank you...

Reply By: Neeko Reply Date: 12/16/2005 9:51:33 AM
Hi again,

This is what I have done so far, still have a long way to go, but its a start, irequire any help possible... thank you.


#include <iostream>
#include <fstream>
#include <assert.h>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;

const int ACC_MAX = 5000 ;

enum MemberType { Annual, Quarterly = 12} ;

//enum Month { Jan, Feb, Mar, Apr, May, Jun,
//             Jul, Aug, Sep, Oct, Nov, Dec } ;

struct Book
{
  string Author;
  string Title;
};

struct Date
{
   int   day;
   int     month;
   int   year;
};

struct Name
{
    string first_name;
    string surname;
};


struct Address
{
    string        house_no;
    string        streetname;
    string        streettype;
    string        town;
};

struct Library_member
{
    Name        name;
    Address        address;
    MemberType    member;
    Date        date;
};


void process( Library_member a )
{
   switch( a.member )
   {
      case Quarterly:
         a.date.month += 1;
         break ;
      case Annual:
         a.date.year += 1;
         break ;
   }
}

void read_clients()
{
    ifstream clientfile( "clients.dat" ) ;

    Library_member member[ACC_MAX] ;
    int nRead = 0 ;
    //string text;
    int nMember;
    int MembershipNo;

    // read data from a file and store it in the array 'member'
    do
    {
        clientfile    >> member[nRead].name.first_name;
        clientfile    >> member[nRead].name.surname;
        clientfile    >> member[nRead].address.house_no;
        clientfile    >> member[nRead].address.streetname;
        clientfile    >> member[nRead].address.streettype;
        clientfile    >> member[nRead].address.town;
        clientfile    >> MembershipNo;
        clientfile    >> member[nRead].date.day;
        clientfile    >> member[nRead].date.month;
        clientfile    >> member[nRead].date.year;
        
        if ( MembershipNo == 12 )
            member[nRead].member = Quarterly;
        else
            member[nRead].member = Annual;

        nMember = nRead + 1;    //identifies the record number

        if ( clientfile )
        {            
            cout << nMember << " "
                    << member[nRead].name.first_name << " "
                    << member[nRead].name.surname << " "
                    << member[nRead].address.house_no << " "
                    << member[nRead].address.streetname << " "
                    << member[nRead].address.streettype << " "
                    << member[nRead].address.town << " "
                    << member[nRead].member << ". "
                    << member[nRead].date.day << " "
                    << member[nRead].date.month << " "
                    << member[nRead].date.year << " \n";
            nRead++ ;
        }
    } while ( clientfile && nRead < ACC_MAX ) ;

    cout << "\n";
    cout << nRead << " accounts read\n" ;

    clientfile.close() ;
};

void add_new_clients()
{
    ofstream clientfile( "clients.dat", ios::app ) ;

    Library_member member;
    int nRead = 0 ;
    //string text;
    string delimiter = "\t";
    //int nMember;
    int MembershipNo;
    
    cout << "Input new data client:\n\n";

    cout << "First Name: ";
    cin >> member.name.first_name;
    
    cout << "Surname: ";
    cin >> member.name.surname;

    cout << "House Number: ";
    cin >> member.address.house_no;

    cout << "Street Name: ";
    cin >> member.address.streetname;

    cout << "Street Type: ";
    cin >> member.address.streettype;

    cout << "Town: ";
    cin >> member.address.town;

    cout << "Member(0:Annual/12:Quarterly): ";
    cin >> MembershipNo;    //i use MembershipNo instead of member.member

    cout << "Day: ";
    cin >> member.date.day;

    cout << "Month: ";
    cin >> member.date.month;

    cout << "Year: ";
    cin >> member.date.year;
    
    // append new data in clients.dat
    clientfile << member.name.first_name << delimiter                
                    << member.name.surname << delimiter
                    << member.address.house_no << delimiter
                    << member.address.streetname << delimiter
                    << member.address.streettype << delimiter
                    << member.address.town << delimiter
                    << MembershipNo << delimiter
                    << member.date.day << delimiter
                    << member.date.month << delimiter
                    << member.date.year;

   clientfile.close() ;
};

void search_clients()
{
    Library_member member[ACC_MAX] ;
    int nRead = 0 ;
    int nFound = 0 ;
    int nMember;
    int MembershipNo;
    string name;

    cout << "Input client's first name or client's surname: ";
    cin >> name;
    
    cout << "\n";

    ifstream clientfile( "clients.dat" ) ;

    // read data from file and store in the array 'member'
    do
    {
        clientfile    >> member[nRead].name.first_name;
        clientfile    >> member[nRead].name.surname;
        clientfile    >> member[nRead].address.house_no;
        clientfile    >> member[nRead].address.streetname;
        clientfile    >> member[nRead].address.streettype;
        clientfile    >> member[nRead].address.town;
        clientfile    >> MembershipNo;
        clientfile    >> member[nRead].date.day;
        clientfile    >> member[nRead].date.month;
        clientfile    >> member[nRead].date.year;
        
        if ( MembershipNo == 12 )
            member[nRead].member = Quarterly;
        else
            member[nRead].member = Annual;

        nMember = nRead + 1;    //identifies the record number

        if ( clientfile )
        {    
            if ( name == member[nRead].name.first_name || name == member[nRead].name.surname)
            {
                cout << nMember << " "
                        << member[nRead].name.first_name << " "
                        << member[nRead].name.surname << " "
                        << member[nRead].address.house_no << " "
                        << member[nRead].address.streetname << " "
                        << member[nRead].address.streettype << " "
                        << member[nRead].address.town << " "
                        << member[nRead].member << ". "
                        << member[nRead].date.day << " "
                        << member[nRead].date.month << " "
                        << member[nRead].date.year << " \n";
                nFound++;
            }
            nRead++ ;
        }
    } while ( clientfile && nRead < ACC_MAX ) ;

    cout << "\n";
    cout << nFound << " records found\n" ;

    clientfile.close() ;
};

void search_book()

{
  struct books_t;
{
  string title;
  int year;
}
 
 int main ()
{
  string mystr;

  books_t abook;
  books_t * pbook;
  pbook = &abook;

  cout << "Enter title: ";
  getline (cin, pbook->title);
  cout << "Enter ISBN: ";
  getline (cin, mystr);
  (stringstream) mystr >> pbook->year;

  cout << "\nYou have entered:\n";
  cout << pbook->title;
  cout << " (" << pbook->year << ")\n";

  return 0;
}
}

int main()
{
    int menu_selection;

    cout    << "\n"
            << "================================================================\n"
            << "            Welcome to The public library system\n"           
            << "    Bellow is the main menu for the library filing system\n"  
            << "================================================================\n"
            << " \n"
            << " \n"
            << "Select 1: To view current clients\n"
            << "Select 2: To enrol new clients\n"
            << "Select 3: To perform a search through the data for\n"
            << "          clients details\n"
        << "Select 4: To search for a Book\n"
            << "Select 5: Exit this program\n\n";

    
    cin >> menu_selection;

    switch (menu_selection)
    {
    case 1:
        cout << "\n";
        read_clients();
        cout << "\n";
        main();
        break;
    case 2:
        cout << "\n";
        add_new_clients();
        cout << "\n";
        main();
        break;
    case 3:
        cout << "\n";
        search_clients();
        cout << "\n";
        main();
        break;
    case 4:
      cout << "\n";
      search_book();
      cout << "\n";
      main();
      break;
    case 5:
        break;
    }
}

any help would be appreciated, i still have to perform a search for a list of users i have been given and list a number of books they have rented out and other parts included in the assignment instructions above.

Any help would be much appreciated, thanks for your help...
Nicolas


Reply By: vector Reply Date: 12/16/2005 11:19:55 AM
Replace C headers with C++ one's

C headers                   C++ headers
-------------------------------------------
#include <assert.h>         #include <cassert>
#include <stdlib.h>         #include <cstdlib>
#include <time.h>           #include <ctime>


why in this world are u using two main() in the code








Reply By: Ankur_Verma Reply Date: 12/16/2005 11:40:58 AM
Nicolas, Quite a progress since your last post, I must say!!
It’s a “Programmer to Programmer” forum but at this point I don’t see any problem chipping in helping you out.

Looking at the code, however, I can say the program has a few problems
and must not be compiling error free.
1 Remove one of the two definitions of main, the first one is not making much sense
2 extra curly braces lying about like the one in search_book function.

For searching you need to structure your storage to make it more manageable.
One of the ways is to index the users and books entries based on their IDs an ISBNs respectively.
U can leave the user storage file and book storage files pretty much the way they are with a little change here there may be. Just make an index file atop for each of them and your searching problem will be solved. This design is clean, resource friendly, gives good performance and highly scalable.

Other way would be to go with the current design only, and load the whole file in some data structure and perform the search in that data structure only. Once the file is loaded, the program would perform reasonably well (based on the configuration of the comp, off course) but will take time to load itself. Not a clean design, scalability –not an issue, but is resource hungry.

And yes, your classmate can use some help, so help him.

Regards
Ankur Verma
Reply By: Neeko Reply Date: 12/16/2005 11:48:07 AM
Thanks for the tip, ive applied it to my program, but i also have to many compilation errors in which i am having trouble fixing, also I am having trouble completing the program as i do not know how to complete it.
Thanks for yuur advice and help...
Nicolas

Reply By: Neeko Reply Date: 12/16/2005 11:56:35 AM
P.S
I will help my collegue with the information provided, thanks for your advice, but as my knowledge of programming is quite weak, i am unable to write the rest of the program as i do not understand the c++ language thoughraly, i think i might just give up, Thank you all for your advice and help, it is much appreciated and has helped quite alot... look forward to keeping updated with this website as i know i can learn alot from here...

Thanks again...
Nicolas

Reply By: Ankur_Verma Reply Date: 12/16/2005 12:08:17 PM
The program would compile just do these three things

1 Delete or comment out the first main- including the curly brace following it

2 in the search book function, delete the curly brace after the line
   struct books_t;

3 replace
  int main()
    with
  void main()


Regards
Ankur Verma
Reply By: Neeko Reply Date: 12/16/2005 12:40:35 PM
I have made the adjustments advised and i am still recieving a compilation error. These are the errors i am recieving:

test1.cc:252: error: `main' must return `int'
test1.cc: In function `int main(...)':
test1.cc:255: error: `books_t' undeclared (first use this function)
test1.cc:255: error: (Each undeclared identifier is reported only once for each function it appears in.)
test1.cc:255: error: expected `;' before "abook"
test1.cc:256: error: `pbook' undeclared (first use this function)
test1.cc:257: error: `abook' undeclared (first use this function)
test1.cc:263: error: invalid use of undefined type `struct std::stringstream'
/usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/../../../../include/c++/3.4.3/iosfwd:84: error: declaration of `struct std::stringstream'
test1.cc: In function `int main()':
test1.cc:274: error: declaration of C function `int main()' conflicts with
test1.cc:252: error: previous declaration `int main(...)' here



Reply By: vector Reply Date: 12/16/2005 3:27:26 PM
replace int main with void main....what where did u learn that...was not expected from a moderator

Reply By: Ankur_Verma Reply Date: 12/17/2005 2:07:48 AM
The ANSI standards don’t require a program with main returning anything else other
than an int, including void, to fail.

But certainly, programs with main not returning an int are unsafe and their behavior
is unreliable should anything goes wrong.

'void main' however, certainly removes a few errors for a person who is not bothered
about what’s right and what’s wrong and just trying tooth and nail to get his program
to compile at all- so many books these days show program with main returning void, and
many compiler wont even flag a warning on that, but the fact remains that ‘void main’
is incorrect.

I gather that is what you were trying to say with your query -“where did u learn that”
and not asking me the name of the university I went to, right SunnyPal?

Thanks for pointing that out.

Well, when I see something I don’t agree with in a post, I just post my point of view on
that with what I think is the corrected version of the info in the post. I find that more
constructive. But that’s me.

People who are well informed, are always welcome to this forum.
If they could help others as well
 - better yet.

Regards
Ankur Verma
Reply By: Neeko Reply Date: 12/17/2005 4:19:17 PM
Thanks for your help guys... but after applying the what you told me, the program is still not compiling. My deadline is on monday and the only thing i can do now is submit the program how it is and hope for the best.
Once again your help was much apprecited.
Sorry for any inconvenience..

Regards
Nicolas.

Reply By: Ankur_Verma Reply Date: 12/18/2005 8:48:39 AM
You are welcome Nicolas, and I would say that your program didn’t have anything seriously wrong about it. The problems were more of those trivial syntactical typos that every beginner makes, a part of learning curve, and quite easy to get by with a little practice.

But let me tell you that if you have written this code yourself, you’ve done a pretty good job for a beginner and you deserve a pat on your back for that.

Regards
Ankur Verma

Go to topic 22296

Return to index page 415
Return to index page 414
Return to index page 413
Return to index page 412
Return to index page 411
Return to index page 410
Return to index page 409
Return to index page 408
Return to index page 407
Return to index page 406