Question

In: Computer Science

I am working on a currency exchanging vending machine. I got it to work until the...

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;
}

Solutions

Expert Solution

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.


Related Solutions

Please show work! I have the answers to these parts, but I am having trouble working...
Please show work! I have the answers to these parts, but I am having trouble working through the problem to find the answer. Find the solution of the IVP: d.) 5y" - y' = 0, y(0) = -1, y'(0) = -1 (answer: y(t)=4 - 5 et/5) e.) 3y" - y = 0, y(0) = 3, y(t) is bounded for 0 <= t < infinity (answer: y = 3 exp(-(1/3)sqrt(3)t) ) g.) y" + 2y' + y = 0, y(0) =...
Hello, I am working on an assignment but I am unsure of how to solve it....
Hello, I am working on an assignment but I am unsure of how to solve it. Please help me. The assignment details are below. Consider this scenario: Your friend starts a website, nothingbutflags.com, which is not making money. Your friend asks you to help generate more traffic. You ask your friend how much traffic the website had last month? And your friend replies and says only 500 visits. You also ask how many flags did you sell? Your friend replies...
Hello, I am working on an assignment but I am unsure of how to solve it....
Hello, I am working on an assignment but I am unsure of how to solve it. Please help me. The assignment details are below. Consider this scenario: Your friend starts a website, nothingbutflags.com, which is not making money. Your friend asks you to help generate more traffic. You ask your friend how much traffic the website had last month? And your friend replies and says only 500 visits. You also ask how many flags did you sell? Your friend replies...
I am working on this problem for the company AT & T and am not sure...
I am working on this problem for the company AT & T and am not sure how to start it. Draw a chart of the main inter-organizational linkage mechanisms (e.g., long -term contacts, strategic alliances, mergers) that your organization uses to manage its symbiotic resource interdependencies. Using resource dependence theory and transaction cost theory, discuss why the organization to manage its interdependencies in this way. Do you think the organization has selected the most appropriate linkage mechanisms? Why or why...
I am working on routing path and subnet. The question is I am given with source...
I am working on routing path and subnet. The question is I am given with source 10.1.240.240 and the destinations is 10.2.240.240. How is the packet traveling?
1.You find a quarter in the vending machine at work. Now you have to check the...
1.You find a quarter in the vending machine at work. Now you have to check the vending machine every day now. This is an example of ___. a. operant conditioning with positive reenforcement b. classical conditioning with positive reenforcemenet c. operant conditioning with negative reenforcement d. classical conditioning with negative reenforcement 2. Every time you answer a question correctly in class, I praise you. This is an example of which learning theory? a. classical conditioning b. observational learning c. operant...
I am working on creating a Broadcast Receiver. I am extremely new to Android development and...
I am working on creating a Broadcast Receiver. I am extremely new to Android development and Java. I added my code at the bottom. Whenever I press the button the app crashes. I'm assuming something is wrong with connecting the broadcastIntent() function. I appreciate any help :) Here are the directions from my professor: Create an empty project Create a method in MainActivity.java which creates a Broadcast. public void broadcastIntent(View view){        Intent intent = new Intent();        intent.setAction("my.CUSTOM_INTENT"); sendBroadcast(intent);...
Explain applying for a position that I am currently working as a para-educator, however, I am...
Explain applying for a position that I am currently working as a para-educator, however, I am hired as an adult assistant. We are a new school short staff so I am needed to do more of the Para-educator's work alongside taking care of my student. How do I explain that showing I am qualified to do the new posting of Para-educator?
I am working on creating a Broadcast Receiver. I am extremely new to Android development and...
I am working on creating a Broadcast Receiver. I am extremely new to Android development and Java. I added my code at the bottom of this, but whenever I press the button the app crashes. I'm assuming something is wrong with connecting the broadcastIntent() function. If you could really focus on the first part that would be great!! I appreciate any help :) Here are the directions from my professor: Create an empty project Create a method in MainActivity.java which...
I am working on a project for my Computer Science course. I am trying to create...
I am working on a project for my Computer Science course. I am trying to create a Battleship game where a user names two coordinates on a grid and is then told whether this results in a "Hit" or a "Miss". Once the ship has been hit a certain number of times (based on the size of the ship) the ship is sunk. I have written all the code but it now fails to execute when I try to run...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT