c++ operator prob

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;
}
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
> 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;
}
Ohh that makes sense, silly me. Cheers, thanks mate! :)
Registered users can post here. Sign in or register to post.