lundi 11 novembre 2019

How to do a "if... then, but also if.... then but if neither do...."

I have made a simple application to play the game FizzBuzz in c++, although this isn't necessarily a c++ question, what is the best way to make an if statement so it does the following:

if(x) then: a, if(y) then: b, if(x) and if(y) then: a and b, if neither then:

this is my current approach:

#include <iostream>
using namespace std;

int main ()
{
    int factor1 = 7;
    int factor2 = 15;
    bool found = false;
    string factor1_word = "Fizz";
    string factor2_word = "Buzz";
    int n = 1000;
    for (int i = 1; i < n+1; i++)
    {
        if (i % factor1 == 0)
        {
            cout << factor1_word;
            found = true;
        }
        if (i % factor2 == 0)
        {
            cout << factor2_word;
            found = true;
        }
        if (found == 0)
        {
            cout << i;
        }
        cout << endl;
        found = false;
    }
    return 0;
}

which works but I feel like it isn't the most readable nor the best way to do it.

Any advice would be great, thanks.

Aucun commentaire:

Enregistrer un commentaire