A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

Repetitiveness

947 views · started by 323 ·
#1
Repetitiveness
What, in your opinion, is the easiest way of removing the repetitiveness of this FizzBuzz program?

I hate having to repeat if statements, it annoys me.

Also, general fizzbuzz thread.


#include <iostream>

using namespace std;

int main() {
int count = 1;
do {
if(!(count%3)) {
if(!(count%5)) {
cout << "FizzBuzz" << endl;
}
}
if(!(count%3)) {
cout << "Fizz" << endl;
} if (!(count%5)) {
cout << "Buzz" << endl;
} else {
cout << count << endl;
}
count++;
} while(count<100);
}


Also, if you think my code looks bad, then suck a dick because I did it in Vim because I'm too lazy to install a real text editor. So I don't have syntax highlighting, and compilation errors in g++ are cryptic at best.

Anyway, yep. Anyone know any other programming interview challenges that are fun?
#2
You don't have to end the line with every cout.

if (!(count % 3))
cout << "Fizz";
if (!(count % 5))
cout << "Buzz";
if (count % 3 || count % 5)
cout << count;
cout << endl;


Also you don't need syntax highlighting to properly space your code. I did this in the quick reply box. :P
#3
GAMEchief wrote:
You don't have to end the line with every cout.

if (!(count % 3))
cout << "Fizz";
if (!(count % 5))
cout << "Buzz";
if (count % 3 || count % 5)
cout << count;
cout << endl;


Also you don't need syntax highlighting to properly space your code. I did this in the quick reply box. :P


Oh hey, that's a pretty good idea, just appending the buzz onto it if it needs it, or printing the number if it's neither, that's pretty sweet. +10lgg

And yeah, good point about the formatting haha.
#4
Using a do while loop is a tad redundant, particularly since you're just incrementing . You're better off using a for loop.