Computing.Net > Forums > Programming > C++ iterate over a constant set

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

C++ iterate over a constant set

Reply to Message Icon

Name: Judago
Date: July 22, 2009 at 22:36:18 Pacific
OS: Windows XP
CPU/Ram: Athlon xp x2 64 5600 / 2gb
Product: Home built / K9a2 cf-f
Subcategory: C/C++
Comment:

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?



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: July 23, 2009 at 11:17:45 Pacific
Reply:

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))

0

Response Number 2
Name: Judago
Date: July 23, 2009 at 17:21:58 Pacific
Reply:

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.


0

Response Number 3
Name: Razor2.3
Date: July 23, 2009 at 19:28:33 Pacific
Reply:

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?


0

Response Number 4
Name: Judago
Date: July 23, 2009 at 20:10:12 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Extract from newest .log ... Plnik Output Code Bat fil...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ iterate over a constant set

How do I iterate in a file? (C++) www.computing.net/answers/programming/how-do-i-iterate-in-a-file-c/13915.html

Just over a year ago www.computing.net/answers/programming/just-over-a-year-ago/14634.html

Sending data over a phone line Help www.computing.net/answers/programming/sending-data-over-a-phone-line-help/11462.html