OpenSourcePhilosiphe

readable fizzbuzz

Mar 21st, 2018
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. //#include <sstream> (used for alternate method)
  5.  
  6. //converts an value to a string(used for alternate method)
  7. /*template <typename T>
  8. std::string valueToString(T value)
  9. {
  10.     std::ostringstream str;
  11.     str << value;
  12.     return str.str();
  13. }*/
  14.  
  15. int main()
  16. {
  17.     ///Start FizzBuzzing
  18.     for (unsigned int i = 1; i <= 1000; i++)
  19.     {
  20.         //create a buffer to build a string
  21.         std::string aBuffer = "";
  22.  
  23.         //If we hit a given multiple add the appropriate word
  24.         if (i % 3 == 0)
  25.             aBuffer += "Fizz";
  26.         if (i % 5 == 0)
  27.             aBuffer += "Buzz";
  28.  
  29.         //handle the no conditions case(print line number)
  30.         if (aBuffer == "")
  31.         {
  32.             std::cout << i;
  33.  
  34.             //alternate code
  35.             //aBuffer = valueToString(i);
  36.         }
  37.         else
  38.         {
  39.             //add ending punctuation
  40.             aBuffer += "!";
  41.         }
  42.  
  43.  
  44.         //handle capitalisation (make all leters in the word lowercase except the first)
  45.         for (int i = 1; i < (int) aBuffer.length(); i++)
  46.         {
  47.             aBuffer[i] = tolower(aBuffer[i]);
  48.         }
  49.  
  50.         //output constructed line
  51.         std::cout << aBuffer << std::endl;
  52.  
  53.     }//end fizzbuzz for()
  54.  
  55.     std::cin.get();//pause
  56.  
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment