C++ Coding divide integer -
my code ran distributor need 80% number total cost , 80% of taken out, used 80/100 , got total collected multiply that, shows 0.
#include <iostream> #include <string> using namespace std; int main() { int adult_tickets; int child_tickets; const int adult_price = 6; const int child_price = 3; cout << "adult tickets sold: " << endl; cin >> adult_tickets; cout << "child_tickets sold: " << endl; cin >> child_tickets; double total_collected; int total_tickets; double average_amount; const int amount_paid = (80/100); double amount_paid_to_distributor; double profit; total_collected = (float)(adult_tickets * adult_price) + (child_tickets * child_price); total_tickets = (float)(child_tickets + adult_tickets); average_amount = (float)total_collected / total_tickets; amount_paid_to_distributor = (float)total_collected * amount_paid; profit = (float)total_collected - amount_paid_to_distributor; cout << "total collected $: " << total_collected << endl; cout << "average amount collected per ticket $: " << average_amount << endl; cout << "amount paid distributor $: " << amount_paid_to_distributor << endl; cout << "profit $: " << profit << endl; system("pause"); return 0; }
const int amount_paid = (80/100);
same const int amount_paid = 0;
long type of variable integer value.
if want perform operations floating numbers, need use float
or double
.
try changing to:
const double amount_paid = 0.8;
you have many other errors in code. example, somewhere use float
, somewhere use double
, cast float
... not mix them up, if not sure required , work problem limitations - may cause errors , problems. example, here:
int adult_tickets; int child_tickets; int total_tickets; total_tickets = (float)(child_tickets + adult_tickets);
here have 2 int
s, sum them up, cast float
, assign int
?
i suggest replacing float
double
everywhere , double-check assignments, casting etc.
that's how code should if exclude code styling issues uppercase variables naming, possible combining of variable declarations, more precise , unambigious names variables etc.:
double total_collected; int total_tickets; double average_amount; const double amount_paid = 0.8; double amount_paid_to_distributor; double profit; total_collected = adult_tickets * adult_price + child_tickets * child_price; total_tickets = child_tickets + adult_tickets; average_amount = total_collected / total_tickets; amount_paid_to_distributor = total_collected * amount_paid; profit = total_collected - amount_paid_to_distributor; cout << "total collected $: " << total_collected << endl; cout << "average amount collected per ticket $: " << average_amount << endl; cout << "amount paid distributor $: " << amount_paid_to_distributor << endl; cout << "profit $: " << profit << endl;
Comments
Post a Comment