C++ Programming Help
 
  Tweaks.com
 Home    Members    Calendar    Who's On        Main Site
 




C++ Programming HelpExpand / Collapse
Author
Message
Posted 9/14/2007 9:59 AM
New Member

New MemberNew MemberNew MemberNew MemberNew MemberNew MemberNew MemberNew MemberNew MemberNew Member

Group: Forum Members
Last Login: 9/14/2007 9:31 PM
Posts: 13, Visits: 54
Hello everyone. If anyone has some free time and would like to help me out with my code, I would really appreciate it. Okay... so my program is similar to a database program and uses linked lists to link each name together. I prompts the user for names they would like to enter into the database and I would like for the names to use the push_back member function I have in the List class. Also, as you can see in my main program, I have a menu prompting the user if they would like to enter names into the linked list. I then setup a while loop so that the user can enter multiple names. After they have entered the names they want into the database (assuming they did it in alphabetical order), I use while loops and if statements and yet prompt again for a name to insert. This time, it inserts the name automatically in alphabetical order into my linked list.

I learned linked lists in C++ a couple a months ago and want to refresh my memory. This is not a homework assignment, I just want to give myself some practice. Here is my code (it is quite lengthy). The error I am getting is Error C2664 and even with Microsoft's MSDN help, I still cannot solve my problem.

-Thanks for any help I may receive

#include
#include
#include
#include

using namespace std;

class List;
class Iterator;

class Information
{
public:
void setInformation(char* s);

private:
char input[20];
friend class List;
friend class Iterator;

};
class Node
{
public:
/*
Constructs a node with a given data value.
@param s the data to store in this node
*/
Node(char* s);
private:
char* data;
Node* previous;
Node* next;
friend class List;
friend class Iterator;
};

class List
{
public:
/**
Constructs an empty list;
*/
List();
/**
Appends an element to the list.
@param s the value to append
*/
void push_back(char* s);
/**
Inserts an element into the list.
@param iter the position before which to insert
@param s the value to append
*/
void insert(Iterator iter, char* s);
/**
Removes an element from the list.
@param i the position to remove
@return an iterator pointing to the element after the
erased element
*/

Iterator erase(Iterator i);
/**
Gets the beginning position of the list.
@return an iterator pointing to the beginning of the list
*/
Iterator begin();
/**
Gets the past-the-end position of the list.
@return an iterator pointing past the end of the list
*/
Iterator end();
private:
Node* first;
Node* last;
};

class Iterator
{
public:
/**
Constructs an iterator that does not point into any list.
*/
Iterator();
/**
Looks up the value at a position.
@return the value of the node to which the iterator points
*/
char* get() const;
/**
Advances the iterator to the next node.
*/
void next();
/**
Moves the iterator to the previous node.
*/
void previous();
/**
Compares two iterators
@param b the iterator to compare with this iterator
@return true if this iterator and b are equal
*/
bool equals(Iterator b) const;
private:
Node* position;
Node* last;
friend class List;
};

void Information::setInformation()
{
cout << "Enter a name to input into the database: " << endl;
cin >> input;
}


Node::Node(char* s)
{
data = s;
previous = NULL;
next = NULL;
}

List::List()
{
first = NULL;
last = NULL;
}

void List::push_back(char* s)
{
Node* newnode = new Node(s);
if (last == NULL) /* list is empty */
{
first = newnode;
last = newnode;
}
else
{
newnode->previous = last;
last->next = newnode;
last = newnode;
}
}

void List::insert(Iterator iter, char* s)
{
if (iter.position == NULL)
{
push_back(s);
return;
}

Node* after = iter.position;
Node* before = after->previous;
Node* newnode = new Node(s);
newnode->previous = before;
newnode->next = after;
after->previous = newnode;
if (before == NULL) /* insert at beginning */
first = newnode;
else
before->next = newnode;
}

Iterator List::erase(Iterator i)
{
Iterator iter = i;
assert(iter.position != NULL);
Node* remove = iter.position;
Node* before = remove->previous;
Node* after = remove->next;
if (remove == first)
first = after;
else
before->next = after;
if (remove == last)
last = before;
else
after->previous = before;
iter.position = after;
delete remove;
return iter;
}

Iterator List::begin()
{
Iterator iter;
iter.position = first;
iter.last = last;
return iter;
}

Iterator List::end()
{
Iterator iter;
iter.position = NULL;
iter.last = last;
return iter;
}

Iterator::Iterator()
{
position = NULL;
last = NULL;
}

char* Iterator::get() const
{
assert(position != NULL);
return position->data;
}

void Iterator::next()
{
assert(position != NULL);
position = position->next;
}

void Iterator::previous()
{
if (position == NULL)
position = last;
else
position = position->previous;
assert(position != NULL);
}

bool Iterator::equals(Iterator b) const
{
return position == b.position;
}

int main()
{
cout << "What would you like to do today?" << endl;
cout << "1 : Add names to the linked list" << endl;
int choice;
cin >> choice;

if(choice==1)
{
bool more = true;
while(more)
{
List staff;
Information name;
name.setInformation();
staff.push_back(name);

cout << "Would you like to enter more information? (Y/N)" << endl;
string newchoice;
cin >> newchoice;

if(newchoice!="Y") more=false;

//staff.push_back("Carl");
//staff.push_back("Harry");
//staff.push_back("Larry");
//staff.push_back("Susan");

/* add a value in fourth place */

Iterator pos;

//Iterate through every element in the list

//Compare the current name in the data field with the next name in the data field
//Use strcmp to compare the strings of the current node with the next node
//If the name falls between the two names, use the insert function to insert it between the names
//Reroute the links so the previous node is pointing from the newnode to the before node
//and the next node is pointing from the new node to the after node


for (pos = staff.begin(); !pos.equals(staff.end()); pos.next())
{
cout << "Enter a name you would like to insert into the database: " << endl;
char newname[15];
cin >> newname;

if(strcmp(newname, pos.get()) < 0)
{
staff.insert(pos,newname);
for (pos = staff.begin(); !pos.equals(staff.end()); pos.next())
cout << pos.get() << "\n";
exit(1);
}


while(strcmp(newname, pos.get()) > 0)
{
pos.next();

if(strcmp(newname, pos.get()) < 0)
{
staff.insert(pos,newname);
for (pos = staff.begin(); !pos.equals(staff.end()); pos.next())
cout << pos.get() << "\n";
exit(1);
}

}

}

}
}

/* print all values */

//for (pos = staff.begin(); !pos.equals(staff.end()); pos.next())
//cout << pos.get() << "\n";

return 0;
}



Post #227825
Posted 9/14/2007 2:49 PM
New Member

New MemberNew MemberNew MemberNew MemberNew MemberNew MemberNew MemberNew MemberNew MemberNew Member

Group: Forum Members
Last Login: 9/14/2007 9:31 PM
Posts: 13, Visits: 54
Never mind anyone... I fixed my problem!
Post #227833
« Prev Topic | Next Topic »


Reading This TopicExpand / Collapse

All times are GMT -6:00, Time now is 6:40am

Powered By InstantForum.NET v4.1.4 © 2008
Execution: 0.078. 10 queries. Compression Enabled.