#include <iostream>
#include <map>
#include <string>
int main( )
{
enum Currency{ Silver = 10 , Gold = 50 , Silver10oz = 100 , Gold10oz = 500 } ;
std:: map < std:: string , unsigned > weapons
{
{ "M240" , 1250u} ,
{ "M249" , 3150u}
} ;
std:: cout << "Weapons:" << std:: endl ;
for ( auto const & it: weapons) //std::pair<std::string, unsigned> instead of auto
{
std:: cout << it.first << " = $" << it.second << std:: endl ;
}
std:: cout << "Which weapon would you like to purcahse? " ;
std:: string weaponToPurchase = "" ;
std:: cin >> weaponToPurchase;
auto selectedWeapon = weapons.find ( weaponToPurchase) ; //auto can be replaced with
//std::map<std::string, unsigned>::iterator
if ( selectedWeapon ! = weapons.end ( ) ) //it was found
{
std:: cout << "Please enter the number of silver, silver 10 oz, gold, and gold 10 oz"
<< " you are going to use for the purchase(separated with spaces): " ;
unsigned silver = 0u;
unsigned silver10oz = 0u;
unsigned gold = 0u;
unsigned gold10oz = 0u;
std:: cin >> silver >> silver10oz >> gold >> gold10oz;
unsigned paid = Silver * silver + silver10oz * Silver10oz +
Gold * gold + Gold10oz * gold10oz;
int owed = selectedWeapon- > second - paid;
std:: cout << "You gave me $" << paid;
if ( owed > 0 )
{
std:: cout << " and still owe $" << owed << std:: endl ; //charage them again
}
else if ( owed < 0 )
{
std:: cout << " which is an extra $" << - owed << std:: endl ;
}
}
else
{
std:: cout << "That weapon was not found" << std:: endl ;
}
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWFwPgojaW5jbHVkZSA8c3RyaW5nPgoKaW50IG1haW4oKQp7CgllbnVtIEN1cnJlbmN5eyBTaWx2ZXIgPSAxMCwgR29sZCA9IDUwLCBTaWx2ZXIxMG96ID0gMTAwLCBHb2xkMTBveiA9IDUwMCB9OwoJc3RkOjptYXA8c3RkOjpzdHJpbmcsIHVuc2lnbmVkPiB3ZWFwb25zCgl7CgkJeyJNMjQwIiwgMTI1MHV9LAoJCXsiTTI0OSIsIDMxNTB1fQoJfTsKCQo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