C++ Coding divide integer

后端 未结 1 769
温柔的废话
温柔的废话 2021-01-29 07:41

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

1条回答
  •  轮回少年
    2021-01-29 08:40

    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;
    

    0 讨论(0)
提交回复
热议问题