-2
void DrawStairs() {
// Declare Variable i
int i = 0;

// while loop 
while (i == 0){
    //Increment Variable 
    i + 1;
    //Print # (Should print 8 times but it's not)
    cout << "#\n";
    if (i == 8)
        break;        
}
printf("\n\n\n\n\n\n\n\n\n\n%d", i);

The code above on compiler explorer just prints # forever. If i change the if to if (i = 8) Then it will break when i = 8 but it will only print # once. I want it to print # 8 times. How do I accomplish this?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 2
    although you are trying to increment the variable in `i+1` you are not assigning in to anything (no lvalue). so the statement doesn't do anything. It should be `i = i + 1;` or `i ++;` When you type i = 8 in the if statement, it assigns 8 to the variable i instead of doing a logical comparison. Since it's a non zero value, the if statement evaluates to true and breaks. Effectively, the loop executes only once. '=' is the assignment operator and '==' is the logical comparison operator. It would be helpful if you could go through the fundamentals. – Malith Sep 02 '20 at 18:20
  • 1
    [The Definitive C++ Book Guide and List](https://stackoverflow.com/q/388242/5910058) – Jesper Juhl Sep 02 '20 at 18:30
  • BTW, after the variable `i` is incremented, it is no longer equal to zero, so your loop terminates after 1 iteration. – Thomas Matthews Sep 02 '20 at 18:32
  • @Thomas But `i` is never actually incremented in OPs code. – Jesper Juhl Sep 02 '20 at 18:34

4 Answers4

2
i + 1;

This evaluates i + 1, and then does nothing with it. Most importantly, it does not assign the result of that evaluation to anything. You most likely intended to do this...

i = i + 1;

...which can be shortened to...

i += 1;

...or, for the specific case of adding one...

++i;

Additionally:

int i = 0;

while (i == 0){
    ++i;
    // ...

    if (i == 8)
        break;
}

...is somewhat broken, because your while condition becomes false after the first iteration. From the context it becomes clear that this was a convoluted way to say...

int i = 0;

while ( i != 8 ) {
    ++i;

    // ...
}

...which is probably better expressed as...

for ( int i = 1; i != 8; ++i ) {
    // ...
}

Note that i = 1 in my last example, as your while loop increments i once (from 0 to 1) before doing anything with it.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • In the OP's `while` loop, the variable `i` will (should be) incremented, and thus `i` will not be equal to zero, so the loop terminates after one iteration. – Thomas Matthews Sep 02 '20 at 18:34
2

This code does NOT increment your i:

//Increment Variable 
i + 1;

try:

++i;
Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27
1

All you need to do is set the while loop to be while (i < 8) and change the i statement. You then can get rid of the if statement and break;

For example:

 void DrawStairs()
{
    // Declare Variable i
    int i = 0;

    // while loop 
    while (i < 8) {
        //Increment Variable 
        ++i;
        //Print # (Should print 8 times but it's not)
        std::cout << "#\n";
        
       
    }
    
}
Geno C
  • 1,401
  • 3
  • 11
  • 26
0

The original loops forever because you don't change the variable i

This line does nothing:

    //Increment Variable 
    i + 1;

Here you are creating a new value i + 1 but this is immediately forgotten because you don't assign it to anything. I think what yo wanted to do is:

    //Increment Variable 
    i = i + 1;    // or i += 1; or i++; or ++i;

This line will only be true the first iteration of the loop (when i is actually zero).

while (i == 0){

After the first loop has run then i will no longer be zero and the loop will exit. The reason it currently runs forever is that you don't change i so it stays at zero.

Since you want to get to 8 change this to:

while (i < = 8) {

But you can make this neater by using a for() loop:

void DrawStairs() {

    for (int i = 0; i <= 8; ++i){
        //Print # (Should print 8 times but it's not)
        cout << "#\n";
    }
}
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • Your final example doesn't compile. But assuming you fixed the syntax error, it would print 9 times. – scohe001 Sep 02 '20 at 18:30
  • @scohe001 This is a great example of why comments (like this (that explain the code)) are bad. Save your comments for "WHY" you are doing something. Is the code correct or the comments! Which is normative and thus which do you fix? The code above emulates the behavior of the original code (or the expected behavior of the code based on fixing the bugs). Without accesses to the specifications I must assume the whoever wrote the comments did so incorrectly. :-) The code is the definitive definition of what the code does. – Martin York Sep 02 '20 at 20:17
  • @scohe001 As for not compiling: That' because it was a snipit. There was more missing than just the closing brace! But have added the closing brace so it compiles. – Martin York Sep 02 '20 at 20:22
  • I'd say the combination of both comment and title make it pretty clear what the expected behavior is, but if you believe the question or expected behavior is unclear, I'd tell you to vote to close as unclear instead of writing a possibly useful/possibly wrong answer :) – scohe001 Sep 02 '20 at 21:22