nesting for loops

hey, i am having trouble with nesting for loops and the output makes no sense to me, i have done this all with while loops and it works fine but my teacher asks for for loops so im setting it all to for loops and for some reason the output it weird
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <cstdlib> 

//data structure for shapes
struct shape{
    int height;
    int length;
    int reverb;
};
// function to produce a triangle
void triangle_1(int height);
// function to produce the 2nd triangle
void triangle_2(int height, int reverb);
// function to produce square
void square_shape(int width);

int main(){
    // defines shapes 
    shape triangle, square, triangle2;
    // into and prompts user for the length of square 
    std::cout << "*** The Incridible Shape Drawing Program ***\n";
    std::cout << "Let's draw a rectangle please enter a number for the width between 2 and 20: ";
    std::cin >> square.length;
    // length of square size requirements
    for (square.length = square.length; square.length < 2 || square.length > 20; NULL) {
        std::cout << "You entered an invalid number...\n Enter a new number: ";
        std::cin >> square.length;
    }
    //prompts user for height of square 
    std::cout << "Please enter a number for height between  1 and 10, (but less than the width: ";
    std::cin >> square.height;
    // height of square size requirements
    for (square.height = square.height; square.length > square.height && (square.height > 10 || square.height < 1); NULL) {
        std::cout << "You entered an invalid number...\n Enter a new number: ";
        std::cin >> square.height;
    }
    // formating
    std::cout << '\n';
    //using square_shape function and a fore loop to make the square
    for (square.height = square.height; square.height > 0; --square.height){
        square_shape(square.length);
        std::cout << '\n';
    }
    //prompts user for height of triangle_1
    std::cout << "Lets draw  triangle #1\nPlease enter a number for the height between 2 and 10: ";
    std::cin >> triangle.height;
    // height of triangle_1 requirements
    for(triangle.height = triangle.height; triangle.height < 2 || triangle.height > 10; NULL){
        std::cout << "You entered in an invalid number...\n Enter a new number: ";
        std::cin >> triangle.height;
    }
    //formating
    std::cout << '\n';
    //for loop which creates triangle_1
    for(triangle.length = 1; triangle.height > 0; ++triangle.length && --triangle.height){ 
    triangle_1(triangle.length);
        std::cout << '\n';
    }
    // prompts user for height of triangle2
    std::cout << "Lets draw triangle #2\nPlease enter a number for the height between 2 and 20: ";
    std::cin >> triangle2.height;
    // height of triangle2 requirements
    for(triangle.height = triangle2.height; triangle2.height < 2 || triangle2.height > 10; NULL){
        std::cout << "You entered in an invalid number...\n Enter a new number: ";
        std::cin >> triangle2.height;
    }
    //formating
    std::cout << '\n';
    //for statement to create triangle_2
    for(triangle2.length = 1 && (triangle2.reverb = triangle2.height - 1); triangle2.height > 0;
        --triangle2.reverb && ++triangle2.length && --triangle2.height){ 
        triangle_2(triangle2.length,triangle2.reverb);
        std::cout << '\n';
    }
        return 0;
}

void triangle_1(int height){
    //for loop to make the width of triangle_1
    for(height = height; height != 0; --height){
        std::cout << "*";
    }
}    

void triangle_2(int height, int reverb){
    //for loop to make the width of triangle_2 empty sparce
    for(reverb = reverb; reverb != 0; --reverb){
        std::cout << " "; 
    }
    //for loop to make the width of triangle_2 
    for(height = height; height != 0; --height){
        std::cout << "*";
        }
}

void square_shape(int width){
    // for loop to make the width of the square
    for(width = width; width != 0; --width){
        std::cout << "*";
    }
}


the output for this is
*** The Incridible Shape Drawing Program ***
Let's draw a rectangle please enter a number for the width between 2 and 20: 5
Please enter a number for height between  1 and 10, (but less than the width: 5

*****
*****
*****
*****
*****
Lets draw  triangle #1
Please enter a number for the height between 2 and 10: 5

*
**
***
****
*****
Lets draw triangle #2
Please enter a number for the height between 2 and 20: 5

    *
   **
  ***
 ****
****

the last star is missing and the new line is entered, at this point the program is still running and does not change eny output, help me plz.
In a for loop: for ( a; b; c), a should be used for initializing the counter that controls loop execution, b is the expression that must be true for the loop to continue and c is the expression that updates the counter. You're making your code difficult to read by abusing the for loop construct.

operator&& is not for chaining statements. The way operator&& works for:
--triangle2.reverb && ++triangle2.length && --triangle2.height

is:

evaluate --triangle2.reverb: If it is 0, don't evaluate the other two expressions.
evaluate ++triangle2.length: If it is 0, don't evaluate the last expression.
evaluate --triangle2.height.

And reverb does reach 0 during your loop, and you do feed it to triangle_2 with a negative value.

Btw, triangle2.length = 1 && (triangle2.reverb = triangle2.height - 1) happens to be the same as:
triangle2.length = (1 && (triangle2.reverb = triangle2.height - 1)) which, fortunately for you, always evaluates to 1.
im not on the comp with the code atm, but if i put the ++triangle2.length and the --triangle2.height at the within the for loop, instead of within the for(here) so it would be more like
for(stuff){move them here then other code also}
would that work? because the problem is that i have to manually shut down the program each time i run it because it gets stuck in one of my loops, and misses one of the stars. and ty for the help.
Last edited on
it worked, thank you for the help :)
Topic archived. No new replies allowed.