c++for-loopstdio

Why is my FizzBuzz program in C++ not working correctly?


I was creating the FizzBuzz program in C++, where you print 1-100 and replace multiples of 3 with "Fizz", multiples of 5 with "Buzz" and multiples of both 3 and 5 with "FizzBuzz".

Although on running the code, I receive no errors, but the numbers 6, 21, 36 etc. don't get replaced with "Fizz" whereas the numbers 10, 25, 40 etc. don't get replaced with "Buzz".

I don't understand why this is happening.

My code is given below:

#include <stdio.h>
#include <iostream>
using namespace std;

int main() 
{
    
    for (int i = 1; i < 101; i++)
    {
        
        if ((i%3) == 0 && (i%5) == 0)
        {
            printf("FizzBuzz ");
            i++;
        }
        else if ((i%3) == 0)
        {
            printf("Fizz ");
            i++;
        }
        else if ((i%5) == 0)
        {
            printf("Buzz ");
            i++;
        }
        
        printf("%d ", i);

    }

    return 0;

}

In my opinion, the expected output should be(using code from Geeks for Geeks):

[Running] cd "d:\Projects\VS Code\Jetlag\helloworld\" && g++ fzz.cpp -o fzz && "d:\Projects\VS Code\Jetlag\helloworld\"fzz
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz 
[Done] exited with code=0 in 6.043 seconds

This is the exact output I get:

[Running] cd "d:\Projects\VS Code\Jetlag\helloworld\" && g++ FizzBuzz.cpp -o FizzBuzz && "d:\Projects\VS Code\Jetlag\helloworld\"FizzBuzz
1 2 Fizz 4 Buzz 6 7 8 Fizz 10 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz 21 22 23 Fizz 25 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz 36 37 38 Fizz 40 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz 51 52 53 Fizz 55 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz 66 67 68 Fizz 70 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz 81 82 83 Fizz 85 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz 96 97 98 Fizz 100 
[Done] exited with code=0 in 0.987 seconds

As can be seen above, some numbers do not get replaced.

Any help would be appreciated.


Solution

  • There are several issues in your code:

    1. You increment i both in the for loop statement and under the ifs. Since you need to increment it in each iteration, the proper place is just in the for statement.

    2. The last print for the number itself should be under an else block (following the previous if/else ifs): you should print the number itself only if the previous cases handled by the if/else ifs (for "FizzBuzz", "Fizz" and "Buzz") did not hold, and this is what the final else is for (see in the fixed version below).

    3. Although it can work, it is not recommended to use C I/O in C++. Instead you can use std::cout from <iostream> that you #included anyway. An even more modern way (from C++23) would be to use std::print.

    4. It is recommended to avoid using namespace std;. See here: What's the problem with "using namespace std;"?.

    A fixed version:

    #include <iostream>
    
    int main()
    {
        for (int i = 1; i < 101; i++)
        {
            if (((i % 3) == 0) && ((i % 5) == 0)) {
                std::cout << "FizzBuzz ";
            }
            else if ((i % 3) == 0) {
                std::cout << "Fizz ";
            }
            else if ((i % 5) == 0) {
                std::cout << "Buzz ";
            }
            else {  // this block will be executed only if the previous cases did not hold
                std::cout << i << " ";
            }
        }
    }
    

    Live demo