I have been looking at this piece of code for the last 3 hours and I am pretty stuck. I appreciate any help, thank you.
file: UnsortedType.h
#include "ItemType.h"
class UnsortedType{
public:
UnsortedType();
void RetireveItem(ItemType& item, bool& found);
bool InsertItem(ItemType item);
private:
int length;
ItemType info[MAX_ITEMS];
};
file: UnsortedType.cpp
#include "UnsortedType.h"
#include <iostream>
UnsortedType::UnsortedType() {
length = 0;
}
void UnsortedType::RetireveItem(ItemType& item, bool& found) {
bool moreToSearch = true;
int location = 0;
found = false;
moreToSearch = (location < length);
while (moreToSearch && !found) {
switch (item.ComparedTo(info[location])) {
case LESS:
location++;
moreToSearch = (location < length);
break;
case GREATER:
location++;
moreToSearch = (location < length);
break;
case EQUAL:
found = true;
break;
}
}
if (found) {
item = info[location];
std::cout << "Item " << item.getValue() << " has been retrieved." << std::endl;
}
else {
std::cout << "Item " << item.getValue() << " has NOT found and has NOT been retrieved."
}
}
bool UnsortedType::InsertItem(ItemType item) {
if (length == MAX_ITEMS) {
std::cout << "List is Full!" << std::endl;
std::cout << "Item " << item.getValue() << " has not been added." << std::endl;
return false;
} else {
std::cout << "Item " << item.getValue() << " added successfully." << std::endl;
info[length] = item;
length++;
return true;
}
}
file: ItemType.h
const int MAX_ITEMS = 40;
enum RelationType{LESS,GREATER,EQUAL};
class ItemType{
private:
int value;
public:
ItemType();
ItemType(int value);
RelationType ComparedTo(ItemType otherItem);
void Initialize(int value);
void printItem();
int getValue();
};
file: ItemType.cpp
ItemType::ItemType(){
this->value=0;
}
ItemType::ItemType(int value){
this->value = value;
}
RelationType ItemType::ComparedTo(ItemType otherItem){
if(value < otherItem.value){
return LESS;
}
if(value == otherItem.value){
return EQUAL;
}
if(value < otherItem.value){
return GREATER;
}
}
void ItemType::Initialize(int value){
this->value = value;
}
void ItemType::printItem(){
std::cout << "Item Type: " << this->value <<std::endl;
}
int ItemType::getValue(){
return this->value;
}
Please note: In the above code, I have ommited some code parts that I think that are not relevant. Therefore if you copy / paste the code and run it may require some include statements ( like iostream ) and so..
Now here is the question:
When I run the main like this:
UnsortedType unsortedType;
bool item3found = false;
ItemType item3(3);
unsortedType.InsertItem(item3);
unsortedType.RetireveItem(item3, item3found);
bool item1found = false;
ItemType item1(1);
unsortedType.InsertItem(item1);
unsortedType.RetireveItem(item1, item1found);
bool item2found = false;
ItemType item2(2);
unsortedType.RetireveItem(item2, item2found);
There are no problems.
The output is:
Item 3 added successfully.
Item 3 has been retrieved.
Item 1 added successfully.
Item 1 has been retrieved.
Item 2 has NOT found and has NOT been retrieved.
However if I first add item1 and retrieve item1, then add item3 and retrieve item3, the switch statement suddenly stops working.
So this is the main file in the weird situation:
UnsortedType unsortedType;
bool item1found = false;
ItemType item1(1);
unsortedType.InsertItem(item1);
unsortedType.RetireveItem(item1, item1found);
bool item3found = false;
ItemType item3(3);
unsortedType.InsertItem(item3);
unsortedType.RetireveItem(item3, item3found);
bool item2found = false;
ItemType item2(2);
unsortedType.RetireveItem(item2, item2found);
While debugging the program I keep myself finding in:
while (moreToSearch && !found)
and the code does not go to ANY of the switch statements. Any idea?
This is the output in the weird situation:
Item 1 added successfully.
Item 1 has been retrieved.
Item 3 added successfully.
RUN FAILED (exit value 1, total time: 1s)
Any help greatly appropriated, I am about to lose it!
The problem looks to be in your
ComparedTomember function:The comparison for the
GREATERcase doesn’t appear to be correct. For such a function, it would make sense that you don’t allow a possible path through it that won’t return a value (i.e., useif,else if,else). Also, you might want to turn on all compiler warnings and treat them as errors; it would help to avoid a problem like this.