-1

my code ran but for the distributor I need 80% to be the number to be the total cost and 80% of it taken out, i used 80/100 and got the total collected multiply by that, but it 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 to distributor $: " << Amount_paid_to_distributor << endl;
    cout << "Profit $: " << Profit << endl;
    system("pause");
    return 0;
}
Lorenzo Belli
  • 1,767
  • 4
  • 25
  • 46
  • You know that 80% = 0.8? That doesn't look like an integral value to me. (and read about integer arithmetic. 80/100=0, but 80/100.0 = 0.8). – Axel Sep 30 '15 at 07:54
  • 1
    possible duplicate of [Dividing 1/n always returns 0.0](http://stackoverflow.com/questions/13331054/dividing-1-n-always-returns-0-0) – Jan Doggen Sep 30 '15 at 08:17
  • `const int Amount_paid = (80/100);` change from int to float `const float Amount_paid = (80/100);`. int never consider decimals, values gets truncated to floor if it is of decimal type. – Suprabhat Biswal Sep 30 '15 at 08:26
  • @Suprabhat: That's not enough. Change from int to float const float Amount_paid = (80/100f);. Integer division always returns int. – Axel Sep 30 '15 at 09:04
  • @Axel Thanks Sir, I missed that ('f') out while typing and return will be of integer type as compiler will treat division as integer type not float :) – Suprabhat Biswal Sep 30 '15 at 09:11
  • possible duplicate of [Why does division result in zero instead of a decimal?](http://stackoverflow.com/questions/8906722/why-does-division-result-in-zero-instead-of-a-decimal) – risingDarkness Sep 30 '15 at 09:11

1 Answers1

4

const int Amount_paid = (80/100); is the same as const int Amount_paid = 0; as long as the type of variable is integer value.

If you want to perform operations with floating numbers, you need to use float or double.

Try changing to:

const double Amount_paid = 0.8;

You have many other errors in code. For example, somewhere you use float, somewhere you use double and then cast it to float... Do not mix them up, if you are not sure that it is required and it will work for your problem limitations - it may cause errors and problems. For example, here:

int Adult_Tickets;
int Child_Tickets;

int Total_tickets;

Total_tickets = (float)(Child_Tickets + Adult_Tickets);

Here you have two ints, you sum them up, cast to float and assign to int?

I would suggest replacing float with double everywhere and double-check all assignments, casting etc.

That's how your code should look like if we exclude some code styling issues like uppercase variables naming, possible combining of variable declarations, more precise and unambigious names for 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 to distributor $: " << Amount_paid_to_distributor << endl;
cout << "Profit $: " << Profit << endl;
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101