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 132 133 134 135 136 137 138
|
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
// display a menu (the items to display are in the vector)
void show_menu( const std::string& title, const std::vector<std::string>& menu )
{
std::cout << '\n' << title << "\n\n" ;
for( int i = 0 ; i < menu.size() ; ++i )
std::cout << '[' << i+1 << "] " << menu[i] << '\n' ;
std::cout << '\n' ;
}
// get an integer from stdin in the interval [minv,maxv]
// prompt ask for fresh input in case of input errors
int get_int( int minv, int maxv )
{
std::cout << "enter a value in [1," << maxv << "]: " ;
int value ;
if( std::cin >> value )
{
if( value >= minv && value <= maxv ) return value ; // valid value, return it
else std::cout << value << " is not a valid value\n" ;
}
else // you may want to skip this part (input error handling) for now
// it takes care of the situation where the user did not enter a number
{
std::cout << "input failure. non-numeric input\n" ;
std::cin.clear() ; // clear the failed state
std::cin.ignore( 1000, '\n' ) ; // ignore this line of input
}
return get_int( minv, maxv ) ; // try again
}
// display a menu and return a valid choice entered by the user
int get_menu_choice( const std::string& title, const std::vector<std::string>& menu )
{
if( !menu.empty() )
{
show_menu( title, menu ) ;
// valid choices are from 1 up to the number of items in the menu
return get_int( 1, menu.size() ) ;
}
else return 0 ;
}
double get_bill_amount() // get a positive value as the bill amount
{
std::cout << "\nenter the bill amount: " ;
double bill ;
if( std::cin >> bill && bill > 0 ) return bill ; // valid input, return it
// input error handling
std::cout << "input failure. bad bill amount\n" ;
std::cin.clear() ; // clear the possible failed state
std::cin.ignore( 1000, '\n' ) ; // ignore this line of input
return get_bill_amount() ;
}
void tip_calculator()
{
std::cout << "\ntip calculator\n----------------\n" ;
const std::vector<std::string> menu = { "8%", "10%", "15%", "20%", "back to menu" } ;
const int choice = get_menu_choice( "Pick the tip percentage you would like to give", menu ) ;
if( choice != menu.size() ) // if the choice is not the last item in the menu (back)
{
// look up the tip percentage in an array
const double tip_value[] = { 0.00 /* unused place holder*/, 0.08, 0.10, 0.15, 0.20 } ;
const double tip = tip_value[choice] ; // choice is guaranteed to be a valid value
const double bill = get_bill_amount() ;
std::cout << std::fixed << std::setprecision(2)
<< "bill amount:" << std::setw(10) << bill << '\n'
<< " % tip:" << std::setw(10) << tip*100 << '\n'
<< " tip amount:" << std::setw(10) << bill*tip << '\n'
<< " total:" << std::setw(10) << bill + bill*tip << '\n' ;
}
}
double average( const std::vector<int>& grades )
{
long long total = 0 ;
// http://www.stroustrup.com/C++11FAQ.html#for
for( int g : grades ) total += g ;
// defensive: avoid the division 0/0
return grades.empty() ? 0 : double(total) / grades.size() ;
}
void grade_calculator()
{
std::cout << "\ngrade calculator\n----------------\n" ;
const std::vector<std::string> menu = { "calculate average grade", "back to menu" } ;
const int choice = get_menu_choice( "pick one of these choices", menu ) ;
if( choice == 1 )
{
std::cout << "number of classes? " ;
const int num_classes = get_int( 1, 10 );
// create a vector containing num_classes values
std::vector<int> grades(num_classes) ;
std::cout << "enter grades for " << num_classes << " classes\n" ;
for( int& g : grades ) // for each value in the vector
{
std::cout << "? " ;
g = get_int(0,100) ; // accept a non-negative grade, max 100
}
std::cout << "\naverage grade: " << std::fixed << std::setprecision(2)
<< average(grades) << '\n' ;
}
}
int main()
{
std::vector<std::string> main_menu = { "Tip Calculator", "Grade Calculator", "Quit Program" } ;
int choice = get_menu_choice( "\nwhat do you want to do?", main_menu );
// keep going till the choice is the last item in the menu (quit program)
while( choice != main_menu.size() )
{
if( choice == 1 ) tip_calculator() ;
else grade_calculator() ;
choice = get_menu_choice( "\nwhat do you want to do?", main_menu );
}
}
|