C++ Saved Program: fragrant-life

facebook share

Here's the online compiler accompanied with IDE at your service. This is a saved code by one of our users. Try it out or save your own from the IDE page and don't forget to leave a feedback. You can find that at the bottom of the page.


// Daniel Malone
// Project 15.2
//5-11-14

#include <iostream>
#include <iomanip.h>
#include <string.h>

const int event_LENGTH = 20;

struct timeline
{
	char events[event_LENGTH];
	long dates;
	timeline *next;
};

int get_data(char name[event_LENGTH], long &popul);
void display_list(timeline *head_ptr);
void delete_list(timeline *head_prtr);
void insert_node(timeline* &head_ptr, char name[event_LENGTH], long popul);
void menu(int &choice);
void delete_node(timeline *&head_ptr);

void main()
{
	
	char event[event_LENGTH];
	long date;
	int choice;
	timeline *head_ptr = NULL;
	
	do
	{
		menu(choice);
		switch(choice)
		{
		case 1:
			if(get_data(event, date))
			{
				insert_node(head_ptr, event, date);
			}
			break;
		case 2:
			delete_node(head_ptr);
			break;
		case 3:
			display_list(head_ptr);
			break;
		case 4:
			delete_list(head_ptr);
			break;
		}
	}
	while (choice != 4);

	
	cout <<"\nENDOFPROGRAM";
	
}

void menu(int &choice)
{
	cout << "\nMain Menu\n";
	cout << "1 Insert new historical event\n";
	cout << "2 Delete an event\n";
	cout << "3. Display all events\n";
	cout << "4 Exit\n\n";
	cin >> choice;
	cin.ignore(80, '\n');
}

void insert_node(timeline* &head_ptr, char event[event_LENGTH], long date)
{
	timeline* new_rec_ptr;
	timeline* before_ptr;
	timeline* after_ptr;
	
	new_rec_ptr = new timeline;
	if(new_rec_ptr != NULL)
	{
		strcpy(new_rec_ptr->events, event);
		new_rec_ptr->dates = date;
		
		after_ptr = head_ptr;
		while(after_ptr != NULL && date < after_ptr->dates)
		{
			before_ptr = after_ptr;
			after_ptr = before_ptr->next;
		}
		
		if(after_ptr == head_ptr)
		{
			new_rec_ptr->next = head_ptr;
			head_ptr = new_rec_ptr;
		}
		else
		{
			new_rec_ptr->next = after_ptr;
			before_ptr->next = new_rec_ptr;
		}
	}
}
int get_data(char event[event_LENGTH], long &date)
{
	int heep_data = 1;
	
	cout << "Enter event name (Press Enter alone to stop): ";
	cin.get(event,event_LENGTH);
	cin.ignore(80,'\n');
	if(event[0] != '\0')
	{
		cout <<"Enter event date: ";
		cin >> date;
		cin.ignore(80,'\n');
	}
	
	else
	{
		heep_data = 0;
	}
	
	return(heep_data);
}


void display_list(timeline *head_ptr)
{
	
	timeline *current_ptr;
	
	
	
	current_ptr = head_ptr;
	
	cout.setf(ios::left);
	cout << setw(25) << "Event";
	cout.unsetf(ios::left);
	cout << setw(12) << "Date" << "\n\n";
	
	while(current_ptr != NULL)
	{
		cout.setf(ios::left);
		cout << setw(25) << current_ptr->events;
		cout.unsetf(ios::left);
		cout << setw(12) << current_ptr->dates << endl;
		current_ptr = current_ptr->next;
	}
	
}

void delete_node(timeline *&head_ptr)
{
	timeline *current_ptr;
	timeline *before_ptr;
	char event[event_LENGTH];
	char answer;
	int found = 0;

	cout << "Enter Event Name> ";
	cin.get(event, event_LENGTH);
	cin.ignore(80, '\n');

	current_ptr = head_ptr;

	while(current_ptr != NULL)
	{
		if(strcmp(current_ptr->events, event) == 0)
		{
			found++;
			cout << endl;
			cout <<"Event: " << current_ptr->events << endl;
			cout << "Date: " << current_ptr->dates << endl;

			cout << "\nDo you want to delte this node?(y/n) > ";
			cin >> answer;
			cin.ignore(80,'\n');

			if(answer == 'Y' || answer == 'y')
			{
				if(current_ptr == head_ptr && current_ptr != NULL)
				{
					head_ptr = current_ptr->next;
					delete current_ptr;
					current_ptr = head_ptr;
				}
				else if(current_ptr != head_ptr && current_ptr != NULL)
				{
					before_ptr->next = current_ptr->next;
					delete current_ptr;
					current_ptr = before_ptr;
				}
			}
			else
			{
				before_ptr = current_ptr;
				current_ptr = before_ptr->next;
			}
		}
		else
		{
			before_ptr = current_ptr;
			current_ptr = before_ptr->next;
		}
	}
	if(found == 0)
	{
		if(current_ptr == head_ptr && current_ptr == NULL)
		{
			cout << "\nNo event in list\n";
		}
		else
		{
			cout << "\nEvent not in list\n";
		}
	}
}
void delete_list(timeline *head_ptr)
{
	timeline *current_ptr;
	timeline *temp_ptr;
	
	current_ptr = head_ptr;
	
	while(current_ptr != NULL)
	{
		temp_ptr = current_ptr->next;
		
		delete current_ptr;
		current_ptr = temp_ptr;
	}
	
}
Execute

Output

Press the execute button or CTRL + enter to run code.

Inputs