1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C++ help please convert it into Linklist

Discussion in 'C, C++, C#' started by demon_kinght, Dec 11, 2009.

  1. demon_kinght

    demon_kinght BANNED BANNED

    Joined:
    Nov 10, 2009
    Messages:
    17
    Likes Received:
    3
    Hi please can someone Convert this program as it is into LINKLIST please i really need it urgent


    // Fig. 14.15: fig14_15.cpp
    // This program reads a random access file sequentially,
    // updates data already written to the file, creates new
    // data to be placed in the file, and deletes data
    // already in the file.
    #include <iostream>
    using std::cout;
    using std::cerr;
    using std::cin;
    using std::endl;
    using std::ios;
    #include <fstream>
    using std::eek:fstream;
    using std::eek:stream;
    using std::fstream;
    #include <iomanip>
    using std::setiosflags;
    using std::resetiosflags;
    using std::setw;
    using std::setprecision;
    #include <cstdlib>
    #include <conio.h>
    #include <stdio.h>
    struct PData {
    char name[40];
    int age;
    int serial;
    char date[15];
    char de[30];
    int room_no;
    };
    enum Choices { NEW = 1, SEARCH, DIS, DELETE, END };
    int enterChoice();
    void input(PData&);
    void print(PData);
    void newRecord(PData& );
    bool srch(PData&);
    bool del();
    void display();
    void main(void)
    {
    int choice;
    while ( ( choice = enterChoice() ) != END )
    {
    system("cls");
    switch ( choice )
    {
    case NEW:
    PData inp;
    input(inp);
    newRecord(inp);
    cout<<" following data has been entered \n";
    print(inp);
    _getch();

    break;
    case SEARCH:
    PData src;
    if(srch(src))
    print(src);
    else cerr<<"Record is not available";
    _getch();

    break;
    case DIS:
    display();

    break;
    case DELETE:
    if(del())
    cout<<" Record has been deleted \n";
    else cerr<<"some unknown errorin deleting record\n";
    _getch();

    break;
    default:
    cerr << "Incorrect choice\n";
    break;
    }
    }
    }
    int enterChoice()
    {
    cout << "\nEnter your choice" << endl
    << "1 - Add new record\n"
    << "2 - Search by name\n"
    << "3 - Diaplay all records\n"
    << "4 - delete an account\n"
    << "5 - end program\n? ";
    int menuChoice;
    cin >> menuChoice;
    return menuChoice;
    }
    void input(PData& input)
    {
    cout<<"Pls Enter the name of Patient\n";
    cin>>input.name;
    cout<<"Pls Enter the age of Patient\n";
    cin>>input.age;
    cout<<"Pls Enter the Date of admit\n";
    cin>>input.date;
    cout<<"Pls Enter the Serial number of Patient\n";
    cin>>input.serial;
    cout<<"Pls Enter the Disease of Patient\n";
    cin>>input.de;
    cout<<"Enterthe room no of patient\n";
    cin>>input.room_no;
    }
    void print(PData prnt)
    {
    cout<<"name of patient\t\t"<<prnt.name<<endl;
    cout<<"Age of patient\t\t"<<prnt.age<<endl;
    cout<<"Date\t\t\t"<<prnt.date<<endl;
    cout<<"Serial number\t\t"<<prnt.serial<<endl;
    cout<<"Disease of patient\t"<<prnt.de<<endl;
    cout<<"Room no of patient\t"<<prnt.room_no<<endl;
    }
    void newRecord( PData &client )
    {

    fstream inOut( "D:\\temp\\project\\rwp\\hello.txt", ios::in|ios::eek:ut);
    if ( !inOut )
    {
    cerr << "File could not be opened." << endl;
    exit ( 1 );

    }
    else
    {
    inOut.seekg(0,std::ios::end);
    inOut.write(reinterpret_cast<const char *>( &client ),sizeof( PData ) );
    inOut.close();
    }

    }
    bool srch(PData &src)
    {
    fstream inOut( "D:\\temp\\project\\rwp\\hello.txt", ios::in);
    if ( !inOut )
    {
    cerr << "File could not be opened." << endl;
    exit ( 1 );

    }
    else
    {
    char name[30];
    bool flag=false;
    cout<<"Pls Enter name to search";
    cin>>name;
    inOut.seekg(0,std::ios::beg);
    while(!inOut.eof())
    {
    inOut.read(reinterpret_cast<char *>( &src ),
    sizeof( PData ) );
    if(!strcmp(src.name,name))
    {flag=true; break;}

    }
    inOut.close();
    return flag;
    }

    }

    bool del()
    {
    fstream inOut( "D:\\temp\\project\\rwp\\hello.txt", ios::in|ios::eek:ut);
    if ( !inOut )
    {
    cerr << "File could not be opened." << endl;
    exit ( 1 );

    }
    else
    {
    char name[30];
    PData src;
    bool flag=false;
    cout<<"Plsear enter name 4 delete data";
    cin>>name;
    inOut.seekg(0,std::ios::beg);
    long loc;
    while(!inOut.eof())
    {
    loc=inOut.tellg();
    inOut.read(reinterpret_cast<char *>( &src ),
    sizeof( PData ) );
    if(!strcmp(src.name,name))
    {flag=true; break;}

    }
    if(flag)
    {
    inOut.seekg(loc,std::ios::beg);
    strcpy(src.name," ");
    strcpy(src.date,"-");
    src.serial=-1;
    src.age=0;
    strcpy(src.de,"-");
    src.room_no=-1;
    inOut.write(reinterpret_cast<const char *>( &src ),sizeof( PData ) );

    }
    inOut.close();
    return flag;
    }


    }

    void display()
    {
    fstream inOut( "D:\\temp\\project\\rwp\\hello.txt", ios::in);
    if ( !inOut )
    {
    cerr << "File could not be opened." << endl;
    exit ( 1 );

    }
    else
    {

    PData src;
    inOut.seekg(0,std::ios::beg);
    while(!inOut.eof())
    {
    inOut.read(reinterpret_cast<char *>( &src ),
    sizeof( PData ) );
    if(src.serial != -1)
    print(src); _getch();

    }
    inOut.close();

    }

    }
     
  2. demon_kinght

    demon_kinght BANNED BANNED

    Joined:
    Nov 10, 2009
    Messages:
    17
    Likes Received:
    3
    No one can help me ?Please help me
     
  3. BozoClown

    BozoClown Junior Member

    Joined:
    Jan 4, 2009
    Messages:
    150
    Likes Received:
    106
    You aren't making it easy for anyone to help you. The code is not sufficiently commented, no indentation hence not readable. That means anyone who would want to help would have to painstakingly figure out the inner details of the program plus think about what information you want in the linked-list only after then would they be able to touch your code(however basic the task it accomplishes).

    Plus
    Instead of multiple "using" statement you could just do
    Code:
    using namespace std;
    
     
  4. demon_kinght

    demon_kinght BANNED BANNED

    Joined:
    Nov 10, 2009
    Messages:
    17
    Likes Received:
    3
    hi i want to use the same functions and data structure and convert this program into LINKLIST
     
  5. seeplusplus

    seeplusplus Power Member

    Joined:
    Aug 18, 2008
    Messages:
    511
    Likes Received:
    163
    Posting the code so it's readable would be a start.

    Without you making any effort I doubt anyone will help you, unless you can offer something in return.

    It's not to do what you want.
     
  6. sikx

    sikx Registered Member

    Joined:
    Jan 4, 2009
    Messages:
    65
    Likes Received:
    166
    Location:
    Germany
    Home Page:
    Do you mean you want a std::list?
     
  7. demon_kinght

    demon_kinght BANNED BANNED

    Joined:
    Nov 10, 2009
    Messages:
    17
    Likes Received:
    3
    Nah thanx for the help i figured it out