#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int numberOfPeople;
int cookiesPerBatch = 12;
double numberOfBatches = (numberOfPeople/cookiesPerBatch);
int main() {
cout << "How many people do you need to serve ?" << endl;
cin >> numberOfPeople;
cout << "You need to make:" << ceil(numberOfBatches) << "batch of cookies" << "\n\n\n\n\n";
I am trying to calculate the number of batches of cookies needed for a certain amount of people. When I plug in any number into numberOfPeople, I get 0 numbers of batches every time. What am i doing here?
try a cast here: ((double)numberOfPeople/cookiesPerBatch);
and re-read what ceil() does if you are not happy with the output after the cast.
remember that 10/12 = 0.0 because its integer math, and int/int drops the decimal, so 10/12 is zero, ceil(0) = 0.
(what you have will work if you put in 50 people, I think, but it will be off by one for the same reason, it loses the decimal so the +1 batches you need to cover everyone is lost).
you can also do this with % instead of double/ceil ideas. If you wanted to go there, its not 'better'.
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
double numberOfPeople;
double cookiesPerBatch = 12.0;
double numberOfBatches = ((double)numberOfPeople/cookiesPerBatch);
int main() {
cout << "How many people do you need to serve ?" << endl;
cin >> numberOfPeople;
cout << "You need to make:" << ceil(numberOfBatches) << "batch of cookies" << "\n\n\n\n\n";
you assign numberOfBatches before you read in numberOfPeople. you can't divide the numbers and get the value before you know what the numbers are.
if you make it a double, you don't need to cast it to a double, it already is one.
cast (double)int/int or just use
double/int or int/double or double/double