Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- //#include <sstream> (used for alternate method)
- //converts an value to a string(used for alternate method)
- /*template <typename T>
- std::string valueToString(T value)
- {
- std::ostringstream str;
- str << value;
- return str.str();
- }*/
- int main()
- {
- ///Start FizzBuzzing
- for (unsigned int i = 1; i <= 1000; i++)
- {
- //create a buffer to build a string
- std::string aBuffer = "";
- //If we hit a given multiple add the appropriate word
- if (i % 3 == 0)
- aBuffer += "Fizz";
- if (i % 5 == 0)
- aBuffer += "Buzz";
- //handle the no conditions case(print line number)
- if (aBuffer == "")
- {
- std::cout << i;
- //alternate code
- //aBuffer = valueToString(i);
- }
- else
- {
- //add ending punctuation
- aBuffer += "!";
- }
- //handle capitalisation (make all leters in the word lowercase except the first)
- for (int i = 1; i < (int) aBuffer.length(); i++)
- {
- aBuffer[i] = tolower(aBuffer[i]);
- }
- //output constructed line
- std::cout << aBuffer << std::endl;
- }//end fizzbuzz for()
- std::cin.get();//pause
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment