I can not get a sum from a function and return to main to check whether the sum is greater than the money added in and input more money.

int vending::MakeSelection(int ItemPrice[], int NumItems[], int sum){

    int response;


     do{

    cout << "Item:";
     cin >> response;


         if(response == 1){
         for(int i = 0; i < 1; i++){
         int totalCost = ItemPrice[i];
         NumItems[i]--;
         sum += totalCost;
         }
         }
         else if (response == 2){
             for(int i = 1; i < 2; i++){
         int totalCost = ItemPrice[i];
         NumItems[i]--;
         sum += totalCost;

         }

         }
         else if (response == 2){
             for(int i = 1; i < 2; i++){
         int totalCost = ItemPrice[i];
         NumItems[i]--;
         sum += totalCost;

         }

         }
         else if (response == 3){
             for(int i = 2; i < 3; i++){
         int totalCost = ItemPrice[i];
         NumItems[i]--;
         sum += totalCost;

         }

         }
         else if (response == 4){
             for(int i = 3; i < 4; i++){
         int totalCost = ItemPrice[i];
         NumItems[i]--;
         sum += totalCost;

         }

         }
         else if (response == 5){
             for(int i = 4; i < 5; i++){
         int totalCost = ItemPrice[i];
         NumItems[i]--;
         sum += totalCost;

         }

         }
         else if (response == 6){
             for(int i = 5; i < 6; i++){
         int totalCost = ItemPrice[i];
         NumItems[i]--;
         sum += totalCost;

         }

         }
         else if (response == 7){
             for(int i = 6; i < 7; i++){
         int totalCost = ItemPrice[i];
         NumItems[i]--;
         sum += totalCost;

         }

         }
         else if (response == 8){
             for(int i = 7; i < 8; i++){
         int totalCost = ItemPrice[i];
         NumItems[i]--;
         sum += totalCost;

         }

         }
         else if (response == 9){
             for(int i = 8; i < 9; i++){
         int totalCost = ItemPrice[i];
         NumItems[i]--;
         sum += totalCost;

         }

         }

     }while(response != 10);

     cout << "Total Cost:"<<sum<< endl;
     return sum;
 }
 int main(){

 int main ()
{ 

//declare nd initialise the items and coin stacks
    vending V;
  int Denominations = 5;

  int Coins[] = {100, 50, 20, 10, 5};
  int NumCoins[] = {10, 10, 10, 10, 10}; //assume we have 10 coins of each denomination

  const int Items = 9;

  int ItemPrice[ ] = { 75, 120, 120, 100, 150, 95, 110, 50, 120 }; //price in cents
  int    NumItems[ ] = { 10, 10, 10, 10, 10, 10, 10, 10, 10 }; 

//ask for input of coins -- you can ask the user to enter something like 2.75, and then you should assume that it has 2 1 dollar coins, 1 50 cents, 1 20 cents and 1 5 cents. Need to write a while loop and use % and / operators to find how many of those coins are present. Later you would need to increment the coins in NumCoins[]. You should also give appropriate error messages if the input is illegal. 

  V.ShowMenu();
  int sum=0;
int money = 0;
cout << "Please enter amount of money:" << endl;
cin >> money;
cout << endl;

 //do the rest

while(money != 0)
    {



        for(int i=0;i < Denominations; i++)
            {
                NumCoins[i]=NumCoins[i]+money/Coins[i];
                money=money%Coins[i];

            }


    }


cout << endl;
V.MakeSelection(ItemPrice,NumItems,sum);

V.ReturnChange(money,sum,Coins,NumCoins);





//make selection -- you will ask user for input, like if the user wants 2 x water and 4 x candy, then the user can enter "224444". call this function and pass this encoded  information eg. "224444" (either string or int), then decode it- meaning that you will need to separate the values - calculate the total cost from ItemPrice[ ] and update your NumItems[ ].



//repeat 










//just checking the items and coins:_ Not needed in Asg, but I wanted to show you how to pass Arrays as Parameter to functions. Note that in C++, Arrays are passed by REFERENCE by default. 

V.PrintConfidentialInformation(Denominations,Items, Coins, NumCoins, ItemPrice, NumItems); //see the style, how the arrays are passed as parameters...








 system ("pause");
  return 0;


 }

Recommended Answers

All 2 Replies

Member Avatar for iamthwee

you have declared two int mains.

My bad. I copy pasted it twice. Anyways i solved the issue. Apparently i did not pass the sum by reference.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.