Thread: Broken for loop in c++

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    7

    Broken for loop in c++

    edit: got the code formatted a little better. Tabs are replaced by single spaces, but at least its halfway readable now

    This'll be a long post, so I'll start with just the gist of the problem, and fill in more and more detail throughout the post.

    And just to get this out of the way, I'm a self learner and a beginner, not a professional or a student, so there may/will be things I am expected to know but don't, about style and such, or certain terms for things I'm not familiar with.
    So basically, I'm having problems with a simple for loop that looks like this:
    Code:
    for (int z = index; z >= 0; z--) {
           //do this
           //do that
       }

    with the variable "index" being an int that takes as its value the size of a vector I am using, minus 1.
    The idea is to count down from whatever index is and select each element in the vector and do stuff with it.
    Instead, this for loop will take some weird number as its index variable, as if Id assigned z to a random number. It then increments the index variable instead of decrementing it, as if I had z++ instead of z--. There is no other statement anywhere in this program that uses any variable called "z" for anything, much less an assignment statement for it other than the "int z = index" in the header of the for loop itself.
    I have tested it a lot. It doesn't matter what I do, I cannot get this for loop to work properly. Here is the entire for loop.
    And I don't know what's wrong with this forum or with my post, but its putting extra code tags behind the word "for" and I cant get it to quit doing it, and then its getting the tabs and spacing of the code all jacked up to where its hard to see how things fit together.

    Code:
     for (int z = index; z >= 0; z--) {
      cout << "requests.size() - 1 == " << requests.size() - 1 << ", but z == " << z << endl;
      cout << "requests.size() == " << requests.size() << ", selecting requests[" << z << "]. . ." << endl;
      if (requests.size() == 0) break;
      int currentRequest = requests[z];
      //cout << "currentRequest == " << currentRequest << endl;
      requests.remove(z);
      //cout << "for (int j = remaining.size() - 1; j >= 0; j--). . ." << endl;
      for (int j = remaining.size() - 1; j >= 0; j--) {
       cout << "remaining[" << j << "]. . ." << endl;
       if (remaining[j] >= currentRequest) {
        //cout << "remaining[j] >= currentRequest" << endl;
        if (requests.size() <= 0) {
         //cout << "requests.size() <= 0" << endl;
         pipes.add(remaining.size());
        } else {
         remaining[j] -= currentRequest;
         cut(requests, remaining, pipes);
         remaining[j] += currentRequest;
        }
       }
      }
      int newPipe = PIPE_LENGTH - currentRequest;
      remaining.add(newPipe);
      cut(requests, remaining, pipes);
      remaining.remove(remaining.size() - 1);
     }

    So I don't expect anyone to read through that garbled mess, it is easier to read in my compiler :/
    Anyway, its part of a function that calls itself to do cool stuff. I'm supposed to be learning about recursive backtracking, and I've already done 9 exercises in this chapter, and hundreds more before I got to this chapter of the book (Chapter 9 of "Programming Abstractions in C++" by Eric Roberts). In all of that time, I have never had an issue with for loops. Everything else in my crappy programs can fall apart, but if nothing else, at least the language and the compiler always work, and I can spend hours debugging and it always comes back to something I did wrong, never the language or compiler, so this problem is shaking me up a little bit.
    Now, at this point I wanted to copy-paste a little bit of the output from the console, but I cant seem to copy-paste it. But its showing that "index" is given the value -1, and the very next statement is the for loop. The for loop begins, and shows values of z starting with 2, then 3, then 4, on up to 7, which isn't anywhere near what its supposed to be. It isn't even going in the right direction, its supposed to be decrementing by 1, not incrementing!
    As for that, Ive tried changing the "z--" to "z -= 1" and then "z = z - 1" and it wont work(and if it HAD work, I wouldn't feel any better, because as Ive said, up to now I have never had a problem with a for loop)
    I have tried assigning the variable z to index before the for loop, as follows:
    Code:
     int z = index;
     for(; z >= 0; z--) {
         //stuff
     }
    of course that didn't work.
    I can even have
    Code:
     int z = index;
     cout << "z before for loop: " << z << endl;
     for(; z >= 0; z--) {
         cout << "z: " << z << endl;
         //other stuff
     }
    and it will show z to be the proper value as assigned before the for loop, but then in the for loop itself, its showing some weird number like 2 to start with, then incrementing instead of decrementing, and then terminating at another weird number like 6 or 7!
    Next I'll post the entire program. I wouldn't think anyone would want or need to read through all of it, but here it is, just so I can know I haven't withheld any information.

    edit: the broken for loop begins on line 69

    Code:
    #include <iostream>
    
    #include "console.h"
    #include "vector.h"
    #include "simpio.h"
    #include "foreach.h"
    
    using namespace std;
    
    //global variables
    //constants
    int PIPE_LENGTH = 10;
    
    //prototypes
    int cutStock(Vector<int> &requests);
    void cut(Vector<int> &requests, Vector<int> remaining, Vector<int> &pipes);
    
    //main
    int main() {
     for (int i = -5; i < 0; i++) {
      cout << "i: " << i << endl;
     }
     int x = 0;
     int stockLength = PIPE_LENGTH;
     Vector<int> requests;
     while (true) {
      while (true) {
       x = getInteger("x: ");
       if (x <= 0) break;
       requests.add(x);
      }
      if (requests.size() <= 0) break;
      cout << cutStock(requests) << " stock pipes needed." << endl;
      requests.clear();
     }
     return 0;
    }
    
    //functions
    int cutStock(Vector<int> &requests) {
     int result = -1;
     Vector<int> remaining;
     Vector<int> pipes;
     cut(requests, remaining, pipes);
     //cout << "pipes.size() == " << pipes.size() << endl;
     for (int i = 0; i < pipes.size(); i++) {
      //cout << "pipes[" << i << "] == " << pipes[i] << endl;
      if (pipes[i] < result || result == -1) {
       result = pipes[i];
      }
      //cout << pipes[i] << endl;
     }
     return result;
    }
    
    void cut(Vector<int> &requests, Vector<int> remaining, Vector<int> &pipes) {
     if (requests.size() <= 0) pipes.add(remaining.size());
     int index = requests.size() - 1;
     cout << "for (int i = requests.size() - 1; i >= 0; i--). . ." << endl;
     cout << "requests.size() - 1 == " << requests.size() - 1 << endl;
     cout << "index ........ing == " << index << endl;
     for (int i = index; i >= 0; i--) {
      cout << "i ........ing == " << i << endl;
     }
     int i = index;
     cout << "*****************" << endl;
     cout << "i == " << i << endl;
     cout << "*****************" << endl;
     for (int z = index; z >= 0; z--, cout << "zinfor: " << z << endl) {
      cout << "requests.size() - 1 == " << requests.size() - 1 << ", but z == " << z << endl;
      cout << "requests.size() == " << requests.size() << ", selecting requests[" << z << "]. . ." << endl;
      if (requests.size() == 0) break;
      int currentRequest = requests[z];
      //cout << "currentRequest == " << currentRequest << endl;
      requests.remove(z);
      //cout << "for (int j = remaining.size() - 1; j >= 0; j--). . ." << endl;
      for (int j = remaining.size() - 1; j >= 0; j--) {
       cout << "remaining[" << j << "]. . ." << endl;
       if (remaining[j] >= currentRequest) {
        //cout << "remaining[j] >= currentRequest" << endl;
        if (requests.size() <= 0) {
         //cout << "requests.size() <= 0" << endl;
         pipes.add(remaining.size());
        } else {
         remaining[j] -= currentRequest;
         cut(requests, remaining, pipes);
         remaining[j] += currentRequest;
        }
       }
      }
      int newPipe = PIPE_LENGTH - currentRequest;
      remaining.add(newPipe);
      cut(requests, remaining, pipes);
      remaining.remove(remaining.size() - 1);
     }
    }
    "simpio.h", "vector.h", and "console.h" are part of the Stanford C++ libraries that go with the book I'm using, and that students taking CS courses at Stanford use. I've been using them throughout the book so far, and done loads of exercises using them, and while I have had some issues here and there, never anything like this. I'm 99.999% sure they do not reprogram the frickin for loop somehow.
    The "vector.h" is just a simplified version of the vector in the c++ standard library, but its not exactly the same thing. Its supposedly simplified in such a way that the student will be able to implement it later in the book, though I haven't gotten that far just yet. In any case, I haven't had any trouble with it, and its been involved in plenty of for loops doing all kinds of odd stuff. So I don't think that its the problem either.
    My compiler is Microsoft Visual Studio 2008 Express.
    The exercise is to create a program that takes as input from the user several numbers (int) greater than 0 and less than or equal to 10. The program should output the minimum number of stock pipes of length 10 you must have to be able to get a pipe of each length input by the user. Say you gave it 3, 3, and 3, it would give you 1 stock pipe because its length is 10, and you can cut 3 lengths of 3 from it and have 1 left over. For two 6s, you would need two stock pipes, etc. I don't want to know how to do this, the idea is for me to figure that part out, I just want my for loop to work.
    Any help would be appreciated. Im gonna move on from this particular problem anyway. Im a completionist but f's sake, just this one thing is holding me back, Im a full week at least behind where I would be if I hadn't got hung up on this lol
    Or actually no. Im gonna just axe the for loop and turn it into a while loop with a break, but I hope to god that doesn't work, because that would mean I cant even use for loops anymore, Im hoping its just a silly thing I messed up like it always has been, I like knowing that every problem is fixable, keeps me from just skipping to the next problem whenever I have a hard time debugging something.
    Anyway, if anyone actually read this far, thanks. The post is probably too long. Ive never done this before, so I don't really know exactly how much information to give.. so I gave.. like... all of it.
    Last edited by Stark_Barksalt; 10-15-2013 at 12:42 AM. Reason: crappy looking code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rep Broken?
    By B0bDole in forum A Brief History of Cprogramming.com
    Replies: 27
    Last Post: 02-04-2005, 07:00 AM
  2. Broken While Loop
    By GamingMarvel in forum C++ Programming
    Replies: 6
    Last Post: 01-10-2005, 12:46 PM
  3. broken search can u fix? plz :)
    By */*/*/*/ in forum C++ Programming
    Replies: 2
    Last Post: 06-07-2003, 04:43 PM
  4. broken logic
    By ggs in forum C Programming
    Replies: 4
    Last Post: 03-17-2002, 04:11 PM