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
|
#include <iostream>
#include <string>
#include <vector>
int choice( std::string prompt, std::vector<std::string> options )
{
std::cout << prompt << '\n' ;
for( std::size_t i = 0 ; i < options.size() ; ++i )
std::cout << i+1 << ". " << options[i] << '\n' ;
int n = 0 ;
std::cout << "choice? " ;
std::cin >> n ;
return n ; // for the moment, assume that n is a valid choice
}
int main()
{
int choice1 = choice( "Please enter a choice", { "to view balance", "to shop", "to exit" } ) ;
switch( choice1 )
{
case 1:
// ...
break ;
case 2:
{
int category = choice( "Choose among the three different categories",
{ "Produce", "Meat and fish", "Beverages", "Return to menu" } ) ;
switch(category)
{
case 1 :
{
std::vector<std::string> items =
{
"Bell peppers---$2.49 ea.",
"Bananas--59c ea.",
"Strawberries---$4.99/lb",
"Romaine lettuce---3.99 ea.",
"Sweet potatoes---$2.49 ea.",
"Garlic bulb---$1.29 ea.",
"Lime--79c ea."
};
int i = choice( "Please choose which produce item you would like", items ) ;
--i ; // vector indices are zero based
std::cout << "You chose " << items[i] << "\nHow many would you like to buy? " ;
int quantity ;
std::cin >> quantity ;
const std::vector<double> prices = { 2.49, 0.59, 4.99, 3.99, 2.49, 1.29, 0.79 } ;
double amount = quantity * prices[i] ;
std::cout << "you spent $" << amount << '\n' ;
}
}
}
}
}
|