Variable For Loops

This is a discussion on Variable For Loops within the C++ Programming forums, part of the General Programming Boards category; I'm try to make a For loop inside a do while loop that varies its (the For's) parameters depending on ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    14

    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:

    Code:
    do {
    for (i = in1; i <in2; ++i) {
           Function();}
    if (condition=answer1) {
    in1+=10;
    in2+=10;}
    else if (condition=answer2) {
    loop=false;}
    } 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
    Posts
    14,185
    Surely you're using == instead of = in all your if statements?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,725
    For starters, you should format your code properly, e.g.,
    Code:
    do {
        for (i = in1; i < in2; ++i) {
            Function();
        }
    
        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 ==.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Location
    New Zealand
    Posts
    6,304
    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
    Posts
    14
    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.

    Code:
     if ((*pos).first != "") {
                    cout<<" ("<< (*pos).second<<") Name: "<<(*pos).first<<endl;
                    ++pos;
                    }
    @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 03: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, 06:04 PM
  2. Pointers
    By MrMatt027 in forum C++ Programming
    Replies: 14
    Last Post: 12-10-2010, 03:32 PM
  3. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 12:57 PM
  4. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  5. Replies: 2
    Last Post: 04-12-2004, 01:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21