Thread: Variable For Loops

  1. #1
    Registered User
    Join Date
    Sep 2010

    Variable For Loops

    I'm try to make a For loop inside a do while loop that varies its (the For's) parameters depending on user input within the while loop. The thing is that it works, but it crashes once the programs finishes. Here's the code:

    do {
    for (i = in1; i <in2; ++i) {
    if (condition=answer1) {
    else if (condition=answer2) {
    } while (loop=true);
    Basically, Function() prints a std::map with its iterator changing, indirectly, depending on the loop variable "i". I've been playing around with the code a bit, and it works when instead of variables in1 and in2 they are fixed numbers. But when replaced with variables, the program runs, it prints the map and then crashes. The error report comes from windows, its the one that goes "'program.exe' has encountered a problem and needs to close. Sorry for the inconvenience." I'm sure I'm doing something I'm not supposed to. Any help is much appreciated.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Surely you're using == instead of = in all your if statements?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    For starters, you should format your code properly, e.g.,
    do {
        for (i = in1; i < in2; ++i) {
        if (condition = answer1) {
            in1 += 10;
            in2 += 10;
        else if (condition = answer2) {
            loop = false;
    } while (loop = true);
    Then, you should fix the cases where you use = when it looks like you intended to use ==.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Nothing in the portion of code you've posted can cause a crash.
    As for Whatever's in 'Function' and the rest of the code though, who knows.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Sep 2010
    Here whats in Function(). I didn't copy it because I used the same coding in other programs( as is) and didn't get any problems, so I thought it wasn't necesary.

     if ((*pos).first != "") {
                    cout<<" ("<< (*pos).second<<") Name: "<<(*pos).first<<endl;
    @tabstop: Yes. I'm using ==. For the sake of laziness, I didn't copy the code exactly as is.

    Another thing, I don't understand iterators that well. At first when I wrote my code I tried to make the iterator pos go accordingly to the loop variable "i" but I didn't know how. Finally, I ended up using ++pos ( which work perfectly well for what I'm trying to do). Anybody can tell me how to access an nth element in a map?

    EDIT: @iMalc: I think I know what's wrong with the program. I changed the in1 and in2 variables to set numbers like 10 and 20 in the For loop, respectively, and it worked perfectly well.
    Last edited by kocmohabt33; 01-31-2011 at 04:00 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Realloc problem, environment variables Linux C
    By yodakohl in forum C Programming
    Replies: 4
    Last Post: 01-05-2011, 07:04 PM
  2. Pointers
    By MrMatt027 in forum C++ Programming
    Replies: 14
    Last Post: 12-10-2010, 04:32 PM
  3. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 01:57 PM
  4. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  5. Replies: 2
    Last Post: 04-12-2004, 01:37 AM