99 bottles problem


See On Github

Data

Contributor

Generic placeholder thumbnail

by alsuga

in c++

Tags

puzzle, song

Source Code

#include <string>
#include <sstream>
#include <iostream>

using namespace std;

string bottles_of_beer(int n) {
  stringstream out;
  if(n == 1)
    out << "1 bottle";
  if(n == 0)
    out << "no more bottles";
  if(n > 1)
    out << n << " bottles";
  out << " of beer";
  return out.str();
}


#ifndef BOTTLES
#define BOTTLES

/*
** Return the state of the bottle
*/

std::string bottles_of_beer(int n);

#endif
#include <iostream>
#include "99_Bottles_Problem.hpp"

using namespace std;

int main() {
  int max_bottles = 99, bottles = 99;
  string act = "";
  // zero is a false in C/C++
  while(bottles) {
    act = "";
    act += bottles_of_beer(bottles);
    act += " on the wall, ";
    act += bottles_of_beer(bottles);
    act += ".\n";
    act += "Take one down, pass it around, ";
    // substract before of send the variable
    // to te function
    act += bottles_of_beer(--bottles);
    act += " on the wall.";
    cout << act << endl;
  }
  act = "No more bottles of beer on the wall, no more bottles of beer.\n";
  act += "Go to the store and buy some more, ";
  act += bottles_of_beer(max_bottles);
  act += " on the wall.";
  cout << act << endl;
  return 0;
}
CC= g++ -std=c++11

all: bottles

bottles: 99_Bottles_Problem_test.cc
	$(CC) 99_Bottles_Problem.cc 99_Bottles_Problem_test.cc -o bottles.out

clean: 
	rm -rf *.out