You can create a new variable, e.g. total, and increment it with the amount after every customer is billed. See it highlighted in the code below.
I'd also create a look-up table for the prices based on your 3 parameters: gender, length, style.
Your code enhanced:
#include <cctype>
#include <cstring>
#include <iostream>
#include <stdexcept>
const unsigned int prices[2][3][2] =
{
{ // Female
{ 25, 35 }, // Short: R, S
{ 30, 40 }, // Medium: R, S
{ 35, 45 }, // Long: R, S
},
{ // Male
{ 28, 33 }, // Short: R, S
{ 31, 36 }, // Medium: R, S
{ 35, 40 }, // Long: R, S
}
};
const char * genders = "FM";
const char * lengths = "SML";
const char * styles = "RS";
// Returns the 0-based index of value in validValues; -1 if not found
int Lookup(const char * validValues, char value)
{
const char * found = std::strchr(validValues, value);
if (found)
return found - validValues;
else
return -1;
}
// Finds the price in prices based on gender, length and style
unsigned int LookupPrice(char gender, char length, char style)
{
int g = Lookup(genders, std::toupper(gender));
if (g == -1)
throw std::runtime_error("Unknown gender: " + std::string(1, gender));
int l = Lookup(lengths, std::toupper(length));
if (l == -1)
throw std::runtime_error("Unknown length: " + std::string(1, length));
int s = Lookup(styles, std::toupper(style));
if (s == -1)
throw std::runtime_error("Unknown style: " + std::string(1, style));
return prices[g][l][s];
}
int main()
{
char addMore;
unsigned int total = 0;
do
{
char gender;
std::cout << "Enter your gender (F/M): ";
std::cin >> gender;
char length;
std::cout << "Enter your hair length (S/M/L): ";
std::cin >> length;
char style;
std::cout << "Enter your style (R/S): ";
std::cin >> style;
try
{
unsigned int price = LookupPrice(gender, length, style);
std::cout << "Price: $" << price << std::endl;
total += price;
}
catch (const std::runtime_error& e)
{
std::cout << "error: " << e.what() << std::endl;
}
catch (const std::exception& e)
{
std::cout << "error: " << e.what() << std::endl;
}
std::cout << "Add another customer? (Y/N): ";
std::cin >> addMore;
} while (std::toupper(addMore) == 'Y');
std::cout << "Total: $" << total << std::endl;
return 0;
}