Click here to Skip to main content
15,896,727 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi everybody.
I got an assignment which specifies that i have to read data from a text file and write in it, insert data in the file by using linked list: insert at beginning, middle and end of the file.

Also I must delete a record and sort using bubble sort and searching.
I have written a piece of code but i have hit a wall and I don't know what to do...
I don't want to lose marks...

Any help please?

The code is as follows:


#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//function prototype
int add_records();
int get_records();
int user_choice();
int search_records();
string buffer;
//structure
struct tabledata//creating linked list
{
int stud_id;//will store information
char name[100];//will store information
float code;//will store information
tabledata *next;//the reference to the next node
};
typedef struct tabledata node;
node *head,*current,*temp;

int main(int argc, char *argv[])
{
    cout<<"\t\t*************************************************"<<endl;
    cout<<"\t\t\tStructured Programming Assignment"<<endl;
    cout<<"\t\t*************************************************\n\n\n"<<endl;
    
    cout<<"\t\tStudent Name: Ashvind Anand MITTOO\n";
    cout<<"\t\tStudent ID: 101384\n";
    cout<<"\t\tCohort ID : BCNS/10B/FT"<<endl;
    cout<<"\t\tProgramme : BSc(Hons.) Computer Science with Network Security"<<endl;
    
    cout<<"\n\n\t\tQuestion 2\n"<<endl;
  
    system("PAUSE");
    system("CLS");
    
    //calling the functions to main
    user_choice();
    add_records();
    get_records();
}
//Function user_choice
int user_choice()
{
    char user;
    
    system("CLS");
    
    cout<<"Do you want to add/search/exit : ";
         cin>>user;
         if ((user=='A') || (user=='a'))
        {
            get_records();
            add_records();
        }
        else if ((user=='e') || (user=='E'))
        {
             exit(1);
         }
        else if ((user=='S') || (user=='s')) 
        {
             int search_records();
         }
         else
         {
             cout<<"Invalid input!\n";
             system("PAUSE");
             system("CLS");
             user_choice();
         }
}
//function to add at the end of list
int add_records()
{
    int stud_id;
    char name[100];
    float code;
    char user;
    int counter=0;
    head=NULL;
    
    //opening file datatable for writing
    ofstream  fin;
    fin.open("tabledata.txt",ios::app);
    
     while(1)
     {
         cout<<"Do you want to enter a Data [Y/N/E]: ";
         cin>>user;
         if ((user=='N') || (user=='n'))
        {
          //break out of the loop...
           break;
        }
        else if ((user=='E') || (user=='e'))
        {
             user_choice();
         }
        else if ((user=='Y') || (user=='y')) 
         {
    current=new node;
    
    cout<<"\n\tEnter ID: "; 
    cin>>stud_id;
    current->stud_id=stud_id;
    
    
    cout<<"\tName: "; 
    //pause for user to enter Name
     cin.ignore(1,'\n');
     cin.getline(name,100,'\n');
     
     //insert into linked list
     strcpy(current->name,name);
     
     cout<<"\tCode: ";
     cin>>code;
     current->code=code;
     
     //saving to file tabledata.txt
     fin<<"\n "<<stud_id<<" "<<name<<"\t"<<code;
  
     //incrementing linked list and linking nodes
     current->next=head;
     head=current;
       }
     
     else
         {
          cout<<"\nInvalid input. Only Y/N/E allowed.Re-enter your choice:\n";
         }
     }  
     fin.close();
     get_records();
  
}
//function to get records from tabledata.txt
int get_records()
{
int stud_id;//declaration
string name;//declaration
float code;//declaration
    ifstream fin;//open output file tabledata.txt for reading
    fin.open("tabledata.txt");
   //Printing Header for Table 
    cout<<"\nID\tName\t\tCode\n";
          cout<<"-------------------------------\n";
          
    if(!fin)
    {
       cout<<"Can't open tabledata.txt file";
       exit(1);        
       }
       
       else
       {
      while(!fin.eof())
        {
          fin>>stud_id>>name>>code;
          cout<<stud_id<<"\t"<<name<<"\t\t"<<code<<"\n\n";
          }
           
          /*while(head != NULL)
          {
            cout<<head->stud_id<<"\t"<<head->name<<"\t\t"<<head->code<<"\n\n";
            head=head->next;          
            }*/
            
        }
        
        fin.close();
}


[edit] code block + readability + retagged [/edit]

The OP added as a solution:

"well inserting at the end is working fine and it is saving in the text file too. the wall i refering to is that i don't know how to insert at the beginning,middle and end of the list. and also how to delete a record from this list????"
Posted
Updated 11-May-11 5:50am
v3
Comments
OriginalGriff 11-May-11 11:36am    
So what is working, and what isn't?
Joan M 11-May-11 11:36am    
Which is that wall? What is happening? Why the code has only 4 functions?
Joan M 11-May-11 11:40am    
It is a little bit too much to call system (deprecated nowadays) to pause the program flow and to clear the screen... You could do it easily using getchar() (in order to pause) and clrscr() in order to clear the screen...
OriginalGriff 11-May-11 11:50am    
The OP posted as a solution:
"well inserting at the end is working fine and it is saving in the text file too. the wall i refering to is that i don't know how to insert at the beginning,middle and end of the list. and also how to delete a record from this list?"
Ashvind.M 11-May-11 11:52am    
well inserting at the end is working fine and it is saving in the text file too. the wall i refering to is that i don't know how to insert at the beginning and middle of the list. and also how to delete a record from this list???? well i'm sorry to use too much to call system but my leturer wants it that way...really sorry for that...;)

1 solution

Inserting in the beginning and middle of the list are quite easy:
Begining:
1) Create your new element.
2) Set the "Next" pointer to the current "head" value.
3) Set the "head" to point to the new element.

Middle:
1) Create your new element.
2) Find the element you want to insert after (call it "middle")
3) Set the new element "next" to the current value of "middle" "next"
4) Set "middle" "next" to point to your new element.

Deleting is even easier:
1) Find the element before the one you want to delete. ("previous")
2) Find the element you want to delete. ("delete")
3) Set the "next" pointer of "previous" to teh value in the "delete" "next" pointer.
4) Deallocate "delete" to prevent memory leaks.
 
Share this answer
 
Comments
Ashvind.M 11-May-11 12:08pm    
hmm will try to make it in code..thnx for helping..;)
OriginalGriff 11-May-11 14:21pm    
You are welcome! It's not difficult code - only a few lines each - but I didn't want to give you everything: it's your homework and you need to learn this kind of stuff! :laugh:
Ashvind.M 11-May-11 15:18pm    
i need an advice from you. in the int get_records function at the end of the program, do you think i should use
"getline(fin,buffer,'\t');
stud_id=(int)atof(buffer.c_str());"
to read from the txt file???
OriginalGriff 11-May-11 15:27pm    
Without actually trying it I can't tell off the top of my head - and I don't have time to create a new C++ project at the moment - please raise this as a new question, since it is unrelated to the old one! :)
Ashvind.M 11-May-11 16:03pm    
okk no problem...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900