user1800666
user1800666

Reputation: 13

For loops are skipped, why?

I have an assignment in which i have to create a console program in c++ that draws Hexagons in a given style. The problem i am having is; my For loops are never entered and i can't figure out why. here's the snippet of code I'm having trouble with.

    void display()
{
    int counter=0;//var that keeps track of the layer that is being drawn
    for(int i=0;i>=size;i++)//spaces before first layer of hexagon
    {
        cout<<" ";
    }
    for (int k=0; k>size;k++)//top layer of hexagon
    {
        cout<<"_";
    }
    cout<<endl;//ends the first layer
    for (counter; counter>=size-1;counter++)//outer loop for the top half that controls the size
    {
        for( int j=0;j>(size-counter);j++)//adds spaces before the shape
        {
            cout<<" ";
        }
        cout<<"/";
        for( int p=0; p>(size+(counter*2));p++)//loop for the hexagon fill
        {
            cout<<fill;
        }
        cout<<"\\"<<endl;
    }
    for(counter;counter==0;counter--);  //loop for bottom half of the hexagon
    {
        for( int j=0;j>(size-counter);j++)//adds spaces before the shape
        {
            cout<<" ";
        }
        cout<<"\\";
        for( int p=0; p>(size+(counter*2));p++)//loop for the hexagon fill
        {
            cout<<fill;
        }
        cout<<"/"<<endl;
    }
    cout<<"\\";
    for(int r=0; r>=size;r++){cout<<"_";}
    cout<<"/"<<endl;

}

the 'Size' and 'fill' are selceted earlier in the program during my main() I'm probably missing something very simple but I've been struggling with this for a while. Any help would be greatly appricated!

Upvotes: 1

Views: 4055

Answers (6)

jimvonmoon
jimvonmoon

Reputation: 341

I am not sure what is the content of your size variable but it looks like you have got your loop conditions wrong:

for(int i=0;i>=size;i++)

probably should be:

for(int i=0;i<size;i++)

The same goes for other loops.

Upvotes: 3

Sebastian Mach
Sebastian Mach

Reputation: 39089

for loops in C++ are while loops, not until loops.

C++ has only while loops (with the meaning of as long as):

for (int i=0; i<10; ++i)
    ....


int i=0;   
while (i<10) {
    ....
    ++i;
}

Upvotes: 1

kenor
kenor

Reputation: 1910

Aren't all of your < and > reversed? Because

(int k=0; k>size;k++)

makes no sense to me.

Upvotes: 1

Jonathan Muller
Jonathan Muller

Reputation: 7516

In your conditions, invert the > to <

< means inferior, you want to do a

for i = 0; if i < size; i++

You do

for i = 0 ; if i > size ; i ++ 

if size is superior to i (0) the loop will never trigger

Upvotes: 1

P.P
P.P

Reputation: 121357

Assuming your size is a postive number, it works as per your condition. Change the > conditojs to < in your conditions.

Upvotes: 1

cyon
cyon

Reputation: 9538

Your loops use > and start at 0. It seems you want < instead. For example

for(int i=0;i<size;i++)//spaces before first layer of hexagon
{
    cout<<" ";
}

Upvotes: 4

Related Questions