A couple of things ... Your "if" statements seem a bit awry, and you end up printing every charge, regardless. You have semi-colons where you shouldn't do, and no braces where you should do.
For example, rather than what you have, if you want to use a bunch of "if" statements, then this would be how you'd do it:
1 2 3 4 5 6 7 8 9 10 11 12
|
if (A=="C" && T==2)
{
cpkm= 0.50*25;
cout << "toll charge is" << cpkm;
}
else if (....)
{
...
}
else if (....)
{
...
|
The following link may help you with
if and
else if statements:
https://www.tutorialspoint.com/cplusplus/cpp_if_else_statement.htm
Also, it would be a better idea to have your variables named something more easily understood. Rather than "A", which doesn't really mean anything, better would be to call it
vehicle, and for "T" maybe
ticket. Then other people can more easily understand your code.
Many people will also tell you it's good practice not to use "using namespace std;" if you really don't need to.
I'm sure you can work on your program some more to get it working the way that you need it to. However, the code below is an alternative method you might like to modify for your purposes, or to play around with for other ideas:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
#include <map>
std::map<char,double> vcharge={ {'C',0.5},{'L',.75},{'B',1},{'H',1.25} };
std::map<int,int> tcharge={ {1,15},{2,25},{3,50},{4,75} };
int main(){
char vehicle; // type of vehicle
int ticket; // ticket type
double charge; // amount to pay
std::cout << "Vehicle: C=car, L=Light truck, B=bus, H=Heavy truck:\n";
std::cin >> vehicle;
std::cout << "Ticket: 1-yellow, 2-blue, 3-red, 4-orange:\n";
std::cin >> ticket;
charge = tcharge[ticket] * vcharge[toupper(vehicle)];
std::cout << "Toll Charge: " << charge;
return 0;
}
|
Example output:
Vehicle: C=car, L=Light truck, B=bus, H=Heavy truck:
h
Ticket: 1-yellow, 2-blue, 3-red, 4-orange:
3
Toll Charge: 62.5
|
An advantage to defining the vehicle and ticket charges in a single place, at the top of the program, besides being easier to read, is that you can easily change them if necessary, rather than searching line by line through the program to find where they need changing, which would be more prone to making mistakes.
You could think of
vcharge and
tcharge as little look-up tables, where, in
vcharge, the keys are 'C', 'L', 'B' and 'H', with the respective values of .5, .75, 1 and 1.25. Similarly with
tcharge, where the keys are 1, 2, 3 and 4, and the values are 15, 25, 50 and 75. The following link will give you much more information about maps if you want it:
http://www.cplusplus.com/reference/map/map/
Note that there is no validation in the program above to check to see if the user enters incorrect values, etc, so you would want to think about adding validation if you use that code, and generally make the program more robust.
I hope that helps. Good luck.