Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

I Need Help Getting Driver 1 To run And Driver 2 To Run

+1 vote
asked Oct 27, 2021 by Terrence D Oliver (130 points)

// Using the given code for ItemType.h, ItemType.cpp, Unsorted.h and Unsorted.cpp and below 2 files for driver:

// Driver 1

#include <iostream>

#include "ItemType.h"

#include "Unsorted.h"

using namespace std;

int main(void) {

Unsorted UL;

ItemType IT;
ItemType IT2;
IT.Initialize(5);

bool TestF = UL.IsFull();
// this should be TestF not Test
cout << TestF << endl; //should print 0 :false

UL.InsertItem(IT);
UL.InsertItem(IT2);

cout << UL.LengthIs() << endl;//should print 2
system("pause");

}

// end of Driver 1

// Driver 2:

#include <iostream>
#include "ItemType.h"
#include "Unsorted.h"
using namespace std;

int main(void) {

Unsorted UL;

// Create an ItemType called MyDay and initialize it with your day of birth and put it in the list UL.
ItemType MyDay;
MyDay.Initialize(10);
UL.InsertItem(MyDay);

// Create an ItemType called MyMonth and initialize it with your month of birth and put it in the list UL.
ItemType MyMonth;
MyMonth.Initialize(7);
UL.InsertItem(MyMonth);

// Create an ItemType called MyYear and initialize it with your year of birth and put it in the list UL.
ItemType MyYear;
MyYear.Initialize(1995);
UL.InsertItem(MyYear);

// Create an ItemType called MyT and initialize it with the last four digits of your TNumber and store it in UL.
ItemType MyT;
MyT.Initialize(4017);
UL.InsertItem(MyT);

// Call the member function LengthIs() and display the length of the list after all these insertions.
cout << "Length: " << UL.LengthIs() << endl;

// display the elements in list
cout << "List: " <<endl;
UL.ResetList(); // reset list
ItemType item;

// loop over the list, displaying elements
for(int i=0;i<UL.LengthIs();i++)
{
UL.GetNextItem(item); // get next item in item
item.Print(); // display item
}

bool found;

// call the member function RetrieveItem() to retrieve your month of birth.
UL.RetrieveItem(MyMonth, found);

// display found or not found based on found value
if(found)
cout << "Found" << endl;
else
cout << "Not found" << endl;

// call the member function DeleteItem() to delete your Tnumber digits.
UL.DeleteItem(MyT);

// Display the elements of the list after this deletion.
cout << "List: " <<endl;
UL.ResetList(); // reset the list

// loop over the elements of the list, displaying elements
for(int i=0;i<UL.LengthIs();i++)
{
UL.GetNextItem(item);
item.Print();
}

system("pause");

return 0;

}

1 Answer

+1 vote
answered Oct 27, 2021 by Peter Minarik (63,340 points)

You can share the whole project of yours ("Share Project" button).

Based on the code you shared I don't see what your problem is.

// this should be TestF not Test
cout << TestF << endl; //should print 0 :false

This means nothing to me. What is TestF or Test? Surely you mean more than just a simple rename of variables.

Could you please share the whole project (as suggested above) and give a more detailed description of the problem you're facing?

Also, not a problem in regards to code compilation, but a problem regarding OOP concepts: you shouldn't have a separate "Initialize" function. The constructor should be used to initialize the instance of the class.

commented Oct 27, 2021 by Terrence D Oliver (130 points)
commented Oct 27, 2021 by Terrence D Oliver (130 points)
These are my task im trying to complete

a)    Run the driver as is and take a screen shot of its execution. driver 1 should produce 0 & 2
b)    Modify the driver file as follows:
a.    Create an ItemType called MyDay and initialize it with your day of birth  and put it in the list UL.
b.    Create an ItemType called MyMonth and initialize it with your month of birth  and put it in the list UL.
c.    Create an ItemType called MyYear  and initialize it with your year of birth and put it in the list UL.
d.    Create an ItemType called  MyT and initialize it with the  last four digits of your TNumber and store it in UL.
e.    Call the member function LengthIs() and display the length of the list after all these insertions.
f.    Call the member function GetNextItem() to display the elements in your list (you need to reset the list first and use a loop to go through the elements of the list).
g.    call the member function RetrieveItem() to retrieve your month of birth.
h.    call the member function DeleteItem() to delete your Tnumber digits.
i.    Display the elements of the list after this deletion.
Submit the code of the driver only with the two  screen shots of your program execution.
commented Oct 27, 2021 by Peter Minarik (63,340 points)
Sorry, you didn't understand me.

I cannot run your code as it's missing headers and source files. If you just copy the same code you shared (headers, source files still missing) that wouldn't help.

Can you share the whole project (that compiles and runs) and include a better explanation of the problem?

This way I could play around and try why the code misbehaves.
commented Oct 27, 2021 by Terrence D Oliver (130 points)
I'M SORRY , Im Going To Show You Everything that was given to me

-Divide the text into 4 portions corresponding to Unsorted.h, Unsorted.cpp, ItemType.h and ItemType.cpp

-In your IDE, first: create a class called Unsorted and paste the code corresponding to Unsorted.h, Unsorted.cpp. Second, in the same project, add a class called ItemType and paste the corresponding code.

UNSORTED LIST FILES BELOW

pragma once
#include "ItemType.h"
//Unsortedspecification file (Unsorted.h)
class Unsorted
{ // declares a class data type
public:            // 8 public member functions

   Unsorted();
   bool   IsFull() const;
   int   LengthIs() const; // returns length of list
   void RetrieveItem(ItemType& item, bool& found);
   void InsertItem(ItemType item);
   void DeleteItem(ItemType item);
   void ResetList();
   void GetNextItem(ItemType& item);

   private:               // 3 private data members

       int        length;
       ItemType   info[MAX_ITEM];
       int       currentPos;
   };


------------------------------------------------------------------
#pragma once
#include "ItemType.h"
#include "Unsorted.h"

//UnsortedType specification file (UnsortedType.h)
// IMPLEMENTATION FILE ARRAY-BASED LIST ( unsorted.cpp )


Unsorted::Unsorted()
// Pre: None.
// Post: List is empty.
{
   length = 0;
}

void Unsorted::InsertItem(ItemType item)
// Pre: List has been initialized. List is not full.
// item is not in list.
// Post: item is in the list.
{
   info[length] = item;
   length++;
}

int Unsorted::LengthIs() const
// Pre: List has been inititalized.
// Post: Function value == ( number of elements in
// list ).
{
   return length;
}
void Unsorted::RetrieveItem(ItemType& item, bool& found)
// Pre: Key member of item is initialized.
// Post: If found, item’s key matches an element’s key in the list
// and a copy of that element has been stored in item;
// otherwise, item is unchanged.
{
   bool moreToSearch;
   int location = 0;

   found = false;
   moreToSearch = (location < length);
   while (moreToSearch && !found)
   {
       switch (item.ComparedTo(info[location]))
       {
       case LESS:
       case GREATER: location++;
           moreToSearch = (location < length); break;
       case EQUAL: found = true;
           item = info[location];
           break;
       }
   }
}


bool Unsorted::IsFull() const
// Pre: List has been initialized.
// Post: Function value == ( list is full ).
{
   return (length == MAX_ITEM);
}

void Unsorted::DeleteItem(ItemType item)
// Pre: item’s key has been inititalized.
//   An element in the list has a key that matches item’s.
// Post: No element in the list has a key that matches item’s.
{
   int location = 0;

   while (item.ComparedTo(info[location]) != EQUAL)
       location++;

   // move last element into position where item was located

   info[location] = info[length - 1];
   length--;
}

void Unsorted::ResetList()
// Pre: List has been inititalized.
// Post: Current position is prior to first element in list.
{
   currentPos = -1;
}

void Unsorted::GetNextItem(ItemType& item)
// Pre: List has been initialized. Current position is defined.
//   Element at current position is not last in list.
// Post: Current position is updated to next position.
// item is a copy of element at current position.
{
   currentPos++;
   item = info[currentPos];
}

================================================================================

#pragma once
// SPECIFICATION FILE       ( itemtype.h )

const int MAX_ITEM = 5;
enum RelationType { LESS, EQUAL, GREATER };
class ItemType       // declares class data type
{
public:            // 3 public member functions
   RelationType ComparedTo(ItemType) const;
   void           Print() const;
   void      Initialize(int number);
   ItemType::ItemType();
   ~ItemType();


private:       // 1 private data member
   int   value; // could be any different type
};
------------------------------------------------------------------------
#include "ItemType.h"
#include <iostream>
using namespace std;

// IMPLEMENTATION FILE       ( itemtype.cpp )
// Implementation depends on the data type of value.


ItemType::ItemType() {
   value = 1;
}
RelationType ItemType::ComparedTo(ItemType otherItem) const
{
   if (value < otherItem.value)
       return LESS;
   else if (value > otherItem.value)
       return GREATER;
   else return EQUAL;
}
void ItemType::Print() const
{
   cout << value << endl;
}

void ItemType::Initialize(int number)
{
   value = number;
}


ItemType::~ItemType()
{
}
--------------------------------------------------------------------------
// Driver file

#pragma once
#include <iostream>
#include "ItemType.h"
#include "Unsorted.h"
using namespace std;

int main(void) {
   Unsorted UL;
   ItemType IT;
ItemType IT2;
   IT.Initialize(5);

   bool TestF = UL.IsFull();
   cout << Test << endl; //should print 0 :false

   UL.InsertItem(IT);
   UL.InsertItem(IT2);
  
   cout << UL.LengthIs() << endl;//should print 2
   system("pause");
  

}
commented Oct 27, 2021 by Peter Minarik (63,340 points)
edited Oct 27, 2021 by Peter Minarik
I put all the sources into a single project. (And fixed some compilation errors.)

https://onlinegdb.com/SGLzgPCGs

Your problem is that you have two main() functions. You must have exactly one.

To have both Driver1 and Driver2 run, you can move them into their own separate functions and create a new main() that calls both of them one by one.

See the shared code above.

Is this what you were after?
commented Oct 27, 2021 by Terrence D Oliver (130 points)
THANK YOU Mr.Manarik!
commented Oct 27, 2021 by Peter Minarik (63,340 points)
You're welcome.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...