6 Replies - 588 Views - Last Post: 14 May 2009 - 07:37 AM Rate Topic: -----

#1 hawks9nkh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-May 09

I'm terrible with functions. Help me?

Post icon  Posted 13 May 2009 - 08:36 AM

#include<iostream>
#include <string>
using std::string;
using namespace std;




class Database;

class Record
{
	int student_ID;
	int stud_age;
	string first_name;
	string last_name;
	string major;
	char grade;
	Record *next;

public:
	friend class Database;
	friend ostream &operator<<(ostream &, Record &);
	Record(const int, const int, string, string, string);
	int getID() const;

};

class Database
{
	Record *firstptr;

public:
	friend ostream &operator<<(ostream &, Database &);
	Database();
	void print(Database);
	bool isEmpty() const;
	void insert();
	void deleteRecord();
	Record *retrieveRecord(int);

};




//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------




Record::Record(const int id, const int age, string firstname, string lastname, string major)
{
	student_ID = id;
	stud_age = age;
	first_name = firstname;
	last_name = lastname;
	major = major;
	next = NULL;
}

ostream &operator<<(ostream &output, Record &newrecord)
{
	output << "Student ID: " << newrecord.student_ID << endl;
	output << "Age: " << newrecord.stud_age << endl;
	output << "First Name: " << newrecord.first_name << endl;
	output << "Last Name: " << newrecord.last_name << endl;
	output << "Major: " << newrecord.major << endl;
	return output;
}

int Record::getID() const
{
	return student_ID;
}

//--------------------------------------------------------------------------

Database::Database(): firstptr(NULL)
{
	//empty
}

bool Database::isEmpty() const
{
	return (firstptr == 0);
}

Record *Database::retrieveRecord(int y)
{
	int y;
	cout<<"Please enter the student id number of the record you want to retrieve";
	cin>> y;
	Record *temp = firstptr;
	while(temp != NULL)
		if (temp->student_ID == y)
			return temp;
	return 0;
}

void Database::deleteRecord(int deletenum)
{
	int deletenum;

	cout << "What record would you like to delete? ";
	cin >> deletenum;
	Record *temp = firstptr;
	Record *prev = temp;

	while(temp != NULL)
	{
		if(temp->student_ID == deletenum)
			break;
		prev = temp;
		temp = temp->next;
	}
	if(temp == NULL)
		cout << "There was no match.";
	else if (temp == prev)
	{
			firstptr = temp->next;
			delete temp;
	}
	else
	{
		prev->next = temp->next;
		delete temp;
	}
}

void Database::print(Database a)
{
	Record *currentPtr = a.firstptr;
	while ( currentPtr != NULL)
	{
		cout << currentPtr->student_ID << " ";
		currentPtr = currentPtr->next;
	}
}


void Database::insert(void)
{
	int id;
	int age;
	string firstname;
	string lastname;
	string major;

	cout << "Please enter id: ";
	cin >> id;
	cout << "Please enter age: ";
	cin >> age;
	cout << "Please enter first name: ";
	cin >> firstname;
	cout << "Please enter last name: ";
	cin >> lastname;
	cout << "Please enter major: ";
	cin >> major;
	
	
	Record *newNode = new Record(id, age, firstname, lastname, major);
	
	if ( isEmpty() )
		firstptr =  newNode;
	else
	{
		Record *temp = firstptr;
		Record *prev = temp;

		while (temp != NULL && temp->student_ID < id)
		{
			prev = temp;
			temp = temp->next;
		}
		if(prev == temp)
		{
			newNode->next = firstptr;
			firstptr = newNode;
		}
		else
		{
			newNode->next = prev->next;
			prev->next = newNode;
		}
	}
}

int main()
{
	int option;
	cout<<"What would you like to do with the database? Please type in the number \
corresponding to the options given.\n \
1. Display the records. \n \
2. Insert a record. \n \
3. Retrieve a specific record. \n \
4. Delete a record. \n";

cin>>option;
	switch(option){
		case 1: print();
			break;
		case 2: insert();
			break;
		case 3: retrieveRecord();
			break;
		case 4: deleteRecord();
			break;
		default:
			cout<< "You have entered an invalid choice, please pick again.\n \
				   1 to display, 2 to insert, 3 to retrieve and 4 to delete. \n";
				   cin>>option;
	}

return 0;
} 


Well there's the code, but I don't know what parameters to put into the function calls, or when I define the functions either. I'm not really a programming type of person, I just don't understand this stuff. It's difficult enough for me to tell you what's going on in that linked list (had a friend help me) let alone tell you what the program is doing.

Feedback?

Is This A Good Question/Topic? 0
  • +

Replies To: I'm terrible with functions. Help me?

#2 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 966
  • Joined: 09-May 09

Re: I'm terrible with functions. Help me?

Posted 13 May 2009 - 08:47 AM

i know you have had help, but u must speak to your friend and have him explain whats going on in the code.
Take it slow, use pen and paper to work through a program. Remember programs are not built out of thin air they take planning like everything else

1 step at a time mate.

This post has been edited by deery5000: 13 May 2009 - 08:48 AM

Was This Post Helpful? 0
  • +
  • -

#3 hawks9nkh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-May 09

Re: I'm terrible with functions. Help me?

Posted 13 May 2009 - 08:57 AM

View Postdeery5000, on 13 May, 2009 - 07:47 AM, said:

i know you have had help, but u must speak to your friend and have him explain whats going on in the code.
Take it slow, use pen and paper to work through a program. Remember programs are not built out of thin air they take planning like everything else

1 step at a time mate.


Could you at least explain to me what parameters would go into the function when it is called? Like an int, char? I'm really confused about this and it won't compile in the main program.
Was This Post Helpful? 0
  • +
  • -

#4 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: I'm terrible with functions. Help me?

Posted 13 May 2009 - 09:04 AM

Which parameter are you specifically referring to?

Also, If your having trouble with functions, don't move straight into classes and deriving them, those complicate functions overall.
Was This Post Helpful? 0
  • +
  • -

#5 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 966
  • Joined: 09-May 09

Re: I'm terrible with functions. Help me?

Posted 13 May 2009 - 09:11 AM

View Posthawks9nkh, on 13 May, 2009 - 07:57 AM, said:

View Postdeery5000, on 13 May, 2009 - 07:47 AM, said:

i know you have had help, but u must speak to your friend and have him explain whats going on in the code.
Take it slow, use pen and paper to work through a program. Remember programs are not built out of thin air they take planning like everything else

1 step at a time mate.


Could you at least explain to me what parameters would go into the function when it is called? Like an int, char? I'm really confused about this and it won't compile in the main program.


Not quite sure what to do with your code, quite a few errors with your code, some functions are overloaded (to many or little variables)

You already passed in int Y, so you shouldnt declare it again there, change y to x or something , int x , cin >> x


int y;
	cout<<"Please enter the student id number of the record you want to retrieve";
	cin>> y;
	Record *temp = firstptr;
	while(temp != NULL)
		if (temp->student_ID == y)
			return temp;
	return 0;



same goes for delete function, if you declare a function with no variables then try and pass one in that wont work,

eg
void delete(int y);

class::delete()
{

}

this wont work cause its expecting a variable.

need to be

class::delete(int y)
{

}

do u see where im going with this?
if u say a function needs int y then you must pass int y into the function
Was This Post Helpful? 0
  • +
  • -

#6 hawks9nkh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-May 09

Re: I'm terrible with functions. Help me?

Posted 13 May 2009 - 05:37 PM

Ah, I got it more now. I know that if a function is void it doesn't return a value, but I'm just clueless with the syntax. Either way, once I'm done with this I won't ever have to take a programming class again, cept for computer architecture, but I'm not a programming type of person. Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#7 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Re: I'm terrible with functions. Help me?

Posted 14 May 2009 - 07:37 AM

:D Hello, nice to meet you.

If I understand your question, the following information might help a little:

In a relational database, your data is organized into one or more tables. You can think of a database table as a spreadsheet table, made up of rows and columns. Each row contains information about a single item, and each column contains the information about the same characteristic from every item. A record is equivalent to a row in the spreadsheet. Each record consists of elements of data that make up that record. These elements of data are known as fields. A field is a cell in the table identified by the column heading. A table usually has at least one field that can be used to identify each record uniquely. A field in a table that serves to identify each record within the table is referred to as a key; a key that uniquely identifies each record in a table is referred to as a primary key. The relational aspect of a database, and the importance of the keys, comes into play when you store related information in separate tables.

For example, the following would be a record or table column headings:

PRIMARY KEY
m_student_ID-----m_stud_age-----m_first_name-----m_last_name-----m_major-----m_grade

In your program you are defining two classes: CRecord and CDatabase. Microsoft Foundation Classes (MFC) adopts the convention of using the prefix C for all class names. MFC also prefixes members of classes with m_ to distinguish them from other variables.

This naming convention helps a lot, even in non-MFC environments.

Hang in there you will do fine. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1