0

Here i want to calculate the number of sweets that i have been sold in Day 2.

int main()
{
float rate;
int left, total, sell ;

cout << "rate" ; // the rate that sweets have been sold per day
cin >> rate;
cout << "sell"; // number of sweets that have been sold in day1.
cin >> sell;
cout << "total"; // total number that the shops originally have
cin >> total;

left = total - sell;

sell = sell*(1+rate*(left/total));
        
cout << sell;
return 0;
}

And i want to calculate the numbers of sweets that have been sold in day2. Assume the input are rate = 0.5, sell=100, total = 10000. The output should be 14950. [ 10000 * (1+0.5*((10000-100)/10000)) ] But why I still get 10000 after running the program.

Updated:

However, I have one more question. If i want to output a rounded up sell value. How could I do it? because i am not allowed to change the datatype of the variables, which means it should be still int sell; at the initialization part. I have tried cout << round((double) sell); But it still does not work.

sidjfs
  • 21
  • 3
  • 2
    Because both `left` and `total` are both integers, the division `left/total` is an *integer* division with an *integer* result. It will not contain any decimals, and if `total` is larger than `left` then the result will be zero. To get decimals you need to convert at least one of the variables to a floating-point value. – Some programmer dude Mar 01 '21 at 12:08
  • When you get a surprising result, it's very often because of invalid assumptions. Verify yours by examining the parts of your expression, starting with the innermost one; `cout << (10000-100)/10000 << endl;`. – molbdnilo Mar 01 '21 at 12:10
  • 1
    Thank you so much! I have got the correct answer now. – sidjfs Mar 01 '21 at 12:18

1 Answers1

0

As mentioned in the comment, you are doing an integer division. Here is the corrected code:-

#include<iostream>
using namespace std;


int main()
{
float rate;
int left, total, sell ;

cout << "rate" ; // the rate that sweets have been sold per day
cin >> rate;
cout << "sell"; // number of sweets that have been sold in day1.
cin >> sell;
cout << "total"; // total number that the shops originally have
cin >> total;

left = total - sell;

sell = sell*(1+rate*(left/(float)total));
        
cout << sell;
return 0;
}

I have typecasted the denominator.

Abhishek Dutt
  • 1,308
  • 7
  • 14
  • 24
  • Thank you so much. I have just got the correct answer. Your comment is helpful ;) – sidjfs Mar 01 '21 at 12:18
  • However, I have one more question. If i want to output a rounded up ```sell``` value. How could I do it? because i am not allowed to change the datatype of the variables, which means it should be still ```int sell;``` at the initialization part. I have tried ```cout << round((double) sell);``` But it still does not work. – sidjfs Mar 01 '21 at 12:34
  • what happens if your remove `double` ? – Abhishek Dutt Mar 01 '21 at 12:44
  • if i just type ```cout << round(sell);```, it still could not round up the ```sell``` value. – sidjfs Mar 01 '21 at 13:00
  • That is quite questionable because the documentation of C++ says to use `round()` without any typecast. Gonna think on it. – Abhishek Dutt Mar 01 '21 at 15:30
  • I could get the right answer only if i change the datatype of ```sell``` from ```int``` to ```double```. – sidjfs Mar 01 '21 at 17:32