First of all, I'm new to Daniweb and I would like to thank in advance to all those who take their precious time to help me. I appreciate it greatly. Hopefully, I'll get good enough to contribute and help others in the Daniweb developing community, as well.

The problem I'm having has to do with, as suggested in the thread title, variable for loops. I'm making a function the prints out an std::map, 10 containers at a time using a for loop inside a while loop. Heres the code:

...
LoadMap();
map<string, int>const_iterator pos;
int in1(1),in2(11);
bool ib1;

do {
        for (i = in1; i <in2; ++i)
            {
                if (map::container != empty)
                cout<< (*pos)->second << (*pos)->first<<endl;
                ++pos;
                }
                
            getline(cin, line);
            if (statement1 == true)) {
                    in2+=10;
                    in3+=10;}
            if (statement2 == true) {
                    ib1 = false;}

            } while (ib1 = true);
...

To be clear, LoadMap() loads the map from a .txt file succesfully and I might have missed declaring some variables in the code above, but they're all declared in the program. Statement1 more or less asks if the user wants to continue to the next page, and statement2 if he wants to exit.

Now here's the thing: the program works. It prints out the contents of the std::map. The problem is that it crashes without letting me input anything. Now I've played a little bit with the code, and made everything work perfectly fine by replacing the variables in1 and in2 with set numbers like 1 and 11, respectively. So, again, to be clear, having variable parameters in the for loop is whats crashing my program. I don't know why its doing that, and I could really use some help. Again, truckloads of appreciation and gratitude for helping me on this.

Recommended Answers

All 11 Replies

Member Avatar for embooglement

I'm not sure what's causing your issue, but I do know one thing for certain: this is not your code. What you posted is riddled with syntax errors that wouldn't compile at all. I'd recommend you literally copy and paste what you have, or if you're worried about intellectual property or something, write a new program that guts your current program and boils it down to what is needed to reproduce the error, and then copy and paste that.

I'm not sure what's causing your issue, but I do know one thing for certain: this is not your code. What you posted is riddled with syntax errors that wouldn't compile at all. I'd recommend you literally copy and paste what you have, or if you're worried about intellectual property or something, write a new program that guts your current program and boils it down to what is needed to reproduce the error, and then copy and paste that.

Heres the code as is, without gutting. I didn't put the load map function, but it loads succesfully.

void PrintReg()
{
    string line;
    map<string,int>::iterator pos;
    pos = Registry.begin();
    int i, in1(1), in2(11),a;


    do {
        for (i = in1; i <in2; ++i)
            {
                if ((*pos).first != "") {
                cout<<" ("<< (*pos).second<<") Name: "<<(*pos).first<<endl;
                ++pos;
                }
            }

            cout<<"View Next page? ";
            getline(cin, line);
            if ((line=="y") || (line=="yes"))
                {
                     in1+=10;
                     in2+=10;}
                        } while (line != "exit");

}

The error report comes froms windows. the one that says "Program.exe has encountered a problem and needs to close. We are sorry for the inconvenience."

BTW, I lived a couple of years in Pullman, Wa.

BTW, I lived a couple of years in Pullman, Wa.

That's your problem. There are different programming rules for the rest of the world :icon_twisted: (just kidding)


Now, explain to me what the line int i, in1(1), in2(11),a; does, and how the variables relate to the line for (i = in1; i <in2; ++i)

Heres the code as is, without gutting. I didn't put the load map function, but it loads succesfully.

Maybe so, but I believe, as embooglement also believes, the code posted doesn't even compile. So back up and tell us what the real problem is.

Member Avatar for embooglement

Actually, this latest code looks good to me, and I think it compiles, assuming Registry is some static variable some where. So whatever the problem is, it's a logic error. I'd recommend running through the program with breakpoints and checking what each value being iterated through is, and that it matches what you expect it to be.

And so far as living in Pullman, I'm sorry to here that. Hopefully you've been able to put that tragedy behind you.

(I live in Seattle, and am currently a student at UW, so I'm somewhat obligated to hate on Pullman. No hard feelings?)

Actually, this latest code looks good to me, and I think it compiles, assuming Registry is some static variable some where. So whatever the problem is, it's a logic error. I'd recommend running through the program with breakpoints and checking what each value being iterated through is, and that it matches what you expect it to be.

And so far as living in Pullman, I'm sorry to here that. Hopefully you've been able to put that tragedy behind you.

(I live in Seattle, and am currently a student at UW, so I'm somewhat obligated to hate on Pullman. No hard feelings?)

@embooglement:Registry is the std::map. Its a <string, int> map. When you mean each value, you mean the printed map values? If so, yes, they are what I expected them to be. The program prints everything like its supposed to do, but its now allowing me the first input at line 19, before it crashes. I'm wondering if it doesn't have anything to do with the compiler or maybe my computer?

As for living in Pullman, I contemplated the possibility that you were from Seattle and thought twice about posting where I'm from. Its cool though :icon_cheesygrin:.

@WaltP: i is the "loop variable" its the one that increases in the for loop. in1 es the initial value of i, and it changes depending on user imput. If the user want to print the rest of the map values, it increases in1 by 10, so the next time it runs the for loop instead of being 1 its 11. Same applies for in2, except its the last value of i, so if the user wants to see more map values, its increased by 10. Thats what lines 22 and 23 do.

Thanks people for all the help. I'm still having troubles sorting this out, I have no idea whats causing it to crash.

Member Avatar for embooglement

Well, I finally copied and pasted with into my compiler to see exactly what's happening, and I think I got it. First off, your problem is that depending on the size of the map, and the values of in1 and in2, you sometimes go out of the bounds of the container. So I'd recommend adding an additional if statement checking to see that the iterator is still within bounds.

The other thing is that incrementing in1 and in2 doesn't do anything. The thing that's important is the distance from one another, so adding 10 to both of them won't matter. If in1 is 0, and in2 is 5, then the for loop runs 5 iterations. If in1 is 10 and in2 is 15, then the for loop is still going to have 5 iterations. I tested this by commenting out the lines that increment them, and it still works as expected.

Member Avatar for embooglement

I went through your code and fixed it up a bit. Here it is.

void PrintReg()
{
    string line;
    map<string,int>::iterator pos;
    pos = Registry.begin();

    int pageSize = 0;

    cout << "Page Size? ";
    cin >> pageSize;
	
    do 
    {
    	for (int i = 0; i < pageSize; ++i)
    	{
            if (pos != Registry.end())
	    {
		if (pos->first != "") 
		{
		    cout << i << " : " << " (" << pos->second << ") Name: " << pos->first << endl;
		    ++pos;
		}
	    }
	    else
	    {
		cout << "I'm sorry Dave. I'm afraid I can't do that." << endl;
		break;
	    }
	}

	cout << "View Next page? ";
	cin.clear(); cin.sync();
	getline(cin, line);
			
    } while ((line=="y") || (line=="yes"));
}

I'm fairly certain this is what you're going for. It lets the user specify how many elements to display, then displays that many each time they say "yes". When it runs out of elements, it quotes HAL, and then repeats the while loop.

@WaltP: i is the "loop variable" its the one that increases in the for loop.

Good.

in1 es the initial value of i, and it changes depending on user imput. If the user want to print the rest of the map values, it increases in1 by 10, so the next time it runs the for loop instead of being 1 its 11.

Then why did you implement it as a function prototype? int in1(1); is not defining a variable in1, it's specifying a function (notice the parentheses).

What you want is int in1=1;

Same applies for in2, except its the last value of i, so if the user wants to see more map values, its increased by 10. Thats what lines 22 and 23 do.

Yes, the same applies.

And, embooglement, at these forums we don't "fix up a bit" the code of others. We give help and let them fix the code. We have enough trouble with their professors getting POd at the students because someone hands them their assignment on a silver platter.

Member Avatar for embooglement

I'm sorry WaltP. I didn't mean to offend you in any way. Truth is I actually admire you quite a bit. The way you spend your time dispensing less than helpful advice, and making grand assumptions about the skills of others. I mean, honestly, this guy from Pullman doesn't deserve help. What he needs is for us to vaguely point him in the general direction kind of.

It's funny, every time I turn to the C++ forums for help, I end up getting replies that are either completely unrelated to my question, critiques of my programming in general, or vague statements that are in no way helpful. I'd recommend looking through what you've posted here, and asking if it really helps kocmohabt33. Because the fact of the matter is, programming is hard, and C++ programming is even harder. Some people need a little more than a gentle push in the right direction, and giving them something to look at and understand is better than trying to get them to magically come to the knowledge on their own. If you were learning to speak a new language, you would want to see examples of that language's idioms in actual use, not have people tell you to look up the definition of "past participles" and see if it lines up with your understanding or something like that.

This forum has the capacity to really help people, but honestly from what I can see, most of the C++ experts have a massive superiority complex. This guy needs help, and I want to give it to him. And while I did post code for him, I didn't give him the answers. There are still massive flaws in what I posted, I just wanted to illustrate what I was saying. And if I were in his situation, that's exactly what I would have wanted to see. Not so that I could copy and paste it, but so I could see what someone else is thinking, and understand it. That's how I learned.

commented: you are right about your vision of daniweb +2

Some people need a little more than a gentle push in the right direction, and giving them something to look at and understand is better than trying to get them to magically come to the knowledge on their own. If you were learning to speak a new language, you would want to see examples of that language's idioms in actual use, not have people tell you to look up the definition of "past participles" and see if it lines up with your understanding or something like that.

This forum has the capacity to really help people, but honestly from what I can see, most of the C++ experts have a massive superiority complex. This guy needs help, and I want to give it to him. And while I did post code for him, I didn't give him the answers. There are still massive flaws in what I posted, I just wanted to illustrate what I was saying. And if I were in his situation, that's exactly what I would have wanted to see. Not so that I could copy and paste it, but so I could see what someone else is thinking, and understand it. That's how I learned.

Thats exactly correct, embooglement. I understand that its not right to "fix" other people's code, but in my case its not an assignment so you can hand me all the code you want. In fact, thats usually how I learn. From trial and error, using already written code I change variables or something and see what happens. You made a very good observation too that I didn't need to go from 1 to 11 and then from 11 to 21. I'm going to get to work on it and see what happens. Thanks a lot, embooglement. Hopefully it will work out. If not, I'll post what happens on this thread.

Sorry for the double post, I didn't know how to edit. I think you can't edit. Anywho, embooglement: your code works, but for some reason they're not being printed in order. This is the order I'm getting (supposing there are 8 entires in the map):

(1) Name: Annie
(3) Name: Bill
(4) Name: Dan
(2) Name: Nancy
(5) Name: Nicole
(7) Name: Benny
(6) Name: Caroline
(8) Name: Darren

Is there a reason for that? Is it possible to print them in order?

I put the same if statement that checks to see its not the last value of the map in my program, and it crashed. Then I went back to your code to compare and I saw that that if statement was first, and in mine it was second. So, I changed the order of the if statements and it worked AWESOME! :icon_cool:except for the order thing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.