Thursday, 22 August 2013

variable is not passed by reference

variable is not passed by reference

I have compile and run my program correctly but it seems that the variable
sum is not passed by reference and it still got 0. Any help here is the
code.
#include "VendingMachine.h"
int VendingMachine::MakeSelection(int ItemPrice[], int NumItems[],int &sum){
int total_cost = 0;
cout << "Enter your choice: ";
cin >> choice;
if(choice >= 1 && choice <= 9){
while (choice != 0){
NumItems[(choice-1) % 10]--;
total_cost += ItemPrice[(choice-1)%10];
choice/=10;
}
}
}
Main.cpp
#include "VendingMachine.h"
int main()
{
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 sum, deposit;
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 };
VendingMachine caller;
caller.ShowMenu();
cout << endl;
cout << "Enter your money: ";
cin >> deposit;
caller.MakeSelection(ItemPrice,NumItems,sum);
cout <<"The total cost is " << sum << endl;
system("PAUSE");
return 0;
}
Vending.h
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int total_cost;
int Coins[5];
int NumCoins[5];
int ItemPrice[9];
int NumItems[9];
class VendingMachine{
public:
int MakeSelection(int ItemPrice[], int NumItems[],int &sum);
void ReturnChange(int& input,int& sum, int Coins[],int NumCoins[]);
void ShowMenu();
void DisplayErrorMessage(int error);
void PrintConfidentialInformation(int Denominations, int Items, int
Coins[], int NumCoins[], int ItemPrice[] , int NumItems[]);
private:
int choice;
string Password;
int deposit2;
};
The total cost should return a value of sum in the MakeSelection cpp but
still it returns a 0..??

No comments:

Post a Comment