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;
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.