please i need help with my homework

forgive me, im really bad at coding,
write a program that accepts vehicle type and ticket type and determine the toll charges given the following formula.
toll charge formula is iirc= charge per km * km
vehicle type cpk
c=car 0.50
l=light truck 0.75
b=bus 1.00
h=heavy truck 1.25

ticket type km
1-yellow 15
2-blue 25
3-red 50
4- orange 75
Hi.

I think you'll generally find that people will respond more quickly or favorably if you can show what work you've done already, by posting the code you're working on.

That said, it's probably easier for you to think of the problem in separate steps, and figure out what you actually want the program to do, and how to do it. So, maybe something like the program outline below would be something that would give an idea of how to progress:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

using namespace std;

int main()
{
    // First you need to ask the user for the vehicle type
    cout << "What type of vehicle?\n";
    
    // so now get the input from the user
    // ... code for input

    // then you need to know the ticket type
    // ... so now you can do something very similar to above
    
    // Output a message telling the user what you want ...
    // ... code for message

    //  ... and now get the second input
    // ... code for input

    // now you have your two inputs, so you use the formula
    // to calculate the toll charges
    
    // then, tell the user how much they have to pay ...
    
    cout << "This is how much you owe:\n";
    
    // ... and print the charge.

return 0;
}


Good luck.
For so far this what i started with the help of my friend, she told me to use array iirc.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cmath>
#include<cctype>

using namespace std;
int main () {
void main () {
	
clrscr ();
cout <<" Toll Charge \n\n";
double TC= 0.0
int km[] ={15,35,50,75};
double cpkm []={php0.50,php0.75,php1.00,php1.25};

Cout << " Vehicle Type \t Ticket Type \t Charge per Km\t Km "<<endl;
for (int ctr=0, ctr <=3, ctr++)
switch (ctr)
case 0 : cout <<" C-Car"; "1-yellow"; break;
case 1: Cout <<" Light Truck"; "2-Blue"; break;
case 2: Cout<<" B-bus"; " 3-Red"; break;
default : cout<<" Heavy Truck"; "4-orange";break;

}
TC = cpkm*km;
Cout<<endl<<" Total Charge \t"<<endl;
Cout <<endl<<endl<<"Presd Any Key to Exit .....";
Getche ();
return 0;
}
Last edited on
Hello mitsumo12,

Since you are new here I am including this to help you in the future when you do post some code.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


If you are think that someone will write a program to get you started it could be a long wait.

Given your instructions start with this:

write a program that accepts vehicle type and ticket type


This tells you that you will start with two variables and get input from the user. Write, compile and debug if necessary a program that will do just this. Until you have some input to work with there is no point in going any further.

When you have done this post the code you have so everyone can see what you have done, make suggestions or help you correct any errors that you can not figure out.

When posting an error message it is better to post the complete message that the compiler puts out. It also helps to mention what IDE you are using along with the operating system.

When it comes to creating a variable name do not feel that you have to use something that was given to you unless you were told you must. When creating a variable name I find it is best to use a name that describes what the variable is or does. So, for a variable name for car use "car" not "c", "ltTruck" for light truck. I would say that camelCase is used more often when combining two or more parts. The other method used works off the old DOS of using the underscore "_" as a space to separate parts. Either method is fine, but choose one and stick with it.

When I see your code and have some time to think about the program I will have some other suggestions for you.

Hope that helps,

Andy
Hmmm. Well, without going all the way through it, or looking at the logic of it, there are a few things that stand out when first looking at it.

a) you can't have two "main" functions. (see lines 6 and 7)
b) clrscr (line 9) is not standard C++ code; best to just delete it
c) you have a semi-colon missing from the end of line 11
d) I don't see anything that you need the <cmath> header for
e) ... also don't see anything that you're using <cctype> for at the moment
f) you have "Cout" not "cout" in several places; this won't work; "Cout" is not the same as "cout"
g) your switch cases have some problems too.

If you click on the little wheel button (top right of code) you can run the code and see all the error messages you're getting. Some of them may not make much sense, but they'll hopefully give you some idea.

Try and fix as many of the errors as you can and see how far you can get; then there's sure to be someone able to help you progress if you need it.
Last edited on
I strongly recommend bookmarking and taking a look at the following sites / YouTube playlists. They're sites I've found incredibly helpful while learning C++.

This Site for Tutorials, Help, and general information:
- http://www.cplusplus.com/

C++ Tutorials:
- https://www.youtube.com/playlist?list=PL6xSOsbVA1eYl_5aQUgxJYvJdzNBroGGy
- https://www.youtube.com/playlist?list=PL318A5EB91569E29A
- https://www.learncpp.com/
- https://www.youtube.com/playlist?list=PL68244A805BD16617

C++ Examples: (This is for once you know a bit more C++)
- https://www.youtube.com/playlist?list=PL6xSOsbVA1eaxY06L5ZZJfDZSohwO6AKY

These sites and videos will teach you everything you need to know. Then if you still have questions after watching those videos etc, you can provide us with the code you have so far and what you're having trouble with.
Hello mitsumo12,

Much better, something to work with.

Cheddar99 has covered the high points and I will try to cover the rest although some may duplicate.

Line 2. There is nothing in your code at this time that requires "cmath". The same goes for "cctype", but you may need it later as the program changes.

Line 5. It may be easy now, but it is best not to use this line.

Line 6. Is correct and the only one you need.

Line 7. Is an error and not the way to write it.

Line 9. May work on your computer, but not everyone can use it. And what Cheddar99 has said.

Line 10. I would move down to say line 14. Now it is most likely my personal preference, but I like to define all the variables at the beginning of the function so I know where to find them. The new method, that I do not quite understand, is to define variables just before you use them. This may be an advantage sometimes, but I find it hard to locate the variables burred in the code.

Line 13. Is defined as an array of doubles, so your initialization list must consist of only floating point numbers. I do not have any idea what the "php" is for, but it should not be there.

Line 16. A for loop consists of three parts the first two separated by a semi-colon not the comma that you have used.

The switch statement has some problems that need addressed. It would also read better as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
switch (ctr)
{
	case 0:
		cout << " C-Car"; "1-yellow";
		TC = cpkm[ctr] * km[ctr];
		break;
	case 1:
		cout << " Light Truck"; "2-Blue";
		break;
	case 2:
		cout << " B-bus"; " 3-Red";
		break;
	default:
		cout << " Heavy Truck"; "4-orange";
		break;
}

The indentation and separate lines are more for the reader than the compiler.

Your for loop starts "ctr" at zero, so you switch is missing case 3. Do not use "default" to catch the last value of "ctr". the "default case" should be used if there is no match for the switch variable.

Also notice I put the line TC = cpkm[ctr] * km[ctr];. This needs to be done in each case statement not after the switch because of using "ctr" as the subscript to the array.

Your original code of TC = cpkm*km; will not work because "cpkm" and "km" are arrays and you need to use an element of the array for the calculation not the whole array. As I said this should be done in the case statements to get the correct value for "TC".

Line 25. Prints the words "Total Charge"< but nt the value of "TC", so you have no idea what the total charge is.

When it comes to your code white space, blank lines and indentation make no difference to the compiler. All this is used for the reader to make it easier to read and follow. This is not a contest to see how small you source code file can be. Make it easy to read. By the time the compiler and linker are finished the final executable file will be much larger than the original source code file.

Hope that helps,

Andy
Hello mitsumo12,

After working on what you posted I did make it work better until I noticed that what you have does not match what you need to do.

Some of what you have is worth keeping, but the for loop does not do what is needed.

To start with there is no user input to use to figure the cost of the toll.

The for loop is nice, but does not deal with one set of information at a time.

The switch is useful, but needs reworked.

Hope that helps,

Andy
guys thank you for the help, my teacher did not attend our class for some reason lol(lucky!)
and here is the simple stuff i came up while reading and practicing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>


using namespace std;
int main(){
	double cpkm;
	string A;
	int T;
	
	cout<<"C= car, L=Light truck , B-bus , H-heavy truck"<<endl;
	cin>>A;
	cout<<"1-yellow,2-blue,3-red,4-orange"<<endl;
	cin>>T;
	 if (A=="C"&& T==1);
	 cpkm= 0.50*15;
	 cout<<"Toll charge is" <<cpkm;
	  else if(A=="C"&& T==2);


only thing i need to do now i guess is to replace the A and T and change the formula of cpkm per if else statement!
so the final codes is like this maybe?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>


using namespace std;
int main(){
	double cpkm;
	string A;
	int T;
	
	cout<<"C= car, L=Light truck , B-bus , H-heavy truck"<<endl;
	cin>>A;
	cout<<"1-yellow,2-blue,3-red,4-orange"<<endl;
	cin>>T;
	 if (A=="C"&& T==1);
	 cpkm= 0.50*15;
	 cout<<"Toll charge is" <<cpkm;
	 if (A=="C"&& T==2);
	  cpkm= 0.50*25;
	  cout<<"toll charge is" <<cpkm;
	  if (A=="C"&& T==3);
	  cpkm= 0.50*50;
	  cout<<"toll charge is" <<cpkm;
	  if (A=="C"&& T==4);
	  cpkm= 0.50*75;
	  cout<<"toll charge is" <<cpkm;
}
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.
Last edited on
Topic archived. No new replies allowed.