0

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. 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;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
Bizzy
  • 1
  • 2
  • 3
    Just for fun, in cases where you are doing floating point, add `.0` to all of the constants. – Mikel F Jan 17 '17 at 23:04
  • You should get rid of the ridiculous numbers of casts in that code (most of which don't do what you seem to think they do) simply by using double literals - in other words 12.0, instead of 12. In general C++ code should not use C-style casts, or casts at all, except where absolutely necessary, which is not the case here. –  Jan 17 '17 at 23:05
  • (`1` is an integer. It does integer math with other integers. Integer math division truncates to the next closest integer to `0`.) – jaggedSpire Jan 17 '17 at 23:05
  • @MikelF Really it is only needed on just one operand of each division. – NathanOliver Jan 17 '17 at 23:05
  • A cast converts the result of the expression at its right *after it has been calculated*; if the literals involved are integers, casting to double *the result* won't help a bit - the expression will still be calculated using integers. – Matteo Italia Jan 17 '17 at 23:06
  • 1
    You say you've been at this "for days". So what you should have done is take a step back, and see what happens when you break up your calculation into several steps, and wound up doing `1 / 3`. You should have seen that the answer is `0`, thus prompting you to investigate why this is, if not, ask directly why this simple calculation yields 0. – PaulMcKenzie Jan 17 '17 at 23:18

0 Answers0