-1

I'm trying to make a calculator, that calculates the amount of pancakes you can make. You input the amount of eggs, milk and flour you have and it should output the amount of pancakes you can make, based on the values you input dived by the minimum amounts you need.

To do that I have to find the smallest value you input, i tried using the min() function, but the code fails to compile. I tried following some tutorials, but it just doesn't work and I don't know what am I doing wrong. I also can't really make out anything out of the error message the compiler throws at me. (I use cpp.sh to compile my code).

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main() {

int eggAmount;
int eggMinimum = 1;
int milkAmount;
int milkMinimum = 200; // ml
int flourAmount;
int flourMinimum = 100; // g

cout << "C++ BLIN CALC v0.1 - Ported by @xzotikk\n";
cout << "C++ BLIN CALC IS STARTING UP...\n";

cout << "How many eggs do you have?\n";
cin >> eggAmount;

cout << "How much milk do you have?\n";
cin >> milkAmount;

cout << "How much flour do you have?\n";
cin >> flourAmount;

bool killSwitch;

if (eggAmount < eggMinimum) {
    cout << "You need at least one egg!\n";
    killSwitch = true;
} else {
    killSwitch = false;
}

if (milkAmount < milkMinimum) {
    cout << "You need at least 200ml of milk!\n";
    killSwitch = true;
} else {
    killSwitch = false;
}

if (flourAmount < flourMinimum) {
    cout << "You need at least 100g of flour!\n";
    killSwitch = true;
} else {
    killSwitch = false;
}

if (eggAmount < eggMinimum || milkAmount < milkMinimum || flourAmount < flourMinimum) {
    cout << "Press A to exit.\n";
} else {
    int eggDiv = eggAmount / eggMinimum;
    cout << "You have " << eggDiv << " eggs to use.\n";

    int milkDiv = milkAmount / milkMinimum;
    cout << "You have " << milkDiv << " portions of milk.\n";

    int flourDiv = flourAmount / flourMinimum;
    cout << "You have " << flourDiv << " portions of flour.\n";

    cout << min(eggDiv, milkDiv, flourDiv);
}

char killSwitchButton;

cin >> killSwitchButton;

if (killSwitch == true) {
    switch (killSwitchButton) {
        case 'a':
            cout << "C++ BLIN CALC IS SHUTTING DOWN...";
            return 1;
            break;
    }
}

}

Here is the error message:

In file included from /usr/include/c++/4.9/bits/char_traits.h:39:0,
                     from /usr/include/c++/4.9/ios:40,
                     from /usr/include/c++/4.9/ostream:38,
                     from /usr/include/c++/4.9/iostream:39,
                     from 1:
    /usr/include/c++/4.9/bits/stl_algobase.h: In instantiation of 'const _Tp& std::min(const _Tp&, const _Tp&, _Compare) [with _Tp = int; _Compare = int]':
    63:40:   required from here
    /usr/include/c++/4.9/bits/stl_algobase.h:243:26: error: '__comp' cannot be used as a function
           if (__comp(__b, __a))
                              ^
cigien
  • 57,834
  • 11
  • 73
  • 112

2 Answers2

5

If you want the minimum of more than 2 values, use the initializer_list overload from <algorithm>:

std::cout << std::min({eggDiv, milkDiv, flourDiv});
cigien
  • 57,834
  • 11
  • 73
  • 112
  • Nice! I didn't know about that one. – Retired Ninja Jul 21 '20 at 17:32
  • It worked, thanks! But I don't really understand, why do I have to use std:: if I already put using namespace std; in my code. –  Jul 21 '20 at 17:35
  • Technically, you don't need `std::` if you do that, but you should *never* do that anyway, it's a bad habit. I'm so used to it, I just automatically typed `std::` :) See https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – cigien Jul 21 '20 at 17:37
  • If this answers the question, consider [accepting](https://stackoverflow.com/help/someone-answers) it. – cigien Jul 21 '20 at 17:41
0

Use the function std::min defined for std::initializer_list

cout << min( { eggDiv, milkDiv, flourDiv });
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335