0

Basically I am a complete beginner in C++ and I am facing a problem in updating the value of a variable in my code. Basically the code is to apply discount, so I am trying to update the value of bill but it is not working, where am I going wrong?

This is my entire code:

#include <iostream>

using namespace std;

int main()
{
    float bill;
    cout << "Enter bill amount: ";
    cin >> bill;
    float discount;
    if (bill < 100) {
        cout << "No discount applicable!" << endl;
        cout << "The bill is " << bill;
    }
    else if (bill >= 100 && bill < 500) {
        cout << "Congrats! A discount of 20% is applicable!" << endl;
        discount = 20 / 100;
        bill = bill - bill * discount;
        cout << "Final bill is " << bill << endl;
    }
    else if (bill >= 500) {
        cout << "Congrats! A discount of 30% is applicable!" << endl;
        discount = 30 / 100;
        bill = bill - bill * discount;
        cout << "Final bill is " << bill << endl;
    }
}

Also any other suggestions regarding the code will be appreciated!

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 1
    what makes you think that your are unable to do it? Please explain what you observe not only "is not working". What is your code supposed to do, what does it do instead? – 463035818_is_not_an_ai Oct 15 '20 at 14:14
  • Debugging tip: print `discount` to check that there really is one. – molbdnilo Oct 15 '20 at 14:16
  • 1
    I suggest learning how to use a debugger, or at least use additional `cout` statements to see if the values of variables are what you expect them to be. – cigien Oct 15 '20 at 14:17
  • This seems like a good time to step through your code with a debugger. I would suggest taking a careful look at the value of `discount` and seeing if it changes unexpectedly. – Nathan Pierson Oct 15 '20 at 14:17
  • Thank you all! I got my problem, it was related to truncation towards zero due to integer divison, I used float and it is working fine – Sanyam Jain Oct 15 '20 at 15:25
  • Follow the link at the top of your question, below "This question already has answers here: " – molbdnilo Oct 15 '20 at 15:26
  • @molbdnilo Yes i went there and got my solution, thanks a lot! – Sanyam Jain Oct 15 '20 at 15:29

0 Answers0