I normally use the FizzBuzz test and try to see how many different variations I can do to get used to a language when first starting. I've successfully made one FizzBuzz program but I'm struggling with this alteration. Can you have a look at the below and let me know if I'm doing anything majorly wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
int main(){
for (int i = 0; i < 101; i++){
string output;
output = "";
if ( i % 5 == 0 ){ output += "Fizz"; }
if ( i % 3 == 0 ){ output += "Buzz"; }
if ( output == "" ){ output = i }
cout << output << "\n";
}
}
When I compile and run, I get a bunch of random output amongst the FizzBuzz output. Including normal characters and even "[" characters.
But assuming it's a small typo and you simply forgot to put the semi-colon in line 11:
The issue is you're doing output = i;
This isn't doing what you think it's doing. When you assign an integer to a string, it is interpreting that number as an ASCII value, and converting the integer into a character.
For example, 64 is the @ symbol. http://www.asciitable.com/