Code:
#include <iostream>
using namespace std;
int getItemPrice()
{
int price;
// making sure cost is not negative
do
{
cout << "Enter cost of item (in cents) :" << endl;
cin >> price;
} while (price <= 0);
// making sure cost is a multiple of 5
while ((price%5)!=0)
{
cout << "Price of item must be multiple of 5 cents." << endl;
cout << "Enter cost of item (in cents) :" << endl;
cin >> price;
// again making sure cost is not negative
while (price <= 0)
{
cout << "Enter cost of item (in cents) :" << endl;
cin >> price;
}
}
return price;
}
int getCoins(int price)
{
int coin = 0;
int balance = 0;
// start entering coins
while (balance < price)
{
cout << "Current Balance: " << balance << endl;
cout << "Enter coin value (200, 100, 25, 10, 5) :" << endl;
cin >> coin;
// for invalid coins
if ( coin != 200 && coin != 100 && coin != 25 && coin != 10 && coin != 5)
{
cout << "Invalid coin." << endl;
}
else
{
balance += coin;
}
return balance;
}
void dispenseChange(int change);
{
// obtain number of quarters given out
Line 69 ----> int quarter = change/25;
int changeafterq = change % 25;
// using the remaining money owed try find number of dimes required
int dime = changeafterq/10;
int changeafterd = changeafterq % 10;
// same thing with nickels
int nickel = changeafterd/5;
Line 81 ----> cout << "You paid " << paid << " cents for something that cost " << getItemPrice() << " cents." << endl;
cout << "Giving " << change << " cents in change." << endl;
// Finally, enter integer values of each coin being given back.
cout << "Quarters: " << quarter << endl;
cout << "Dimes: " << dime << endl;
cout << "Nickels: " << nickel << endl;
}
int main()
{
int price;
int paid;
int change;
price = getItemPrice();
paid = getCoins(price);
change = paid - price;
dispenseChange(change);
}
Now I'm getting warnings when I compile that say
In function 'int getCoins(int)':
69: 'change' undeclared (first use this function)
81: 'paid' undeclared (first use this function)
I marked lines 69 and 81 in the code, but it's not actually marked in my program, this is just so you can see where the errors are occurring.
I'm slightly confused by the warnings because it says they occur in the function int getCoins, but the lines mentioned correspond with lines that are actually in the function dispenseChange.
Also, I think I know why 'paid' is undeclared, but I don't know how to declare it since I can't change anything in the main function.
Thanks.