template<typename T = int>
auto stonum(const std::string& str)
{
static_assert(std::is_arithmetic_v<T>);
constexpr char white[] {" \n\r\t"};
bool ok {false};
T num {};
if (const auto fl {str.find_last_not_of(white)}; fl != std::string::npos) {
const auto end {str.data() + fl + 1};
const auto first {str.data() + str.find_first_not_of(white)};
return ok ? std::optional<T>{num} : std::optional<T> {};
}
template<>
auto stonum<double>(const std::string& str)
{
constexpr char white[] {" \n\r\t"};
bool ok {false};
double num {};
if (const auto fl {str.find_last_not_of(white)}; fl != std::string::npos) {
const auto end {str.data() + fl + 1};
const auto first {str.data() + str.find_first_not_of(white)};
if (end != first) {
std::string snum(first, end);
char* ech;
num = strtod(snum.c_str(), &ech);
ok = (*ech == 0);
}
}
return ok ? std::optional<double>{num} : std::optional<double> {};
}
auto getChar(const std::string& prmpt, const std::string& val, char def = {}) {
if (val.empty())
return getChar(prmpt, def);
std::string vs {" ("};
for (size_t i = 0; i < val.size(); vs += val[i++])
if (i) vs += '/';
vs += ')';
char ch;
do {
ch = (char)std::toupper(getChar(prmpt + vs, def));
} while ((val.find(ch) == std::string::npos) && (std::cout << "Input not a valid value" << vs << std::endl));
return ch;
}
int main()
{
char name[] = "user.txt";
constexpr auto delux {15.80}; // Delux meal
constexpr auto standard {11.75}; // Standard meal
constexpr auto child {60}; // Child discount percent
constexpr auto chdelux {delux * child / 100.0}; // Child delux meal
constexpr auto chstand {standard * child / 100.0}; // Child standard meal
constexpr auto tiptax {18}; // Tip & tax percent. Applies food only
constexpr auto surcharge {7}; // Surcharge percent for total bill if Friday, Saturday, Sunday
constexpr auto prompt {10}; // Days for discount
constexpr auto maxdisc {5}; // Maximum discount allowed
constexpr auto surday {4}; // First week day surcharge applied
constexpr auto discday {10}; // Discount within days
const auto AdServ = getNum("Number of adults served", 1, std::numeric_limits<int>::max());
const auto ChldServ = getNum("Number of children served", 1, std::numeric_limits<int>::max());
const auto type = getChar("Type of meal - Delux(D) or Standard(S)","");
const auto room = getChar("The room", "ABCDE");
const auto day = getNum("The day of the week the party was held on - Monday(1)..Sunday(7)", 1, 7);
const auto elapse = getNum("The number of days since the party was held", 1, 365);
const auto deposit = getNum<double>("The amount of deposit", 1, std::numeric_limits<double>::max());
if (day > surday)
total += total * surcharge / 100.0;
if (elapse < discday) {
bool apply {false};
for (const auto& d : discount)
if (total < d[0]) {
total -= total * d[1] / 100;
apply = true;
break;
}
if (apply == false)
total -= total * maxdisc / 100.0;
}
total -= deposit;
std::cout << std::fixed << std::setprecision(2) << "Amount payable is $" << total << std::endl;
}
umm one problem my teacher says that the code is from C!! and not C++.
Your teacher doesn't know their C from their C++! That code is absolutely C++ If the teacher believes it's C, then your teacher needs some lessons themselves.
Since when has std::cout, std::string, templates et al been part of C?
Well no, she doesn't. Or there's miscommunication. But that's OK. Stick to "basic" C++. (In my opinion, your code is overly complicated for little reason. Probably on purpose.) You should probably strip out all the fancy C++11 and beyond stuff (auto, constexpr, static_assert, std::optional) and just focus on the basics then.
Maybe you should just start again and just focus one part of the requirements at a time.
First, just focus on getting input from the user for the particular categories.