Hello Hassan Ashas,
After working with the program I came up with this idea working with what you started with instead of rewriting the whole program. If you have learned about functions it could be done differently.
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 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#include <iostream>
#include <string>
#include <cctype> // <--- Added. std::tolower() and std::toupper().
#include <limits> // <--- Added.
#include <chrono> // <--- Added.
#include <thread> // <--- Added.
//using namespace std; // <--- Best not to use.
int main()
{
// <--- Ideas I had for better variable names.
int amount{ 500 }; // <--- Did this for testing. Change the number as you need. Or leave the {}s empty to initialize to zero.
char beverageType{}, beverage{};
std::string drinkName{};
bool cont{ true }; // <--- Added.
do
{
//std::cout << "Enter Amount: "; // <--- Seems a bit early for this.
//std::cin >> amount;
std::cout << "\t\t\tVending Machine Menu"
<< "\nBeverage \tCode"
<< "\nCold Drinks\tC"
<< "\nHot Drink \tD"
<< "\nEnter Beverage Code: ";
std::cin >> beverageType;
beverageType = std::toupper(beverageType);
if (beverageType == 'C')
{
do // <--- Added.
{
std::cout << '\n' << std::endl
<< "Cold Drinks: "
<< "\nDrink\t\tCode\tCost "
<< "\nCoke\t\tC\tRs. 60/-"
<< "\nWater\t\tW\tRs. 60/-"
<< "\nSprite\t\tS\tRs. 60/-"
<< "\nPepsi\t\tP\tRs. 60/-"
<< "\nMinear Water\tM\tRs. 60/-"
<< "\nQuit\tQ";
std::cout << '\n' << std::endl << "Enter Drink Code: ";
std::cin >> beverage;
beverage = std::toupper(beverage);
switch (beverage)
{
case 'C':
drinkName = "Coke";
break;
case 'W':
drinkName = "Water";
break;
case 'S':
drinkName = "Sprite";
break;
case 'P':
drinkName = "Pepsi";
break;
case 'M':
drinkName = "Mineral Water";
break;
case 'Q':
break;
default:
std::cout << "Invalid code";
std::this_thread::sleep_for(std::chrono::seconds(3)); // Requires header files "chrono" and "thread". This reeplaces the "system("pause")".
}
} while (beverage != 'C' && beverage != 'W' && beverage != 'S' && beverage != 'P' && beverage != 'M' && beverage != 'Q');
if (beverage != 'Q') // <--- Added.
cont = false; // <--- Added.
}
else if (beverageType == 'D')
{
std::cout << '\n' << std::endl
<< "Hot Drinks: "
<< "\nDrink\t\t\tCode\tCost"
<< "\nBlack Tea\t\tB\tRs. 60/-"
<< "\nCoffee\t\t\tC\tRs. 60/-"
<< "\nGreen Tea\t\tG\tRs. 60/-"
<< "\nCappuccino\t\tP\tRs. 60/-"
<< '\n' << std::endl;
std::cout << "Enter Drink Code: ";
std::cin >> beverage;
beverage = std::toupper(beverage);
switch (beverage)
{
case 'B':
drinkName = "Black Tea";
break;
case 'C':
drinkName = "Coffee";
break;
case 'G':
drinkName = "Green Tea";
break;
case 'P':
drinkName = "Cappuccino";
break;
default:
std::cout << "Invalid code.";
break;
}
}
else
std::cout << "Invalid Beverage drink code. ";
} while (cont); // <--- Could be reworked to loose the "cont" variable.
std::cout << std::endl;
if (amount >= 60)
std::cout << '\n' << "\t\t\t\tInvoice\n" << "Beverage: " << drinkName << '\n' << "Cost: Rs. 60/-\n" << "Remaining amount: Rs. " << amount - 60 << "/-" << std::endl;
else
std::cout << "\nYou are out of money to buy this item." << std::endl;
// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
// <--- Used to keep the console window open in Visual Studio Debug mode.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();
//system("pause");
return 0; // <--- Not really necessary, but good form and a good break point in the IDE.
}
| |
I did not change the else if section yet, but it should have the same format as the if section. This way until you make a valid drink choice it stays in a loop instead of ending the program. Once a valid drink choice is made the do/while loops end and the invoice part is executed.
Doing it this way does set you up for making multiple drink choices in the future.
At the end of the program you will see that I put a comment on
system("pause");
. The lines above it are a replacement. Maybe not the best, but it works. The comments are there to let you know how it works. They can be deleted if necessary.
It is best not to use "system" anything as it could leave the program open to attack and it limits your program to Windows operating systems. If your use is limited to personal or school use continue to use it. And in that case "system("cls") would be useful or I do use a function to clear the screen if you are interested, but again it is limited to Windows.
I did allow an exit back to the main menu, but not from the program. That is your choice. Easily adjusted. You do not have to put an exit choice in the main menu, but you could code for it and have an exit if you need one.
Please notice my use of the "\n" in the "cout" statements. This breaks up the output and makes it more readable. And along with well placed statements to clear the screen it looks much nicer.
The last if statement:
First for a single line the {}s are not needed.
Second you could move lines 116 - 119 inside the outer do/while loop and comment out or remove lines 73 and 74 then change the while condition of the outer while loop to
} while (true);
and the program will always continue to run.
See what you think and if it does what you want. If I think of anything else I will let you know. Any questions let me know.
Hope that helps,
Andy