0

I wrote a code to manage a coffee machine, I have a function findC that finds the cheapest capsule in the capsule array a different function of mine findVP that is supposed to use the findC function's output as variables. however, when I pass the variables mp, ind = findC(prices_copy, quantities_copy, SIZE); and print them it passes them as 0; but the 2nd cout : cout << findC(prices_copy, quantities_copy, SIZE); prints the correct output. why is this ? and how can I pass the output of the function to another

/******************************************************************************

                                  Online C++ Compiler.
                   Code, Compile, Run and Debug C++ program online.
    Write your code in this editor and press "Run" button to compile and execute it.

    *******************************************************************************/

    // Example program
    #include <iostream>
    #include <string>
    #include <bits/stdc++.h>
    using namespace std;
    #define SLEEVE 10
    #define SIZE 10
    #define N 5
    #define BUDGET 70
    //int CapsuleKind[10] = {"JOE","MAC","NES","jamaica","brazil","columbia","MOJO","CLUB","JHON","COF"};

    float findMostExpensiveCapsule( float prices[], int size ) // 1
    {
    float max = prices[0];
    int count = 0;
    for(int i = 1; i < size; i++)
        {
            if (prices[i] > max)
            {
                max = prices[i];
            }
        }
        cout << "The maximum price  " << max << " is found on indexes:  " ;
        for (int i = 0; i < size; i++)
        {
            if (prices[i] == max)
            {
                cout << i << "   ";
                count++;
            }
            
        }
        cout << endl;
        cout << "The maximum number appears " << count << " times." << endl;
        return max;

    }

    int findStrongestCapsuleInStock( int quantities[], int size, int sleeve ) // 2
    {
        
        return 0;
    }

    void SellCapsules( int quantities[], int Qty, int index) // 10
    {
        quantities[index] = quantities[index] - Qty;
        cout << "SOLD " << Qty << " capsules to the Customer, the total now is: " << quantities[index] << endl;
        
    }

    float findC( float prices[],int quantities[], int size ) // 9
    {
    float min = 99999;
    int count = 0;
    float index=0;
    //sort(prices, arr + n);
    for(int i = 0; i < size; i++)
        {
            if (quantities[i] >= SLEEVE)
            {
                if(prices[i] < min){
                min = prices[i];
                index= i;
                }
                else continue;
            }
        }
        cout <<"the minimum price is : " << min << " ---- the index is : " << index << endl;
        return min, index;
    }

    void findCheapestSleeve( float prices[],int quantities[], int size )
    {
    float min = prices[0];
    int count = 0;
    int index=0;
    for(int i = 0; i < size; i++)
        {
            if (prices[i] < min)
            {
                if(quantities[i] > SLEEVE){
                min = prices[i];
                index= i;
                }
                else continue;
            }
        }
        cout <<"the minimum price is : " << min << " ---- the index is : " << index << endl;

    }

    void showAllCapsulesInStock( int quantities[], float prices[], int size, int sleeve) // 3
    {
        for (int i = 0; i < size; i++)
        {
            cout << "capsule kind: " << i << " ---- sleeves available : " << (quantities[i]/sleeve) << " ----  price(for 1 sleeve): " << (prices[i]*sleeve)<< endl;
        }
    }

    float findVP( float prices[], int quantities[], int size, float nis, int sleeve ) //4
    {
        float mp=0;
        float ind =0;
        float prices_copy[size];
        int quantities_copy[size];
        for(int i=0; i<size; i++){
            prices_copy[i] = prices[i];
            quantities_copy[i] = quantities[i];
        }
        
        mp, ind = findC(prices_copy, quantities_copy, SIZE);
        cout << "The lowest price sleeve is:  " << mp * 10 << " --- the capsule kind is: " << ind <<endl;
        cout << findC(prices_copy, quantities_copy, SIZE);
        
    }

    void findValueForMoneyPackage( float prices[], int quantities[], int size, float nis, int sleeve ) 
    {
        int sleeve_num[size];
        float sleeve_price[size];
        float min=0;
        int index = 0;
        int counter=0;
        float quant = 0;
        for (int i=0; i < size; i++)
        {
            sleeve_num[i] = (quantities[i]/sleeve);
            sleeve_price[i] = (prices[i] * sleeve);
        }
        //min, quant = findCheapestSleeve(sleeve_price, quantities, 10);
        
        cout << "the cheapest sleeve costs : " << min << " and its of kind :" << quant << endl;
        
    }


    void addMoreCapsules( int quantities[], int size ) // 5
    {
        char answer;
        int plus;
        for (int i = 0; i < size; i++)
        {
            cout << "do you want to add capsules to capsule kind " << i << "?  (Y/N) " << endl;
            cin >> answer;
            if (answer == 'Y')
            {
                cout << "How many capsules do you want to add (inter a number) " << endl;
                cin >> plus;
                if (plus > 0)
                {
                    quantities[i] = quantities[i] + plus;
                    cout << "Added " << plus << " capsules to the inventory, the total now is: " << quantities[i] << endl;
                }
            }
            else
            {
                continue;
            }
            
        }
        
    }



    // Driver Code 
    int main() 
    {  
        bool flag = false;
        int option;
        float prices[] = { 1.2, 2.2, 2.5, 1.7, 2.2, 3, 2.8, 2.5, 2.9, 3.7 }; 
        int quantities[] = { 14, 22, 25, 13, 22, 33, 50, 60, 33, 25 };
       
      
        while (flag != true)
        {
            cout << "Please choose an option , has to be a number 1-6" << endl;
            cin >> option;
            if (option == 1)
            {
                findMostExpensiveCapsule(prices,SIZE);
            }
            
            else if ( option == 3)
            {
                showAllCapsulesInStock(quantities, prices, SIZE, 10);
            }
            else if (option == 4){
                findVP(prices, quantities, SIZE, BUDGET, SLEEVE);
            }
            else if(option == 5){
                addMoreCapsules(quantities,SIZE);
            }
            else if(option == 9){
                findC(prices, quantities, SIZE);
            }
            else
            {
                flag = true;
            }
            
        }
       
        cout << "GoodBye!" << endl;
        return 0;
    } 
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • `mp, ind = findC(prices_copy, quantities_copy, SIZE);` is not going to assign both variables the result of `findC`. – jkb Nov 27 '20 at 19:40
  • how can i do so ? – newCoder Nov 27 '20 at 19:42
  • You could return a structure or a `std::pair`. If you're using C++17 you can then use *structured binding* to assign to two variables. – jkb Nov 27 '20 at 19:46
  • [Resist the temptations of `#include `](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). It's easy now, but will lead you into darkness later. – user4581301 Nov 27 '20 at 19:50

1 Answers1

0

This

return min, index;

doesn't do what you think it does. You obviously think it's going to return two values. But actually it just returns index.

This

mp, ind = findC(prices_copy, quantities_copy, SIZE);

doesn't do what you think it does. You obviously think it's going to assign the two returned values from findC to the variables mp and ind. But actually it's going to return the single value returned by findC to the variable ind and ignore mp.

If you want to know precisely what these constructs do then look up the comma operator, but I guess the moral of the story is that just because you can get some plausible looking code to compile it doesn't mean that it's going to do what you expected it to do.

So the real question is how to return two values from a function in C++. There are actually several possible approaches. Here's a question that reviews some of them, Returning multiple values from a C++ function.

john
  • 85,011
  • 4
  • 57
  • 81