C++ iterate over a constant set

Home built / K9a2 cf-f
July 22, 2009 at 22:36:18
Specs: Windows XP, Athlon xp x2 64 5600 / 2gb
I've just started learning c++ and am trying to write a semi-complicated console program. Basically it's just a simple label program(well it doesn't do anything yet).

The very simple (and hopefully easy to answer question) is how can I iterate over a set of constants without using a variable?

All I'm actually trying to do is draw parallel lines at different y or x coordinates with a different number of lines between them. I know I could manually do this, but I'm trying to make things easy to change later.

I'm using Dev-cpp 4.9.9.2 and the windows api(if it's not obvious).

Below is the basic sort of thing I'm trying to do(although the basic question is something I would like to know the question to regardless).

HANDLE winid = GetStdHandle(STD_OUTPUT_HANDLE);
COORD home = {0,0}; 
<iterate over a set of constants(say 2 5 9 18) with variable whatever> {
    home.Y=whatever;
    SetConsoleCursorPosition(winid, home);
    for (int n=1; n<15; n++) {
        cout << (unsigned char)196;
    }
}


Basically I curious as to whether c++ has a function akin to the batch for loop (i.e for %%a in (2 5 9 18) do .....) without using a incrementing for loop over an array.

I will use a variable, array or something if I must but it just seems more clutter to set the use once constants to a variable/array just to loop over the set.

One more thing, does anyone know how to specify a coordinate as a constant values(i.e SetConsoleCursorPosition(winid, {x,y})) rather than a "COORD" data type?


See More: C++ iterate over a constant set

Report •


#1
July 23, 2009 at 11:17:45
how can I iterate over a set of constants without using a variable?
STL has for_each(). It's handy if you're doing work with an STL container, but I don't know of anything in C++ like CMD's FOR.

One more thing, does anyone know how to specify a coordinate as a constant values(i.e SetConsoleCursorPosition(winid, {x,y})) rather than a "COORD" data type?
There is no conversion from an array of type short to a COORD, implicit or explicit.

I suppose if you wanted, you could make a simple struct derived from COORD, which took two shorts in its constructor.

struct coord : public COORD {
  coord(short x, short y) { X = x; Y = y;}
};

Then, you could use temporaries and do SetConsoleCursorPosition(winid, coord(x,y))

Report •

#2
July 23, 2009 at 17:21:58
Thanks Razor2.3,

You've saved me searching endlessly for something that wasn't there. I guess a couple of extra lines won't hurt.

I couldn't get that code for coordinates to work because I haven't learnt struts yet. It will hit me over the next week or so....

Your help is much appreciated, top notch as usual.


Report •

#3
July 23, 2009 at 19:28:33
I couldn't get that code for coordinates to work because I haven't learnt struts yet.
They're like classes, only their members are public (vs. private) by default. What's happening when you try it?

Report •

Related Solutions

#4
July 23, 2009 at 20:10:12
Ok I had another look at it(to replicate what I did last time) and it does indeed work. I made the stupid mistake of testing the code right before the code that re-sizes the window, buffer and window again.

No wonder it didn't seem to work!

Thanks again for your help.

[edit]
To answer your actual question nothing seem to be happening, the cursor would stay where it was.


Report •


Ask Question