Trouble finding differential between two items in C++

86 views Asked by At

Let me preface this by saying I'm still extremely new to C++ and want to keep things as simple as possible. I'm also pretty terrible at math.

Mostly, I'm looking to see if anyone can help my code so it will always give the correct result. I've mostly got it to do what I want, except in one scenario.

My code is trying to find out how many packages of hotdog weiners and how many packages of hotdog buns someone has purchased. Then it tells the user how many hotdogs they can make from that as well as how much leftover weiners or buns they would have. Assuming a package of weiners contains 12 and a package of buns contains 8, this is what I have come up with so far:

#include <iostream>
#include <cmath>

using namespace std;

void hotdog(int a, int b){      //a = weiner packages, b = bun packages
    int weiners = 12 * a;
    int buns = 8 * b;
    int total = (weiners + buns) - (weiners - buns);
    int leftOverWeiners = total % weiners;
    int leftOverBuns = total % buns;
    int totalHotDogs = total / 2;

    cout << "You can make " << totalHotDogs << " hotdogs!" << endl;

    if (leftOverWeiners > 0){
        cout << "You have " << leftOverWeiners << " weiners left over though." << endl;
    }else if (leftOverBuns > 0){
        cout << "You have " << leftOverBuns << " buns left over though." << endl;
    }
}

int main(){
    int a;
    int b;

    cout << "Let's see how many hotdogs you can make!" << endl;
    cout << "How many weiner packages did you purchase?: ";
    cin >> a;
    cout << "How many bun packages did you purchase?: ";
    cin >> b;

    hotdog(a, b);

    return 0;
}

With this, I can always get the correct answer if the ratio of buns to weiners is the same or if there are more weiners than buns.

Because of the way I've set up total and/or leftOverBuns (lines 9, 11), I will never get the correct answer to how many left over buns there will be. I know there must be a simpler way to do this if not a way to modify my current code but I am stumped.

I know I left virtually zero notation, so if you would like some please let me know!

2

There are 2 answers

0
Ediac On BEST ANSWER
#include <iostream>

void hotdog( int weinerspackages, int bunspackages ){
    const int weinersPerPackage = 12;
    const int bunsPerPackage = 8;
    const int totalweiners = weinerspackages * weinersPerPackage;
    const int totalbuns = bunspackages * bunsPerPackage;
    int leftoverweiners = 0;
    int leftoverbuns = 0;
    int amountOfHotdogs = 0;

    if( totalweiners > totalbuns ){
        leftoverweiners = totalweiners - totalbuns;
        amountOfHotdogs = totalbuns;
        leftoverbuns = 0;
    }
    else if( totalbuns > totalweiners ){
        leftoverbuns = totalbuns - totalweiners;
        amountOfHotdogs = totalweiners;
        leftoverweiners = 0;
    }
    else{
        amountOfHotdogs = totalweiners;
        leftoverweiners = 0;
        leftoverbuns = 0;
    }

    std::cout << "You can make: " << amountOfHotdogs << " Hotdogs" << std::endl;
    std::cout << "Leftover Weiners: " << leftoverweiners << " || Leftover Buns: " << leftoverbuns << std::endl;
}

int main(){
    int PackagesW = 8;
    int PackagesB = 12;

    hotdog( PackagesW, PackagesB );

    system("pause");
    return 0;
}

Note: It is possible to do this with less variables, I declared this amount of variables to make it easier to understand what the numbers represent.

Assuming that it only takes one of each to make a hotdog, you can find which of the ingredients you have the least, and the amount of hotdogs you can make will be limited by the amount of that ingredient, that is why amountOfHotdogs takes the value of the lesser one. If both are equal in amount, then amountOfHotdogs can take the amount of either.

Only the ingredient with the larger amount will have leftovers, therefore leftoverweiners = totalweiners - totalbuns; when totalweiners > totalbuns and vice-versa.

1
Beta On

You're making it too complicated. Try this:

if(weiners > buns)
{
  cout << "You can make " << buns << " hotdogs!" << endl;
  cout << "with " << weiners-buns << " weiners left over" << endl;
  return;
}
cout << "You can make " << weiners << " hotdogs!" << endl;
if(buns > weiners)
{
  cout << "with " << buns-weiners << " buns left over" << endl;
}

The smaller of {buns, weiners} is the number of hot dogs, and the if-then blocks determine whether the function will report leftover buns or weiners.