﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Tweaks.com Forum  /  Community Forums / Off Topic Forum  / C++ Programming Help / Latest Posts</title><generator>InstantForum.NET v4.1.4</generator><description>Tweaks.com Forum </description><link>http://forum.tweaks.com/forum/</link><webMaster>forum@tweaks.com</webMaster><lastBuildDate>Sun, 23 Nov 2008 06:39:06 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: C++ Programming Help</title><link>http://forum.tweaks.com/forum/Topic227825-10-1.aspx</link><description>Never mind anyone... I fixed my problem!</description><pubDate>Fri, 14 Sep 2007 14:49:30 GMT</pubDate><dc:creator>tripleplay777</dc:creator></item><item><title>C++ Programming Help</title><link>http://forum.tweaks.com/forum/Topic227825-10-1.aspx</link><description>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.&lt;br&gt;&lt;br&gt;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.&lt;br&gt;&lt;br&gt;-Thanks for any help I may receive &lt;br&gt;&lt;br&gt;[quote]#include &lt;string&gt;&lt;br&gt;#include &lt;iostream&gt;&lt;br&gt;#include &lt;cassert&gt;&lt;br&gt;#include &lt;cstring&gt;&lt;br&gt;&lt;br&gt;using namespace std;&lt;br&gt;&lt;br&gt;class List;&lt;br&gt;class Iterator;&lt;br&gt;&lt;br&gt;class Information&lt;br&gt;{&lt;br&gt;public:&lt;br&gt;	void setInformation(char* s);&lt;br&gt;&lt;br&gt;private:&lt;br&gt;	char input[20];&lt;br&gt;friend class List;&lt;br&gt;friend class Iterator;&lt;br&gt;&lt;br&gt;};&lt;br&gt;class Node&lt;br&gt;{&lt;br&gt;public:&lt;br&gt;   /* &lt;br&gt;      Constructs a node with a given data value.&lt;br&gt;      @param s the data to store in this node&lt;br&gt;   */&lt;br&gt;   Node(char* s);&lt;br&gt;private:&lt;br&gt;   char* data;&lt;br&gt;   Node* previous;&lt;br&gt;   Node* next;&lt;br&gt;friend class List;&lt;br&gt;friend class Iterator;&lt;br&gt;};&lt;br&gt;&lt;br&gt;class List&lt;br&gt;{&lt;br&gt;public:&lt;br&gt;   /**&lt;br&gt;      Constructs an empty list;&lt;br&gt;   */&lt;br&gt;   List();&lt;br&gt;   /**&lt;br&gt;      Appends an element to the list.&lt;br&gt;      @param s the value to append&lt;br&gt;   */&lt;br&gt;   void push_back(char* s);&lt;br&gt;   /**&lt;br&gt;      Inserts an element into the list.&lt;br&gt;      @param iter the position before which to insert&lt;br&gt;      @param s the value to append&lt;br&gt;   */&lt;br&gt;   void insert(Iterator iter, char* s);&lt;br&gt;   /**&lt;br&gt;      Removes an element from the list.&lt;br&gt;      @param i the position to remove&lt;br&gt;      @return an iterator pointing to the element after the&lt;br&gt;      erased element&lt;br&gt;   */&lt;br&gt;	&lt;br&gt;   Iterator erase(Iterator i);&lt;br&gt;   /**&lt;br&gt;      Gets the beginning position of the list.&lt;br&gt;      @return an iterator pointing to the beginning of the list&lt;br&gt;   */&lt;br&gt;   Iterator begin();&lt;br&gt;   /**&lt;br&gt;      Gets the past-the-end position of the list.&lt;br&gt;      @return an iterator pointing past the end of the list&lt;br&gt;   */&lt;br&gt;   Iterator end();&lt;br&gt;private:&lt;br&gt;   Node* first;&lt;br&gt;   Node* last;&lt;br&gt;};&lt;br&gt;&lt;br&gt;class Iterator&lt;br&gt;{&lt;br&gt;public:&lt;br&gt;   /**&lt;br&gt;      Constructs an iterator that does not point into any list.&lt;br&gt;   */&lt;br&gt;   Iterator();&lt;br&gt;   /**  &lt;br&gt;      Looks up the value at a position.&lt;br&gt;      @return the value of the node to which the iterator points&lt;br&gt;   */&lt;br&gt;   char* get() const;&lt;br&gt;   /**&lt;br&gt;      Advances the iterator to the next node.&lt;br&gt;   */&lt;br&gt;   void next();&lt;br&gt;   /**&lt;br&gt;      Moves the iterator to the previous node.&lt;br&gt;   */&lt;br&gt;   void previous();&lt;br&gt;   /**&lt;br&gt;      Compares two iterators&lt;br&gt;      @param b the iterator to compare with this iterator&lt;br&gt;      @return true if this iterator and b are equal&lt;br&gt;   */&lt;br&gt;   bool equals(Iterator b) const;&lt;br&gt;private:&lt;br&gt;   Node* position;&lt;br&gt;   Node* last;&lt;br&gt;friend class List;&lt;br&gt;};&lt;br&gt;&lt;br&gt;void Information::setInformation()&lt;br&gt;{&lt;br&gt;	cout &lt;&lt; "Enter a name to input into the database: " &lt;&lt; endl;&lt;br&gt;	cin &gt;&gt; input;&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;br&gt;Node::Node(char* s)&lt;br&gt;{  &lt;br&gt;   data = s;&lt;br&gt;   previous = NULL;&lt;br&gt;   next = NULL;&lt;br&gt;}&lt;br&gt;&lt;br&gt;List::List()&lt;br&gt;{  &lt;br&gt;   first = NULL;&lt;br&gt;   last = NULL;&lt;br&gt;}&lt;br&gt;&lt;br&gt;void List::push_back(char* s)&lt;br&gt;{  &lt;br&gt;   Node* newnode = new Node(s);&lt;br&gt;   if (last == NULL) /* list is empty */&lt;br&gt;   {  &lt;br&gt;      first = newnode;&lt;br&gt;      last = newnode;&lt;br&gt;   }&lt;br&gt;   else&lt;br&gt;   {  &lt;br&gt;      newnode-&gt;previous = last;&lt;br&gt;      last-&gt;next = newnode;&lt;br&gt;      last = newnode;&lt;br&gt;   }&lt;br&gt;}&lt;br&gt;&lt;br&gt;void List::insert(Iterator iter, char* s)&lt;br&gt;{  &lt;br&gt;   if (iter.position == NULL)&lt;br&gt;   {  &lt;br&gt;      push_back(s);&lt;br&gt;      return;&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   Node* after = iter.position;&lt;br&gt;   Node* before = after-&gt;previous;&lt;br&gt;   Node* newnode = new Node(s);&lt;br&gt;   newnode-&gt;previous = before;&lt;br&gt;   newnode-&gt;next = after;&lt;br&gt;   after-&gt;previous = newnode;&lt;br&gt;   if (before == NULL) /* insert at beginning */&lt;br&gt;      first = newnode;&lt;br&gt;   else&lt;br&gt;      before-&gt;next = newnode;&lt;br&gt;}&lt;br&gt;&lt;br&gt;Iterator List::erase(Iterator i)&lt;br&gt;{  &lt;br&gt;   Iterator iter = i;&lt;br&gt;   assert(iter.position != NULL);&lt;br&gt;   Node* remove = iter.position;&lt;br&gt;   Node* before = remove-&gt;previous;&lt;br&gt;   Node* after = remove-&gt;next;&lt;br&gt;   if (remove == first)&lt;br&gt;      first = after;&lt;br&gt;   else&lt;br&gt;      before-&gt;next = after;&lt;br&gt;   if (remove == last)&lt;br&gt;      last = before;&lt;br&gt;   else&lt;br&gt;      after-&gt;previous = before;&lt;br&gt;   iter.position = after;&lt;br&gt;   delete remove;&lt;br&gt;   return iter;&lt;br&gt;}&lt;br&gt;&lt;br&gt;Iterator List::begin()&lt;br&gt;{  &lt;br&gt;   Iterator iter;&lt;br&gt;   iter.position = first;&lt;br&gt;   iter.last = last;&lt;br&gt;   return iter;&lt;br&gt;}&lt;br&gt;&lt;br&gt;Iterator List::end()&lt;br&gt;{  &lt;br&gt;   Iterator iter;&lt;br&gt;   iter.position = NULL;&lt;br&gt;   iter.last = last;&lt;br&gt;   return iter;&lt;br&gt;}&lt;br&gt;&lt;br&gt;Iterator::Iterator()&lt;br&gt;{  &lt;br&gt;   position = NULL;&lt;br&gt;   last = NULL;&lt;br&gt;}&lt;br&gt;&lt;br&gt;char* Iterator::get() const&lt;br&gt;{  &lt;br&gt;   assert(position != NULL);&lt;br&gt;   return position-&gt;data;&lt;br&gt;}&lt;br&gt;&lt;br&gt;void Iterator::next()&lt;br&gt;{  &lt;br&gt;   assert(position != NULL);&lt;br&gt;   position = position-&gt;next;&lt;br&gt;}&lt;br&gt;&lt;br&gt;void Iterator::previous()&lt;br&gt;{  &lt;br&gt;   if (position == NULL)&lt;br&gt;      position = last;&lt;br&gt;   else &lt;br&gt;      position = position-&gt;previous;&lt;br&gt;   assert(position != NULL);&lt;br&gt;}&lt;br&gt;&lt;br&gt;bool Iterator::equals(Iterator b) const&lt;br&gt;{  &lt;br&gt;   return position == b.position;&lt;br&gt;}&lt;br&gt;&lt;br&gt;int main()&lt;br&gt;{  &lt;br&gt;		cout &lt;&lt; "What would you like to do today?" &lt;&lt; endl;&lt;br&gt;		cout &lt;&lt; "1 : Add names to the linked list" &lt;&lt; endl;&lt;br&gt;		int choice;&lt;br&gt;		cin &gt;&gt; choice;&lt;br&gt;&lt;br&gt;	if(choice==1)&lt;br&gt;	{&lt;br&gt;	bool more = true;&lt;br&gt;	while(more)&lt;br&gt;	{&lt;br&gt;		List staff;&lt;br&gt;		Information name;&lt;br&gt;		name.setInformation();&lt;br&gt;		staff.push_back(name);&lt;br&gt;&lt;br&gt;		cout &lt;&lt; "Would you like to enter more information? (Y/N)" &lt;&lt; endl;&lt;br&gt;		string newchoice;&lt;br&gt;		cin &gt;&gt; newchoice;&lt;br&gt;&lt;br&gt;		if(newchoice!="Y") more=false;&lt;br&gt;	&lt;br&gt;			//staff.push_back("Carl");&lt;br&gt;			//staff.push_back("Harry");&lt;br&gt;			//staff.push_back("Larry");&lt;br&gt;			//staff.push_back("Susan");&lt;br&gt;&lt;br&gt;   /* add a value in fourth place */&lt;br&gt;&lt;br&gt;  Iterator pos;&lt;br&gt;&lt;br&gt; //Iterate through every element in the list&lt;br&gt;	&lt;br&gt;	   //Compare the current name in the data field with the next name in the data field&lt;br&gt;			//Use strcmp to compare the strings of the current node with the next node&lt;br&gt;	   //If the name falls between the two names, use the insert function to insert it between the names&lt;br&gt;	   //Reroute the links so the previous node is pointing from the newnode to the before node&lt;br&gt;	   //and the next node is pointing from the new node to the after node&lt;br&gt;  &lt;br&gt; &lt;br&gt;	for (pos = staff.begin(); !pos.equals(staff.end()); pos.next())&lt;br&gt;	{&lt;br&gt;		cout &lt;&lt; "Enter a name you would like to insert into the database: " &lt;&lt; endl;&lt;br&gt;	   char newname[15];&lt;br&gt;		cin &gt;&gt; newname;	&lt;br&gt;&lt;br&gt;		if(strcmp(newname, pos.get()) &lt; 0)&lt;br&gt;		{&lt;br&gt;			staff.insert(pos,newname);&lt;br&gt;			 for (pos = staff.begin(); !pos.equals(staff.end()); pos.next())&lt;br&gt;			 cout &lt;&lt; pos.get() &lt;&lt; "\n";&lt;br&gt;			exit(1);	&lt;br&gt;		}&lt;br&gt;	&lt;br&gt;	&lt;br&gt;		while(strcmp(newname, pos.get()) &gt; 0)&lt;br&gt;		{&lt;br&gt;			pos.next();&lt;br&gt;		&lt;br&gt;			if(strcmp(newname, pos.get()) &lt; 0)&lt;br&gt;			{&lt;br&gt;				staff.insert(pos,newname);&lt;br&gt;				for (pos = staff.begin(); !pos.equals(staff.end()); pos.next())&lt;br&gt;				cout &lt;&lt; pos.get() &lt;&lt; "\n";&lt;br&gt;				exit(1);&lt;br&gt;			}&lt;br&gt;			&lt;br&gt;		}&lt;br&gt;&lt;br&gt;	}&lt;br&gt;&lt;br&gt;	}&lt;br&gt;	}&lt;br&gt;&lt;br&gt;   /* print all values */&lt;br&gt;&lt;br&gt;   //for (pos = staff.begin(); !pos.equals(staff.end()); pos.next())&lt;br&gt;      //cout &lt;&lt; pos.get() &lt;&lt; "\n";&lt;br&gt;&lt;br&gt;   return 0;&lt;br&gt;}&lt;br&gt;&lt;br&gt;[/quote]&lt;br&gt;&lt;br&gt;</description><pubDate>Fri, 14 Sep 2007 09:59:14 GMT</pubDate><dc:creator>tripleplay777</dc:creator></item></channel></rss>