Need help with program.

This is a discussion on Need help with program. within the C++ Programming forums, part of the General Programming Boards category; Project instructions: You’re working for a company that wants to take client contact information as it is entered by a ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    22

    Need help with program.

    Project instructions:

    You’re working for a company that wants to take client contact information as it is entered by a salesperson from a stack of business cards that he or she has collected, and output the information to a file (contacts.dat) in alphabetical order. Assume that there are never more than 100 cards to enter. The program should prompt the salesperson for each of the following values for each card:
    Last Name
    First Name
    Middle Name or Initial
    Title
    Company Name
    Street Address
    City
    State
    Zip Code
    Phone number
    Fax number
    Email address

    After the data are entered for each card, the user should be asked if there is another card to enter. This data should be sorted by Last Name, using a version of the SortedList class that will hold up to 100 struct values, each with a member corresponding to an item in the list above. Because the struct is so large, however, it will be more efficient if each element of the list array is a pointer to one of the struct values, and the class merely rearranges the pointers within the array in order to do the sorting. You should dynamically create a new struct value each componet of the list array to point to as the cards are input. The print function of the class will need to be modified to output the list to the contacts.dat file instead of cout. Each member of the struct should be written on a separate line. The function should also write the number of cards that are in the list as the first line of the file.

    I have created a program that works but I do not know how to implement the class SortedList. Could someone look at my code and make suggestions about how I can go about sorting with the class.

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Posts
    22
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include "SortedList.h"
    
    using namespace std;
    
    const MAX_LENGTH = 100;
    
    struct me
    {
    	string lName;
    	string fName;
    	string mName;
    	string title;
    	string companyName;
    	string streetAddress;
    	string city;
    	string state;
    	string zipCode;
    	string phoneNumber;
    	string faxNumber;
    	string emailAddress;
    	me *link;
    };
    
    int main()
    {
    	SortedList list;
    	// Where do I go from here
    
    	me *Head = NULL;
    	me *Current;
    	me *Next;
    	me *Previous;
    
    	ofstream fileOutput;
    	fileOutput.open("contacts.dat");
        
    	Current = Head;
    	Previous = Current;
    	Next = new me;
    	
    	char another;
    	int count = 0;
    
    	cout << "Enter last name: ";
    	getline(cin, Next->lName);
    	cout << "Enter first name: ";
    	getline(cin, Next->fName);
    	cout << "Enter middle name: ";
    	getline(cin, Next->mName);
    	cout << "Enter title: ";
    	getline(cin, Next->title);
    	cout << "Enter company name: ";
    	getline(cin, Next->companyName);
    	cout << "Enter street address: ";
    	getline(cin, Next->streetAddress);
    	cout << "Enter city: ";
    	getline(cin, Next->city);
    	cout << "Enter state: ";
    	getline(cin, Next->state);
    	cout << "Enter zip code: ";
    	getline(cin, Next->zipCode);
    	cout << "Enter phone number: ";
    	getline(cin, Next->phoneNumber);
    	cout << "Enter fax number: ";
    	getline(cin, Next->faxNumber);
    	cout << "Enter e-mail address: ";
    	getline(cin, Next->emailAddress);
    
    	count++;
    
    	cin.ignore(MAX_LENGTH, '\n');
    
    
    	if ( Head == NULL )
    	{
    		Head = Next;
    		Current = Head;
    		Current->link = NULL;
    		Previous = Current;
    	}
    	else
    	{
    		if ( Next->lName < Current->lName )
    		{
    			Next->link = Head;
    			Head = Next;
    		}
    		else
    		{
    			Current = Head;
    			Previous = Head;
    			while ( Current != NULL )
    			{
    				if ( Next->lName < Current->lName)
    				{
    					Next->link = Current;
    					Previous->link = Next;
    					break;
    				}
    				else
    				{
    					Previous = Current;
    					Current = Current->link;
    				}
    			}
    			if ( Current == NULL )
    			{
    				Previous->link = Next;
    			}
    		}
    	}
    	
    	cout << "Would you like to enter another contact(Y/N): ";
    	cin >> another;
    	
    	while (	another == 'Y' || another == 'y' )
    	{
    		cin.ignore(MAX_LENGTH, '\n');
    
    		Current = Head;
    		Previous = Current;
    		Next = new me;
    
    		cout << "Enter last name: ";
    		getline(cin, Next->lName);
    		cout << "Enter first name: ";
    		getline(cin, Next->fName);
    		cout << "Enter middle name: ";
    		getline(cin, Next->mName);
    		cout << "Enter title: ";
    		getline(cin, Next->title);
    		cout << "Enter company name: ";
    		getline(cin, Next->companyName);
    		cout << "Enter street address: ";
    		getline(cin, Next->streetAddress);
    		cout << "Enter city: ";
    		getline(cin, Next->city);
    		cout << "Enter state: ";
    		getline(cin, Next->state);
    		cout << "Enter zip code: ";
    		getline(cin, Next->zipCode);
    		cout << "Enter phone number: ";
    		getline(cin, Next->phoneNumber);
    		cout << "Enter fax number: ";
    		getline(cin, Next->faxNumber);
    		cout << "Enter e-mail address: ";
    		getline(cin, Next->emailAddress);
    
    		count++;
    		
    
    		if ( Head == NULL )
    		{
    			Head = Next;
    			Current = Head;
    			Current->link = NULL;
    			Previous = Current;
    		}
    		else
    		{
    			if ( Next->lName < Current->lName )
    			{
    				Next->link = Head;
    				Head = Next;
    			}
    			else
    			{
    				Current = Head;
    				Previous = Head;
    				while ( Current != NULL )
    				{
    					if ( Next->lName < Current->lName)
    					{
    						Next->link = Current;
    						Previous->link = Next;
    						break;
    					}
    					else
    					{
    						Previous = Current;
    						Current = Current->link;
    					}
    				}
    				if ( Current == NULL )
    				{
    					Previous->link = Next;
    				}
    			}
    		}
    		cout << "Would you like to enter another contact(Y/N): ";
    		cin >> another;
    	}
    
    	if ( another == 'N' || another == 'n' )
    	{
    		fileOutput << "There are " << count << " contacts in this file." << endl;
    		Current = Head;
    		while ( Current != NULL )
    		{
    			fileOutput << Current->lName << endl;
    			fileOutput << Current->fName << endl;
    			fileOutput << Current->mName << endl;
    			fileOutput << Current->title << endl;
    			fileOutput << Current->companyName << endl;
    			fileOutput << Current->streetAddress << endl;
    			fileOutput << Current->city << endl;
    			fileOutput << Current->state << endl;
    			fileOutput << Current->zipCode << endl;
    			fileOutput << Current->phoneNumber << endl;
    			fileOutput << Current->faxNumber << endl;
    			fileOutput << Current->emailAddress << endl;
    			Current = Current->link;
    		}
    		
    	}
    
    	fileOutput.close();
    	cout << "Data has been written to the file." << endl;
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    22
    Code:
    Here's the SortedList.h file
    
    //******************************************************************
    // SPECIFICATION FILE (SortedList.h)
    // This file gives the specification of a SortedList abstract data
    // type.
    // The SortedList components are assumed to be in order by value
    //******************************************************************
    
    
    typedef Entry ItemType;           // Type of each component
                                    //   (a simple type or string class)
    
    class SortedList
    {
    public:
        bool IsEmpty() const;
        // Postcondition:
        //     Return value is true, if SortedList is empty
        //                  is false, otherwise
    
        bool IsFull() const;
        // Postcondition:
        //     Return value is true, if SortedList is full
        //                  is false, otherwise
    
        int Length() const;
        // Postcondition:
        //     Return value is     length of SortedList
    
        void Insert( /* in */ ItemType& item );
        // Inserts item into the SortedList
     
        // Precondition:
        //     length < MAX_LENGTH
        //  && data[0..length&#208;1] are in ascending order
        // Postcondition:
        //     item is in the SortedList
        //  && length == length@entry + 1
        //  && data[0..length&#208;1] are in ascending order
     
    
        void Delete( /* in */ ItemType item );
            // Precondition:
            //     NOT IsEmpty()
            // Postcondition:
            //     IF item is in SortedSortedList at entry
            //           First occurrence of item is no longer in SortedList
            //        && Length() == Length()@entry - 1
            //     ELSE
            //           SortedList is unchanged
    
        bool IsPresent( /* in */ ItemType item ) const;
            // Precondition:
            //     item is assigned
            // Postcondition:
            //     Return value is true, if item is in SortedList
            //                  is false, otherwise
    
        void Reset();
            // Postcondition:
            //     Iteration is initialized
            
        ItemType GetNextItem();
            // Precondition:
            //     No transformers have been invoked since last call
            // Postcondition:
            //     Returns item at the current position in the SortedList
                	
        SortedList();
            // Constructor
            // Postcondition:
            //     Empty SortedList is created
            
        SortedList( /* in */ int numElements );
        // Postcondition:
        //     data has been created
        //     size has been set to numElements
        //     length has been set to 0
            
    private:
        int       length;
        int       size;
        int       currentPos;
        ItemType* data;
        void      BinSearch( ItemType, bool&, int& ) const;
    };

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    22
    Code:
    Here's the SortedList.cpp file (Implementation File)
    
    //******************************************************************
    // IMPLEMENTATION FILE (SortedList.cpp)
    // This file implements the SortedList class member functions
    // SortedList representation: a one-dimensional array and a length
    // variable
    // Array is generated at run time
    //******************************************************************
    #include "SortedList.h"
    #include <iostream>
    
    using namespace std;
    
    //******************************************************************
    
    SortedList::SortedList()
    
    // Constructor
    
    // Postcondition:
    //     length is set to zero
    //     size is set to 20
    //     data is created with 20 slots
    
    {
        length = 0;
        size = 20;
        data = new ItemType[size];
    }
    
    //******************************************************************
            
    SortedList::SortedList( /* in */ int numElements )
    
    // Constructor
    
    // Postcondition:
    //     data has been created with numElements slots
    //     size has been set to numElements
    //     length has been set to 0
    
    {
        length = 0;
        size = numElements;
        data = new ItemType[numElements];
    }    
        
    //******************************************************************
    
    bool SortedList::IsEmpty() const
    
    // Reports whether SortedList is empty
    
    // Postcondition:
    //     Return value is true, if length == 0
    //                  is false, otherwise
    
    {
        return (length == 0);
    }
    
    //******************************************************************
    
    bool SortedList::IsFull() const
    
    // Reports whether SortedList is full
    
    // Postcondition:
    //     Return value is true, if length equals size;
    //     is false, otherwise
    
    {
        return (length == size);
    }
    
    //******************************************************************
    
    int SortedList::Length() const
    
    // Returns current length of SortedList
    
    // Postcondition:
    //     Return value is length
    
    {
        return length;
    }
    
    //******************************************************************
    
    void SortedList::Insert( /* in */ ItemType& item )
     
    // Inserts item into the SortedList
     
    // Precondition:
    //     length < MAX_LENGTH
    //  && data[0..length&#208;1] are in ascending order
    //  && item is assigned
    // Postcondition:
    //     item is in the SortedList
    //  && length == length@entry + 1
    //  && data[0..length&#208;1] are in ascending order
     
    {
        int index;    // Index and loop control variable
        index = length - 1;
        while (index >= 0 && item.LessThan(data[index]))
        {
            data[index+1] = data[index];
            index--;
        }
        data[index+1] = item;          // Insert item
        length++;
    }
    
    //******************************************************************
    
      void SortedList::Reset()
            // Postcondition:
            //     Iteration is initialized
      {
          currentPos = 0;
      }
                
    //******************************************************************
             
     ItemType SortedList::GetNextItem()
            // Precondition:
            //     No transformers have been invoked since last call
            // Postcondition:
            //     Returns item at the current position in the SortedList 
            //     and resets current to next position or first position if
            //     last item is returned
      {
           ItemType item;
           item = data[currentPos];
           if (currentPos == length - 1)
               currentPos = 0;
           else
               currentPos++;
           return item;    
      }     
                  
    
    //******************************************************************
    
    void SortedList::BinSearch(
          /* in */  ItemType item,              // Item to be found
          /* out */ bool&    found,             // True if item is found
          /* out */ int&     position ) const   // Location if found
    
    // Searches list for item, returning the index
    // of item if item was found.
    
    // Precondition:
    //     length <= INT_MAX / 2
    //  && data[0..length-1] are in ascending order
    //  && item is assigned
    // Postcondition:
    //     IF item is in the list
    //         found == true  &&  data[position] contains item
    //     ELSE
    //         found == false  &&  position is undefined
    
    {
        int first = 0;            // Lower bound on list
        int last = length - 1;    // Upper bound on list
        int middle;               // Middle index
    
        found = false;
        while (last >= first && !found)
        {
            middle = (first + last) / 2;
            if (item.LessThan(data[middle]))
                // Assert: item is not in data[middle..last]
                last = middle - 1;
            else if (data[middle].LessThan(item))
                // Assert: item is not in data[first..middle]
                first = middle + 1;
            else
                // Assert: item == data[middle]
                found = true;
        }
        if (found)
            position = middle;
    }
    
    //******************************************************************
    
    void SortedList::Delete( /* in */ ItemType item )
    
    // Deletes item from the list, if it is there
    
    // Precondition:
    //     0 < length <= INT_MAX/2
    //  && data[0..length-1] are in ascending order
    //  && item is assigned
    // Postcondition:
    //     IF item is in data array at entry
    //           First occurrence of item is no longer in array
    //        && length == length@entry - 1
    //        && data[0..length-1] are in ascending order
    //     ELSE
    //           length and data array are unchanged
    
    {
        bool found;      // True if item is found
        int position;    // Position of item, if found
        int index;       // Index and loop control variable
    
        BinSearch(item, found, position);
        if (found)
        {
            // Shift data[position..length-1] up one position
    
            for (index = position; index < length - 1; index++)
                data[index] = data[index+1];
            length--;
        }
    }
    
    //******************************************************************
    
    bool SortedList::IsPresent( /* in */ ItemType item ) const
    
    // Searches the list for item, reporting whether it was found
    
    // Precondition:
    //     length <= INT_MAX / 2
    //  && data[0..length-1] are in ascending order
    //  && item is assigned
    // Postcondition:
    //     Return value is true, if item is in data[0..length-1]
    //                  is false, otherwise
    
    {
        bool found;      // True if item is found
        int position;    // Required (but unused) argument for
                         //   the call to BinSearch
    
        BinSearch(item, found, position);
        return found;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21