In: Computer Science
I am working on a currency exchanging vending machine. I got it to work until the currency exchange section but the money won't be put into userAmount for the final section and it just repeat the currency exchange section.
Here is my code:
#include <cctype>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <iomanip>
using namespace std;
double convert(double currency, double conversionRate)
{
return currency * conversionRate;
}
int menu() {
char choice;
int price;
cout << "Welcome to the snack vending machine"
<< endl;
cout << endl;
cout << "Available snacks to select from:"
<< endl;
cout << "\t L - Lays Chips \t $2" <<
endl;
cout << "\t S - Snickers \t $5" <<
endl;
cout << "\t P - PopTart \t \t $3" <<
endl;
cout << "\t C - Cookies \t \t $5" <<
endl;
cout << "\t B - Browine \t \t $2" <<
endl;
cout << "\t N - Nuts \t \t $5" <<
endl;
while (1) {
cout << "Please enter the
letter labeling your snack selection: ";
cin >> choice;
choice = toupper(choice);
if (choice == 'L') {
price = 2;
break;
}
else if (choice == 'S') {
price = 5;
break;
}
else if (choice == 'P') {
price = 3;
break;
}
else if (choice == 'C') {
price = 5;
break;
}
else if (choice == 'B') {
price = 2;
break;
}
else if (choice == 'N') {
price = 5;
break;
}
else
cout <<
"Invalid selection!" << endl << endl;
}
return price;
}
int acceptMoney(int price) {
int userAmount = 0;
int choice;
double currency1, currency2;
do
{
cout << " CURRENCY
CONVERSION" << endl << endl;
cout << "1. Euros to Dollars"
<< endl;
cout << "2. Peso to Dollars"
<< endl;
cout << "3. Pounds to
Dollars" << endl;
cout << "4. Exit" <<
endl << endl;
cout << "Select your choice:
";
cin >> choice;
while (choice < 0 || choice >
4)
{
cout <<
"Enter a valid option: ";
cin >>
choice;
}
cout << endl;
switch (choice)
{
case 1: cout << "Enter amount
in Euros: ";
cin >>
currency1;
currency2 =
convert(currency1, 1.11);
cout <<
"Amount in Dollars: " << currency2;
userAmount +=
currency2;
break;
case 2: cout << "Enter
amount in Peso: ";
cin >>
currency1;
currency2 =
convert(currency1, 0.052);
cout <<
"Amount in Dollars: " << currency2;
userAmount +=
currency2;
break;
case 3: cout << "Enter
amount in Pounds: ";
cin >>
currency1;
currency2 =
convert(currency1, 1.31);
cout <<
"Amount in Dollars: " << currency2;
userAmount +=
currency2;
break;
case 4: break;
}
cout << endl <<
endl;
} while (choice != 4);
return price;
}
int computeChange(int totalPaid, int totalPrice) {
return totalPaid - totalPrice;
}
int main()
{
int totalPrice, totalPaid, change;
char choice;
while (1) {
totalPrice = menu();
totalPaid =
acceptMoney(totalPrice);
change = computeChange(totalPaid,
totalPrice);
cout << endl;
cout << "Your total inserted:
" << totalPaid << " Dollars" << endl;
cout << "Dispensing change: "
<< change << " Dollars" << endl;
cout << endl;
cout << "Would you want to
make another purchase? (Y/N): ";
cin >> choice;
cout << endl;
choice = toupper(choice);
if (choice == 'N') {
cout <<
"Thank you!!!" << endl;
break;
}
cout << endl;
}
return 0;
}
Try this code:
#include <cctype>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <iomanip>
using namespace std;
double convert(double currency, double conversionRate)
{
return currency * conversionRate;
}
int menu() {
char choice;
int price;
cout << "Welcome to the snack vending machine"
<< endl;
cout << endl;
cout << "Available snacks to select from:"
<< endl;
cout << "\t L - Lays Chips \t $2" <<
endl;
cout << "\t S - Snickers \t \t $5" <<
endl;
cout << "\t P - PopTart \t \t $3" <<
endl;
cout << "\t C - Cookies \t \t $5" <<
endl;
cout << "\t B - Browine \t \t $2" <<
endl;
cout << "\t N - Nuts \t \t $5" <<
endl;
while (1) {
cout << "Please enter
the letter labeling your snack selection: ";
cin >> choice;
choice =
toupper(choice);
if (choice == 'L') {
price
= 2;
break;
}
else if (choice == 'S')
{
price
= 5;
break;
}
else if (choice == 'P')
{
price
= 3;
break;
}
else if (choice == 'C')
{
price
= 5;
break;
}
else if (choice == 'B')
{
price
= 2;
break;
}
else if (choice == 'N')
{
price
= 5;
break;
}
else
cout
<< "Invalid selection!" << endl << endl;
}
return price;
}
int acceptMoney(int price) {
int userAmount = 0;
int choice;
double currency1, currency2;
do{
cout << " CURRENCY CONVERSION" <<
endl << endl;
cout << "1. Euros to Dollars" <<
endl;
cout << "2. Peso to Dollars" <<
endl;
cout << "3. Pounds to Dollars" <<
endl;
cout << "4. Exit" << endl <<
endl;
cout << "Select your choice: ";
cin >> choice;
while (choice < 0 || choice > 4)
{
cout << "Enter a
valid option: ";
cin >>
choice;
}
cout << endl;
switch (choice)
{
case 1: cout << "Enter amount in Euros:
";
cin >>
currency1;
currency2 =
convert(currency1, 1.11);
cout << "Amount in
Dollars: " << currency2;
userAmount +=
currency2;
break;
case 2: cout << "Enter amount in Peso:
";
cin >>
currency1;
currency2 =
convert(currency1, 0.052);
cout << "Amount in
Dollars: " << currency2;
userAmount +=
currency2;
break;
case 3: cout << "Enter amount in Pounds:
";
cin >>
currency1;
currency2 =
convert(currency1, 1.31);
cout << "Amount in
Dollars: " << currency2;
userAmount +=
currency2;
break;
case 4: break;
}
cout << endl << endl;
}while(choice != 4);
return userAmount;
}
int computeChange(int totalPaid, int totalPrice) {
return totalPaid - totalPrice;
}
int main()
{
int totalPrice, totalPaid, change;
char choice;
while (1) {
totalPrice = menu();
totalPaid =
acceptMoney(totalPrice);
change =
computeChange(totalPaid, totalPrice);
cout << endl;
cout << "Your total
inserted: " << totalPaid << " Dollars" <<
endl;
cout << "Dispensing
change: " << change << " Dollars" << endl;
cout << endl;
cout << "Would you want
to make another purchase? (Y/N): ";
X:cin >> choice;
cout << endl;
choice =
toupper(choice);
if (choice == 'N') {
cout <<
"Thank you!!!" << endl;
break;
}
cout << endl;
}
return 0;
}
Note: you should use float values for currency and there isn't need of do while loop in func userAmount.
In the func acceptMoney you returned price instead you should return userAmount.
You should use this code:
#include <cctype>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <iomanip>
using namespace std;
double convert(double currency, double conversionRate)
{
return currency * conversionRate;
}
int menu() {
char choice;
int price;
cout << "Welcome to the snack vending machine"
<< endl;
cout << endl;
cout << "Available snacks to select from:"
<< endl;
cout << "\t L - Lays Chips \t $2" <<
endl;
cout << "\t S - Snickers \t \t $5" <<
endl;
cout << "\t P - PopTart \t \t $3" <<
endl;
cout << "\t C - Cookies \t \t $5" <<
endl;
cout << "\t B - Browine \t \t $2" <<
endl;
cout << "\t N - Nuts \t \t $5" <<
endl;
while (1) {
cout << "Please enter
the letter labeling your snack selection: ";
cin >> choice;
choice =
toupper(choice);
if (choice == 'L') {
price
= 2;
break;
}
else if (choice == 'S')
{
price
= 5;
break;
}
else if (choice == 'P')
{
price
= 3;
break;
}
else if (choice == 'C')
{
price
= 5;
break;
}
else if (choice == 'B')
{
price
= 2;
break;
}
else if (choice == 'N')
{
price
= 5;
break;
}
else
cout
<< "Invalid selection!" << endl << endl;
}
return price;
}
int acceptMoney(int price) {
int userAmount = 0;
int choice;
double currency1, currency2;
cout << " CURRENCY CONVERSION" <<
endl << endl;
cout << "1. Euros to Dollars" <<
endl;
cout << "2. Peso to Dollars" <<
endl;
cout << "3. Pounds to Dollars" <<
endl;
cout << "4. Exit" << endl <<
endl;
cout << "Select your choice: ";
cin >> choice;
while (choice < 0 || choice > 4)
{
cout << "Enter a
valid option: ";
cin >>
choice;
}
cout << endl;
switch (choice)
{
case 1: cout << "Enter amount in Euros:
";
cin >>
currency1;
currency2 =
convert(currency1, 1.11);
cout << "Amount in
Dollars: " << currency2;
userAmount =
currency2;
break;
case 2: cout << "Enter amount in Peso:
";
cin >>
currency1;
currency2 =
convert(currency1, 0.052);
cout << "Amount in
Dollars: " << currency2;
userAmount =
currency2;
break;
case 3: cout << "Enter amount in Pounds:
";
cin >>
currency1;
currency2 =
convert(currency1, 1.31);
cout << "Amount in
Dollars: " << currency2;
userAmount =
currency2;
break;
case 4: break;
}
cout << endl << endl;
return userAmount;
}
int computeChange(int totalPaid, int totalPrice) {
return totalPaid - totalPrice;
}
int main()
{
int totalPrice, totalPaid, change;
char choice;
while (1) {
totalPrice = menu();
totalPaid =
acceptMoney(totalPrice);
change =
computeChange(totalPaid, totalPrice);
cout << endl;
cout << "Your total
inserted: " << totalPaid << " Dollars" <<
endl;
cout << "Dispensing
change: " << change << " Dollars" << endl;
cout << endl;
cout << "Would you want
to make another purchase? (Y/N): ";
X:cin >> choice;
cout << endl;
choice =
toupper(choice);
if (choice == 'N') {
cout
<< "Thank you!!!" << endl;
break;
}else if(choice != 'Y'){
cout<<"please enter (y/n):";
goto
X;
}
cout << endl;
}
return 0;
}
Hope, you like the solution.
Please give it a like and your valuable feedback or any doubt.