Incorrect Double Outputs

I'm new to c++ and relatively new to programming so there's probably an easy fix though I can't seem to figure it out. This is a program I have to do for class that's almost done. I've been beating my head against my desk trying to get the double equations in my code to produce the correct results. I've been at this problem for days and I've tried to input the double equations in several different ways and this is just the current way I have them. For example, for double poundPowderedSugar = (batchesMade / ((double)(11 / 5))); when I test 3 for batchesMade, I should be getting a double output of 1.363636, with a ceiling output of 2, but instead I get 1.04167 with a ceiling output of 2. When I had it just as double poundPowderedSugar = (batchesMade / 2.2); I was getting an output of less than 1. Thanks!

The following is the part of my program that's causing me trouble:

#include <iostream>
#include <cmath>
#include <string>
using namespace std;

string pluralize(string singular, string plural, int number) {
if (number == 1) {
return singular;

}
return plural;
}

int main() {
int peopleServed = 0;
cout << "How many people do you need to serve? ";
cin >> peopleServed;
cout << endl;

double batchesMade = (((double)(peopleServed)) / 12);

double flourBag = (batchesMade / ((double)(13 + (1 / 3))));
double sugarBag = (batchesMade / 10.0);
double poundOfButter = (batchesMade / ((double)(1 + (1 / 3))));
double ozSourCream = (batchesMade / 2.0);
double dozenEggs = (batchesMade / 12.0);
double poundPowderedSugar = (batchesMade / ((double)(11 / 5)));
double ozVanilla = (batchesMade / ((double)(2 + (2 / 3))));

cout << "You need to make: " << ceil(batchesMade) << " ";
cout << pluralize("batch", "batches", batchesMade) << " of cupcakes.";
cout << endl << endl;

cout << "Shopping List for \"Best Ever\" Vanilla Cupcakes" << endl;
cout << "----------------------------------------------" << endl;
cout << ceil(flourBag) << " " << pluralize("bag", "bags", ceil(flourBag));
cout << " of flour" << endl << endl;
cout << ceil(sugarBag) << " " << pluralize("bag", "bags", ceil(sugarBag));
cout << " of granulated sugar" << endl << endl;
cout << ceil(poundOfButter) << " " << pluralize("pound", "pounds", ceil(poundOfButter));
cout << " of butter" << endl << endl;
cout << ceil(ozSourCream) << " " << pluralize("container", "containers", ceil(ozSourCream));
cout << " of sour cream" << endl << endl;
cout << ceil(dozenEggs) << " dozen eggs";
cout << endl << endl;
cout << ceil(poundPowderedSugar) << " " << pluralize("bag", "bags", ceil(poundPowderedSugar));
cout << " of powdered sugar" << endl << endl;
cout << ceil(ozVanilla) << " " << pluralize("bottle", "bottles", ceil(ozVanilla));
cout << " of vanilla" << endl << endl;

return 0;
}
Last edited on
 
(double)(11 / 5)

You are dividing the integer 11 with the integer 5 which will give you the integer 2. Casting it to a double afterwards will not give you the fractional part back.

Instead you need to cast at least one of the two operands to a double before doing the division

 
((double) 11 / 5)

or you could simply use double literals by adding decimal dots to the numbers.

 
(11.0 / 5.0)
Last edited on
Thank you so much! I knew it was a simple mistake that I just couldn't see in front of me.
Topic archived. No new replies allowed.