Having a minor issue with my program
May 4, 2017 at 8:59pm UTC
I'm working on a vending machine program but i'm having trouble getting it to output change correctly. My code is listed below, can someone point out what i'm doing wrong please?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
#include <iostream>
#include <string>
using namespace std;
void vendingMenu();
int main()
{
//Declaration block
int userSelection = 0;
int itemPrice = 0;
int itemNumber = 0;
int userChange = 0;
double userCashAmount = 0;
double itemOnePrice = 1.50;
double itemTwoPrice = 0.75;
double itemThreePrice = 0.90;
double itemFourPrice = 0.75;
double itemFivePrice = 1.75;
double itemSixPrice = 0.75;
vendingMenu();
cout << "Please enter the item number you want: " ;
cin >> userSelection;
if (userSelection == 1)
{
cout << "Enter your money amount: " ;
cin >> userCashAmount;
cout << "Thank you for purchasing item #1. Your change is: " << userChange << endl;
userChange = userCashAmount - itemOnePrice;
}
else
if (userSelection == 2)
{
cout << "Enter your money amount: " ;
cin >> userCashAmount;
cout << "Thank you for purchasing item #2. Your change is: " << userChange << endl;
userChange = userCashAmount - itemTwoPrice;
}
else
if (userSelection == 3)
{
cout << "Enter your money amount: " ;
cin >> userCashAmount;
cout << "Thank you for purchasing item #3. Your change is: " << userChange << endl;
userChange = userCashAmount - itemThreePrice;
}
else
if (userSelection == 4)
{
cout << "Enter your money amount: " ;
cin >> userCashAmount;
cout << "Thank you for purchasing item #4. Your change is: " << userChange << endl;
userChange = userCashAmount - itemFourPrice;
}
else
if (userSelection == 5)
{
cout << "Enter your money amount: " ;
cin >> userCashAmount;
cout << "Thank you for purchasing item #5. Your change is: " << userChange << endl;
userChange = userCashAmount - itemFivePrice;
}
else
if (userSelection == 6)
{
cout << "Enter your money amount: " ;
cin >> userCashAmount;
cout << "Thank you for purchasing item #6. Your change is: " << userChange << endl;
userChange = userCashAmount - itemSixPrice;
}
system("pause" );
return 0;
}
void vendingMenu()
{
cout << "Vending Machine" << endl;
cout << "1 1.50" << endl;
cout << "2 0.75" << endl;
cout << "3 0.90" << endl;
cout << "4 0.75" << endl;
cout << "5 1.75" << endl;
cout << "6 0.75" << endl;
}
void purchaseSummary()
{
cout << "Purchase Summary" ;
int itemsPurchased = 0;
int totalCost = 0;
int moneyInserted = 0;
int changeReturned = 0;
{
cout << "Number of items purchased: " << itemsPurchased << endl;
cout << "Total cost of all items purchased: " << totalCost << endl;
cout << "Total Amount of money inserted: " << moneyInserted << endl;
cout << "Total Amount of change returned: " << changeReturned << endl;
}
}
May 4, 2017 at 10:11pm UTC
bump
May 4, 2017 at 10:33pm UTC
The code is outputting the message about the user change before it's calculated.
May 4, 2017 at 10:37pm UTC
also userChange should be a double
May 4, 2017 at 10:39pm UTC
Ah I see, thank you :)
Topic archived. No new replies allowed.