Apr 15, 2019 at 8:16am UTC
Hi guys, I've a small doubt in the calculation part of my code. The total is not calculating properly. I need it to take either type A or type B when calulating the sum. thank you in advance
#include <iostream>
using namespace std;
int main()
{
int homeA = 500, CLI, usage, typeA = 410, typeB = 500, GB, tax, total;
char pack = 'N';
cout << "Enter usage amounts:" << endl;
cout << "SL COM - ";
cin >> CLI;
cout << "Voice Usage - ";
cin >> usage;
cout << " Enter package:\n1 - Domestic\n2 - Business" << endl;
cin >> pack;
cout << "GB Used - ";
cin >> GB;
cout << "Tax - ";
cin >> tax;
total = homeA + CLI + usage + (typeA | typeB) + GB + tax;
cout << "\n SLCOM Billing Date : 04 / 04 / 2019" << endl;
cout << " SLCOM PLC Billing Period : 01 / 03 / 2019 - 30 / 03 / 2019" << endl;
cout << " Flower Road, Colombo - 03 \n" << endl;
cout << " NUG2489658 (Rs.)" << endl;
if (pack == '1')
{
cout << " SLCOM Type A- Domestic [Rental] \t\t" << typeA << endl;
}
else if (pack == '2')
{
cout << " SLCOM Type B- Business [Rental]\t\t" << typeB << endl;
}
cout << " SLCOM CLI\t\t" << CLI << endl;
cout << " SLCOM Voice Usage\t\t" << usage << endl;
cout << " SLCOM Home Advance[Rental]\t\t" << homeA << endl;
cout << " Extra GB Used - --------------------(5GB)\t\t" << GB << endl;
cout << " Taxes and Levies\t\t" << tax << endl;
cout << " Total is - \t\t" << total << endl;
return 0;
}
Apr 15, 2019 at 8:37am UTC
Use the conditional operator
total = homeA + CLI + usage + (pack == '1' ? typeA : typeB) + GB + tax;
or split the calculation and add either typeA or typeB inside the if statements where you know which one you should use.
Last edited on Apr 15, 2019 at 8:38am UTC
Apr 15, 2019 at 8:39am UTC
> total = homeA + CLI + usage + (typeA | typeB) + GB + tax;
Well start with this
total = homeA + CLI + usage + GB + tax;
Then do
if (pack == '1')
{
cout << " SLCOM Type A- Domestic [Rental] \t\t" << typeA << endl;
total += typeA;
}
Apr 15, 2019 at 12:35pm UTC
Ohh that makes sense, silly me. Cheers, thanks mate! :)