Liam Sheffer
Liam Sheffer

Reputation: 1

Break statement not exiting for loop

I'm trying to make a for loop so that when there is an item for sale and an item wanted with a lower price, it removes the two listings and exits the for loop using 'j'. For some reason though, the value of askingPrice is staying the same, and removing all items with a lower price, instead of just removing the first one it encounters, and breaking the loop. Is there a different way I should be exiting the loop, or do I just have a logic error somewhere?

    for(int i = 0; i < elements; i++){
    if(itemArray[i].status == "for sale"){
        int askingPrice = itemArray[i].price;
        string ITEM = itemArray[i].type;
        for(int j = 0; j < elements; j++){
            SOLD = 0;
            position = 0;
            if(itemArray[j].status == "wanted" && itemArray[j].type == ITEM && itemArray[j].price >= askingPrice){
                soldArray[soldPosition].type = itemArray[j].type;
                soldArray[soldPosition].price = askingPrice;
                soldPosition += 1;
                //cout << soldPosition << endl;
                if(i > j){
                for(int k = i; k < elements; k++){
                    itemArray[k].price = itemArray[k+1].price;
                    itemArray[k].type = itemArray[k+1].type;
                    itemArray[k].status = itemArray[k+1].status;
                    if(k == elements - 2){
                        elements = elements - 1;
                    }
                }
                position = 1;
                }
                for(int k = j; k < elements; k++){
                    if(k == elements - 1){
                        elements = elements - 1;
                        break;
                    }
                    itemArray[k].price = itemArray[k+1].price;
                    itemArray[k].type = itemArray[k+1].type;
                    itemArray[k].status = itemArray[k+1].status;
                }
                if(position = 0){
                for(int k = i; k < elements; k++){
                    if(k == elements - 1){
                        elements = elements - 1;
                        break;
                    }
                    itemArray[k].price = itemArray[k+1].price;
                    itemArray[k].type = itemArray[k+1].type;
                    itemArray[k].status = itemArray[k+1].status;
                }
                }
                SOLD = 1;
            }
            if(SOLD == 1){
                i = i-2;
                break;
            }
        }
    }
}

Upvotes: 0

Views: 1245

Answers (3)

Umair Ahmad
Umair Ahmad

Reputation: 11

For some reason though, the value of askingPrice is staying the same

You are not changing askingPrice anywhere in your code.

when there is an item for sale and an item wanted

An itemArray object cannot have both status "for sale" and "wanted" at the same time.

So lets say if(itemArray[i].status == "for sale") is true in i loop. The control goes into the if statement and initializes askingPrice and ITEM with ith item's price and type.

From there, j loop is started from 0th element in itemArray. Your j loop has no regard for status = "for sale", this loop only knows the price and type. At this point SOLD = 0;

Now control checks this

if(itemArray[j].status == "wanted" && itemArray[j].type == ITEM && itemArray[j].price >= askingPrice)

Please Note that above if can either be true or false. But in any case it is not considering

status = "for sale"; Therefore, when the above if condition is true, it runs the code within and deletes low priced item according to the condition and when it is false, the code never reaches SOLD = 1; which does not allow j loop to break as if(SOLD == 1) break;

So there you have it and yes you are right, it is indeed a logical error.

Upvotes: 0

awiebe
awiebe

Reputation: 3836

break only breaks out of the innermost loop. There are 2 ways you can conquer this problem:

  1. Set a flag and break out of each subsequent loop if it is triggered.
  2. Extract functions from your program and distinguish single loop breaks from total loop breaks using return to break form multiple associated loops vs break for a single loop. For larger problems always use this method, it will make your code easier to read, well structured and more maintainable.

Upvotes: 1

Casey
Casey

Reputation: 10936

Either use a sentinel value and sequentially break out of each loop. (The modern way of doing things) OR Cause a massive wave of "You're doing it wrong!" screaming and just use a goto statement. I advocate goto only if you are nesting for loops on 4th or 5th order or higher. If that's the case: "YOU'RE DOING IT WRONG!"

Upvotes: 0

Related Questions