Solved

c++ question Linked list

Posted on 2011-09-03
9
453 Views
Last Modified: 2012-05-12
How do i add a function to delete a record? i have got the program to run correctly but cannot add a function to delete the records...in my list thank you very much.
THIS IS WHAT I HAVE SO FAR..

#include <iostream>
#include<iomanip>
#include <cstdlib>
#include <string>

  using namespace std;

//Header File


//declare structure types 
	struct AppointmenRecord{ 
		string name;
		string description;
                string day;
		double date;
		double time;

	       };

const int listSize = 5;    //number of records in array
int currentSize = 0;
//declare a list of records.
	AppointmenRecord AppointmenList[listSize];     //global array of records


//Funtion Body
  
void addAppointmenRecords();
void displayAllRecords();
void delAppointmen();
void displayMenu(int &option);


 
void main( )
{
    int option; 
    bool endOfSession = false;
//get user action choice
   while (!endOfSession)
   {	
       displayMenu(option);

switch (option)
       {   
         case 1: addAppointmenRecords( );
                	 break;
         
         case 2: delAppointmen();
                	 break;

         case 3: displayAllRecords( );
                	 break;

         case 4: system("cls");
                 	 cout<<"\nEND OF SESSION\n\n";
                	 endOfSession = true;
                	 break;

         default: cout<<endl<<"Wrong option number!! Try again\n";
       }
   }
}
void delAppointmen()
{

}

void displayMenu(int &option)
{ // display menu options and choose one 

     system("cls");        //clear screen
                 
    cout<<endl<<endl;
    cout<<"\t    * APPOINTMENTS DATA MENU *"<<endl<<endl;
    cout<<"\t  1. Add An Appointmen record(s)"<<endl;
    cout<<"\t  1. Add Day Mon,Tue,Wed,Thur,Fri" <<endl;
    cout<<"\t  2. Display All Appointmens records"<<endl;
    cout<<"\t  3. Edit An Appointmen record(s)"<<endl;
    cout<<"\t  4. End Session"<<endl;
    cout<<endl<<setw(28)<<"Enter option number: ";

    cin>>option;
    cin.get();
}


//Client Code
 
void addAppointmenRecords()
{  
        int number, i;

system("cls");         //clear screen

	cout<<"\nHow many Appointmen you wish to add? ";
	cin>>number;
	cin.get();                         //read newline character left in the buffer

	if( (number + currentSize ) <= listSize)                   //There is still room in the array	
	      for( i = 1; i<=number; i++)
                         {
			cout<<"\nEnter the date: ";
			cin >>AppointmenList[currentSize].date;
			cout<<"Enter Appointmen Descriptions: ";
			cin>>AppointmenList[currentSize].description;
			cout<<"Enter Appointmen date: ";
            cin>>AppointmenList[currentSize].date;
			cout<<"Enter Appointmen time: ";
            cin>>AppointmenList[currentSize].date;
			cout<<endl;

	  		 cin.get();          //read newline character left in the buffer
			currentSize += 1;
		}
	else
cout<<"Overflow!!!! Appointmen List is full"<<endl;
         

cout<<"\nPress any key to continue"<<endl;
cin.get();     //read a character 
}


void displayHeading()
{      
           cout<<setiosflags(ios::left);                //left justify output
			cout<<endl<<setw(20)<<"Name"
               <<setw(20)<< "Descriptions"
               <<setw(12)<<"Date"
			   <<setw(15)<<"Time"<<endl;
}

 
void displayAllRecords()
{   /* print the data from the array of records under a suitable header*/
    
      int index;

       system("cls");
       displayHeading();
       cout<<setiosflags(ios::left);                //left justify output

       for (index = 0; index < currentSize; index++)
        {
	cout<<setw(20)<<AppointmenList[index].name;
	cout<<setw(15)<<AppointmenList[index].description;
	cout<<setw(12)<<AppointmenList[index].date;
	cout<<setw(12)<<AppointmenList[index].time<<endl;
        }
   //give the user a chance to read the output data
  	cout<<endl<<"Press any character to continue  ";
	cin.get();          //read entered character      
}

Open in new window

0
Comment
Question by:businessesatoz
  • 4
  • 2
  • 2
  • +1
9 Comments
 

Author Comment

by:businessesatoz
ID: 36479983
my question is how to i add a function to delete? i have a function already setup  
void delAppointmen()
{

}

Open in new window

  but i'm not sure what to put in their to delete ? thank you for any help.
0
 
LVL 8

Expert Comment

by:Andrei Fomitchev
ID: 36480136
Option 1. You can add a flag "deleted" in your structure and instead or real "delete" just set deleted = true.
In other places you should show the record if deleted is false.

Option 2. Use TList<AppointmenRecord> instead of array - it has "delete" method.

Option 3. in addition to listSize add numberOfRecords and use it to show existing records.
to delete - decrease it by 1 and copy
- last record over deleted record
- or copy all record after deleted to the previous one to save the order.
0
 

Author Comment

by:businessesatoz
ID: 36480519
hello thank you for the feedback... can you help me with the code... ? that works with what i wrote so i can see what you did and learn? thank you very much! :)
0
 
LVL 8

Accepted Solution

by:
eager earned 334 total points
ID: 36480881
I don't see a linked list in your code.  You are storing each record in an element of the AppointmenList array.

If you want to use linked lists, read Wikipedia http://en.wikipedia.org/wiki/Linked_list or any C++ text book.  
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 8

Expert Comment

by:Andrei Fomitchev
ID: 36481010
option 3.1. delete 2nd record; 2<currentSize-1 :

AppointmenList[2].name = AppointmenList[currentSize-1].name;
AppointmenList[2].description = AppointmenList[currentSize-1].description;
AppointmenList[2].day = AppointmenList[currentSize-1].day;
AppointmenList[2].date = AppointmenList[currentSize-1].date;
AppointmenList[2].time = AppointmenList[currentSize-1].time;
currentSize--;

to delete the record "currentSize-1" (the last one) just:
currentSize--;
0
 

Author Comment

by:businessesatoz
ID: 36481664
Thanks, i realized i'm not using linked list.. i have fixed up my program now it's using linked list.

/*Name: Patel Dharti
ID: xxxxx
CSCI528: Assignment #2
COMPILER: BloodShed C++ Version 3.0

Purpose : The purpose of this program is to make a simple schedule program which can control appointment and cancel days. To simplify programming, you have only five days, Monday, Tuesday, Wednesday, Thursday, and Friday. Here we use linked list programming method to complete the program.

In this program First, you use linked list programming method, and then make a user enter list screen in DOS command asking three choices, 1: to insert an arranged day, 2: to delete an arranged day, and 3: to display all arranged day(s).
*/


#include <iostream>
#include<iomanip>
#include <cstdlib>
#include <string>

using namespace std;

//Header File


//declare structure types 
	struct AppointmentRecord{ 
		//string name;
		//string description;
        string day;
        AppointmentRecord *next;
		//double date;
		//double time;
	       };

const int listSize = 5;    //number of records in array
int currentSize = 0;
//declare a list of records.
	AppointmentRecord AppointmenList[listSize];     //global array of records

AppointmentRecord *first;
//Funtion Body
  
void addAppointmentRecords( );
void displayAllRecords( );
void delAppointment( );
void displayMenu(int &option);
 
main()
{
    int option; 
    bool endOfSession = false;

//get user action choice
   while (!endOfSession)
   {	
       displayMenu(option);

switch (option)
       {   
         case 1: addAppointmentRecords();
                	 break;
         
         case 2: displayAllRecords();
                	 break;

         case 3: delAppointment();
                	 break;

         case 4: system("cls");
                 	 cout<<"\nEND OF SESSION\n\n";
                	 endOfSession = true;
                	 break;

         default: cout<<endl<<"Wrong option number!! Try again\n";
       }
   }
}

void displayMenu(int &option)
{ // display menu options and choose one 

     system("cls");        //clear screen
                 
    cout<<endl<<endl;
    cout<<"\t    * APPOINTMENTS DATA MENU *"<<endl<<endl;
//    cout<<"\t  1. Add Appointment Record(s)"<<endl;
    cout<<"\t  1. Add Appointment Day \"Monday,Tuesday,Wednesday,Thursday,Friday\""<<endl;
    cout<<"\t  2. Display All Appointment Records"<<endl;
    cout<<"\t  3. Delete an Appointment Record"<<endl;
    cout<<"\t  4. End Session"<<endl;
    cout<<endl<<setw(28)<<"Please enter your option : ";

    cin>>option;
    cin.get();
}

//Client Code
 
void addAppointmentRecords()
{  
        int number, i;

        system("cls");         //clear screen

//	cout<<"\nHow many Appointments you wish to add? ";
//	cin>>number;
//	cin.get();                //read newline character left in the buffer
    AppointmentRecord *node = new AppointmentRecord();
    
//	if((number + currentSize) <= listSize)           //There is still room in the array	
//	{
//    for( i=0; i<number; i++)
//       {
            //cout<<"\nEnter the name: ";
			//cin>>AppointmenList[currentSize].name;
			//cout<<"\nEnter the description: ";
			//cin>>AppointmenList[currentSize].description;
			                                             //getline(cin, AppointmenList[currentSize].date);
			cout<<"Enter Appointment Day: ";
			cin>>node->day;
			node->next = NULL;
			
			if(first == NULL)
			{
                first = node;
            }
            else
            {
                AppointmentRecord *temp;
                temp = first;
                while (temp->next != NULL)
                {
                      temp = first->next;
                }
                temp->next = node;
            }
			//cin>>AppointmenListcurrentSize].day;
			//cout<<"Enter Appointment Date: ";
            //cin>>AppointmenList[currentSize].date;
			//cout<<"Enter Appointment Time: ";
            //cin>>AppointmenList[currentSize].time;
			cout<<endl;

	  		//cin.get();          //read newline character left in the buffer
			//currentSize += 1;
//		}
//    }
//	else
//    {	
//         cout<<"Overflow!!!! Appointment List is full"<<endl;
//         cout<<"\nPress any key to continue.."<<endl;
//         cin.get();     //read a character
//    }
}


void displayHeading( )
{      
           cout<<setiosflags(ios::left);                //left justify output
			cout<<endl<<setw(20)<<"Name"
               <<setw(20)<< "Descriptions"
               <<setw(12)<<"Date"
			   <<setw(15)<<"Time"<<endl;
}

 
void displayAllRecords( )
{   /* print the data from the array of records under a suitable header*/
    
      int index;

       system("cls");
//       displayHeading();
    //   cout<<setiosflags(ios::left);                //left justify output

//       for (index = 0; index < currentSize; index++)
//        {                
//	cout<<setw(20)<<AppointmenList[index].name;
//	cout<<setw(15)<<AppointmenList[index].description;
//	cout<<setw(12)<<AppointmenList[index].date;
//	cout<<setw(12)<<AppointmenList[index].time<<endl;
//        }

          if(first != NULL)
          {
           AppointmentRecord *temp;
           temp = first;
           cout<<"Your appointment(s) list is : ";
           while (temp != NULL)
           {
              cout<<temp->day + ",";
              temp=temp->next;
           }
          }
   //give the user a chance to read the output data
  	cout<<endl<<endl<<"Press any character to continue  ";
	cin.get();          //read entered character      
}


void delAppointment()
{
  string day;
  cout<<"Enter Appointment Day to delete: ";
  cin>>day;  
  
  if(first != NULL)
  {
           AppointmentRecord *temp, *previous;
           temp = first;
           if(first->day == day)
           {
               first=first->next;
               cout<<"Appointmet deleted successfully.";
           }
           else
           {
               while (temp != NULL)
               {
                     if(temp->day == day)
                     {
                         previous->next=temp->next;
                         cout<<"Appointmet deleted successfully.";
                         temp=temp->next;
                     }
                     else
                     {
                         previous = temp;
                         temp=temp->next;
                     }
               }
           }
  }
  
  cin.get();
}

Open in new window


But now i can't get the program to run not sure where my error is at.. 1>tetstfghjgv.cpp(46): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
0
 
LVL 2

Assisted Solution

by:xeltek
xeltek earned 166 total points
ID: 36482070
Hi,
I noticed that you are using c++, why not use standard container, vector or list.
http://www.cplusplus.com/reference/stl/list/
http://www.cplusplus.com/reference/stl/vector/
0
 
LVL 8

Assisted Solution

by:eager
eager earned 334 total points
ID: 36482135
Line 46 should read

  int main (void)

Main always is required to be defined to return an int.  As the message says, C++ does not assume that functions declared without a type to return an int.  
0
 

Author Closing Comment

by:businessesatoz
ID: 36504261
thank you.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now