Use the code provided below to write a complete C++ program that asks for the co
ID: 3534529 • Letter: U
Question
Use the code provided below to write a complete C++ program that asks for the code of an item in a vending machine and then prints the name and price of the item.Here is some sample code to get you going:
class Item {
public:
Item(string code, string name, float price);
// write the constructor code yourself
// write getCode, getName and getPrice functions
private:
string code;
string name;
float price;};
vector itemList;
Item notFound("00", "Not Found", 0.0);
Item findItem(string code){
// loop through the items in the vector
// and if found return the item
// otherwise return notFound;}
void main(){
Item smallDrink("A1", "Small Drink", .75);
Item candyBar("B1", "Candy Bar", .50);
itemList.push_back(smallDrink);
itemList.push_back(candyBar);
string code = "";
cout << endl;
cout << "Enter an item code:" ;
cin >> code;
Item item = findItem(code);
cout << "You selected: " << item.getName() << endl;}
You will need to make the following changes:
Add a loop
Exit the loop if the user types in code 99. Program should output the following:
Exit code enter
Program Ending
Run and check you code with Test 1 below
Add the additional items:
Item A2, Medium Drink, 1.00
Item A3, Large Drink, 1.25
Make your code print out the price of the item selected
Enter an item code: A1
You selected: Small Drink
The price is: 0.75
Run and test your code with Test 2 below
Add a variable to keep track of and print the number of items sold
Total items sold: 2
Add a variable to keep track of and print the total money made
Total money made: 1.25
Make your program's input and output look exactly like this:
Enter an item code: A1
You selected: Small Drink
The price is: 0.75
Enter an item code: B1
You selected: Candy Bar
The price is: 0.50
Enter an item code: C4
Sorry item not found.
Enter an item code: 99
Exit code entered.
Program Ending
Total items sold: 2
Total money made: 1.25
Explanation / Answer
#include <stdio.h>
int beverages();
void chosen(int choice);
float drink_cost(int choice);
int main()
{
int user_at_machine = 1;
while(user_at_machine)
{
int choice;
/* in an actual program floats and doubles
would not be used for monetary values
as they are not always exact */
float item_cost;
float money;
float change;
/* display items and get users choice */
choice = beverages();
/* display pick */
chosen(choice);
/* getting money could be in a function */
if(choice >= 1 && choice <= 20)
{
/* get drinks cost */
item_cost = drink_cost(choice);
printf("Enter your money: ");
scanf(" %f", &money);
printf(" ");
if (money > 0)
{
printf("ACCEPTED! ");
change = money - item_cost;
printf("Your change is %.2f", change);
}
else
{
printf("NOT ACCEPTED! ");
}
printf(" ");
/* get newline in stream */
getchar();
/* get character to pause */
getchar();
}
else
{
/* is user quitting ? */
if(choice == 99) {
user_at_machine = 0;
}
}
} /* end while */
return 0;
}
/* display beverages and get pick */
int beverages()
{
int choice;
printf("---------------------- ");
printf("Name list of beverage: ");
printf(" ");
printf("1. Pepsi"); printf(" RM1.00 ");
printf("2. Miranda"); printf(" RM1.00 ");
printf("3. Mountain Dew"); printf(" RM1.00 ");
printf("99. Quit");
printf(" ");
printf("Enter your choice: ");
scanf(" %d",&choice);
return choice;
}
/* display users pick */
void chosen(int choice)
{
switch(choice)
{
case 1:
printf("You choose Pepsi"); printf(" RM1.00 ");
break;
case 2:
printf("You choose Miranda"); printf(" RM1.00 ");
break;
case 3:
printf("You choose Mountain Dew"); printf(" RM1.00 ");
break;
case 99:
break;
default:
printf("Invalid input! ");
break;
}
}
/*get drink cost */
float drink_cost(int choice)
{
float cost = 0;
switch(choice)
{
case 1:
cost = 1.00;
break;
case 2:
cost = 1.00;
break;
case 3:
cost = 1.00;
break;
}
return cost;
}
//Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.