Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8768569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:06:10+00:00 2026-06-13T17:06:10+00:00

I have been looking at this piece of code for the last 3 hours

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:06:11+00:00Added an answer on June 13, 2026 at 5:06 pm

    The problem looks to be in your ComparedTo member function:

    RelationType ItemType::ComparedTo(ItemType otherItem){
    
        if(value < otherItem.value){
            return LESS;
        }
    
        if(value == otherItem.value){
            return EQUAL;
        }
    
        if(value < otherItem.value){
            return GREATER;
        }    
    
    }
    

    The comparison for the GREATER case 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., use if, 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been looking over this code for the past hour, I cant see
I have been looking at this for to long so I am tossing it
I have been looking all over for this. I have two seperate prpt files
I have been looking for an answer to this on Stack Overflow, but I
I have been looking for some time for this and I can't seem to
I have been looking for an answer to this and could not find it
I have been looking at the answer to this question: Pulling details from response
I have been looking around the web for this but cannot really find a
I have been looking everywhere but could not find a tutorial for this. I
I have been looking over the internet for a while about this, but it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.