Program: Patriot Parties

Pages: 12
So? They're most likely way too advanced for this OP and assignment and are probably just adding unnecessary confusion.

closed account (oG8qGNh0)
#include <limits>
#include <iostream>
#include <iomanip>
#include <string>
#include <type_traits>
#include <algorithm>
#include <optional>
#include <sstream>
#include <cctype>
#include <charconv>
#include <cstdlib>

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)};

ok = (end != first) && (std::from_chars(first, end, num).ptr == end);
}

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 getStr(const std::string& prmpt, bool noblank = true)
{
constexpr char term[] {": "};
std::string str;

while ((std::cout << prmpt << term) && (!std::getline(std::cin, str) |(noblank && str[0] < ' '))) {
std::cout << "Invalid input" << std::endl;
std::cin.clear();
}
return str;
}
template<typename T = int>
auto getNum(const std::string& prmpt)
{
static_assert(std::is_arithmetic_v<T>);
decltype(stonum<T>("")) optval;
do {
optval = stonum<T>(getStr(prmpt, true));
} while (!optval && (std::cout << "Bad input" << std::endl));

return *optval;
}
template<typename T = int>
auto getNum(const std::string& prmpt, T nmin, T nmax)
{
static_assert(std::is_arithmetic_v<T>);

if ((nmin == std::numeric_limits<T>::lowest()) && (nmax == std::numeric_limits<T>::max()))
return getNum<T>(prmpt);

const auto defs = [nmin, nmax]() {
std::ostringstream os;
os << " (";

if ((nmin != std::numeric_limits<T>::lowest()) || std::is_unsigned<T>::value)
os << nmin;

os << " - ";
if (nmax != std::numeric_limits<T>::max())
os << nmax;
os << ")";

return os.str();
} ();

T num {};
const std::string pr {prmpt + defs};

do {
num = getNum<T>(pr);
} while (((num < nmin) || (num > nmax)) && (std::cout << "Input out of range" << defs << std::endl));

return num;
}
auto getChar(const std::string& prmpt, int noblank = true)
{
std::string str;
bool bad {true};
do {
str = getStr(prmpt, noblank);
if (bad = (noblank && str.size() != 1) || (!noblank && str.size() > 1); bad)
std::cout << "Bad input" << std::endl;
} while (bad);
return str.empty() ? char(0) : str[0];
}
auto getChar(const std::string& prmpt, char def)
{
using namespace std::string_literals;

const auto ispr = std::isprint(def);
const auto ch = getChar(prmpt + (ispr ? " ["s + def + "]"s : ""s), !ispr);

return ch ? ch : def;
}

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

constexpr double roomrate[5] {55.0, 75.0, 85.0, 100.0, 130.0}; // Room rates
constexpr double discount[][2] {{100.0, 0.5}, {400.0, 3.0}, {800.0, 4.0}}; // Discount rates

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());

const auto mealcost = (type == 'D') * (delux * AdServ + chdelux * ChldServ) + (type == 'S') * (standard * AdServ + chstand * ChldServ);
auto total = roomrate[room - 'A'] + mealcost * (1.0 + tiptax / 100.0);

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;
}
Is there a question.......
closed account (oG8qGNh0)
no just reposted it in a cleaner way...
You consider posting completely unformatted code "cleaner"?
closed account (oG8qGNh0)
I guess
Looks like that was the first time OP posted code (in this thread, at least),

naveenmmenon, you can edit your post and add [code] and [/code] around your program text.

[code]
    { program here; }
[/code]
closed account (oG8qGNh0)
aight thx
closed account (oG8qGNh0)
okay umm one problem my teacher says that the code is from C!! and not C++.

cout<<"Question? ";
cin>>response; //where response is some variable

seeplusplus, The whole program you sent is C! Not C++
Last edited on
What the heck is "C factorial"? There is no code in this thread that is C.
Last edited on
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?
Last edited on
closed account (oG8qGNh0)
She thought computer programming for 25 years. Im pretty sure she knows the difference...
She says keyin.hpp should not be included ANDDD

"I think you are really over-complicating this.

You do not need to do error handling in this program.

It is simply a demonstration of checking conditions and accumulating a value."

My teacher said the last 3 lines
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.
Last edited on
If the teacher wants simple C++ then....

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
#include <string>
#include <iostream>
#include <iomanip>
#include <cctype>

int main()
{
	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

	constexpr double roomrate[5] {55.0, 75.0, 85.0, 100.0, 130.0};			// Room rates
	constexpr double discount[][2] {{100.0, 0.5}, {400.0, 3.0}, {800.0, 4.0}};	// Discount rates

	int AdServ, ChldServ, day, elapse;
	double deposit;
	char type, room;

	std::cout << "Number of adults served: ";
	std::cin >> AdServ;

	std::cout << "Number of children served: ";
	std::cin >> ChldServ;

	std::cout << "Type of meal - (D)elux, (S)standard: ";
	std::cin >> type;
	type = std::toupper(type);

	std::cout << "Room letter (ABCDE): ";
	std::cin >> room;
	room = std::toupper(room);

	std::cout << "Day of week number - Monday(1)..Sunday(7): ";
	std::cin >> day;

	std::cout << "Number of days elapsed since held: ";
	std::cin >> elapse;

	std::cout << "Deposit: ";
	std::cin >> deposit;

	const auto mealcost = (type == 'D') * (delux * AdServ + chdelux * ChldServ) + (type == 'S') * (standard * AdServ + chstand * ChldServ);
	auto total = roomrate[room - 'A'] + mealcost * (1.0 + tiptax / 100.0);

	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;
}


Note no input error checking at all. Make sure input is absolutely correct.......

https://repl.it/repls/WetGrandBusinesssoftware#main.cpp
Last edited on
Topic archived. No new replies allowed.
Pages: 12