What is wrong with my code (C++)

soo i am learning C++ and now i am at the FizzBuzz thing and i wrote my code and idk what’s wrong can you help

my code

#include <iostream>

int main() {

int i = 0;

for (i > 100; i++) {

    std::cout << i <<"\n";

} if ( i % 3 == 0) {

  std::cout <<"fizz\n";

} else if ( i % 5 == 0) {

  std::cout <<"buzz\n";

} else (i % 5 == 0 && i % 3 == 0) {

  std::cout <<"fizzbuzz\n";




Hello @tarekSabouni31522108, welcome to the forums!
It seems you have a couple of errors in your code:

Here, the loop will run when i is greater than 100. But since i starts at 0, the loop will never run. It seems C++ isn’t too fond of the way you initialise i outside of the loop, although I don’t quite know the reason for that.

An else statement shouldn’t have a condition after it. The purpose of an else is to run for all other conditions.

You might also want to check the way you’ve structured the logic (conditional statements) in your code.

Also, in future posts, make sure to format your code correctly; it just makes it easier for people to read! :smiley:

#include <iostream> int main() { //same as a while loop int i = 0; for (;i < 5;) { std::cout << i; i++; } return 0; }
1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.