help with 2 counters for class assignment

I had to create a automated highway patrol system for my class and I am stuck with the counters. I have to create 2 counters one to count number of vehicles and one to count number of ticketed vehicles. I need the counters so I can figure out how many vehicles got tickets out of the total amount of vehicles, and then figure out the percent of ticketed vehicles but I have no idea how to use counters.

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

//Prototype 
void programInfo();
void licenseP( string & plateNumber);
void inputData(double & vehicleSpeed, double & speedLimit);
void processData( double vehicleSpeed, double & ticketAmount, double & speedOver, double speedLimit, string plateNumber);
void displayData();

int main()
{// Variables
	double vehicleSpeed;
	double ticketAmount;
	double speedOver;
	double speedLimit;
	string plateNumber;
	string sentinel = "NONE";
	programInfo();
	cout << fixed << showpoint << setprecision(2);
	licenseP(plateNumber);
	
	// While loop
	while ( plateNumber != sentinel)
	{
		inputData(vehicleSpeed,speedLimit);
		processData( vehicleSpeed, ticketAmount, speedOver, speedLimit, plateNumber);
		cout << "\n";
		licenseP(plateNumber);
		
	}
	displayData();

	

	return 0;
}

//Definitions

// Display basic program information 
void programInfo()
{
	cout << "AHP Program version 2.1 by Gabriela Quevedo Rodriguez.";
	cout << "Enter the license plate number, vehicle's current speed, and speed limit in the zone." << endl;
	cout << "To stop repeating the process please enter 'NONE'." << endl;
	cout << "\n";
	
}

// Licence plate input
void licenseP(string & plateNumber)
{
	cout << "Enter a license plate number  --->" << setw(8);
	cin >> plateNumber;
}



// Vehicle speed and speed limit input
void inputData( double & vehicleSpeed, double & speedLimit)
{
	cout << "Enter current vehicle's speed --->" << setw(8);
	cin >> vehicleSpeed;
	cout << "Enter speed limit in the zone --->" << setw(8);
	cin >> speedLimit;
}

// Determine if a ticket will be given and total ticket cost
void processData( double vehicleSpeed, double & ticketAmount, double & speedOver, double speedLimit, string plateNumber)
{
	speedOver = vehicleSpeed - speedLimit;

	if (speedOver < 5)

	{
		cout << "No ticket is issued to " << plateNumber << "." << endl;
		return ;
	}
	else if (speedOver <= 20)
	{
		ticketAmount = 150.00 + (5.00 * speedOver);
	}
	else
	{
		ticketAmount = 150.00 + (10.00 * speedOver);

		if (speedOver > 50)

			ticketAmount = 1000.00;
	}
	 
	cout << " A ticket of $" << ticketAmount << " is issued to " << plateNumber << "." << endl;
}

// Display ticketed vehicle amount out of total vehicles. Determine percent of ticketed vehicles and average ticket amount.
void displayData()
{ 
	int totalVehicles;
	int ticketVehicles;

	for (totalVehicles = 0; 

	totalVehicles = inputData() + 1;

	cout << "Thank you for using our AHP program." << endl;
}
	

Put the code you need help with here.

// Display ticketed vehicle amount out of total vehicles. Determine percent of ticketed vehicles and average ticket amount.
void displayData()
{
int totalVehicles;
int ticketVehicles;

for (totalVehicles = 0;

totalVehicles = inputData() + 1;

cout << "Thank you for using our AHP program." << endl;
}
Last edited on
By using counters, it helps to control or keep track of the number of iterations a loop performs. For example, the below code displays a table consisting of the numbers 1 through 10 and their squares, so its loop must iterate 10 times.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// This program displays a list of numbers and
// their squares.
#include <iostream>
using namespace std;

int main()
{
   const int MIN_NUMBER = 1,   // Starting number to square
             MAX_NUMBER = 10;  // Maximum number to square

   int num = MIN_NUMBER;       // Counter

   cout << "Number Number Squared\n";
   cout << "-------------------------\n";
   while (num <= MAX_NUMBER)
   {
      cout << num << "\t\t" << (num * num) << endl;
      num++; //Increment the counter.
   }
   return 0;
}


The variable num , which starts at 1, is incremented each time through the loop. When num reaches 11 the loop stops. num is used as a counter variable, which means it is regularly incremented in each iteration of the loop. In essence, num keeps count of the number of iterations the loop has performed.

I hope it helps.
Do you think you can show me another example of how a counter is used. I'm still a bit confused .

Below is another example of the counter. Take a look at line 15. It counts the tries the user have after the user inputted the number to guess the number.

int tries = 0; // It counts the number of times the user guess the number.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	if (num > random) // Determine if the guess number is higher than the random.
	{
		cout << "Too high, try again.\n"; // Notify the user the number is higher than the random.
	}

	else if (num < random) // Determine if the guess number is lower than the random.
	{
		cout << "Too low, try again.\n"; // Notify the user the number is lower than the random.
	}

	else // Determine if the user guessed correctly.
	{
		cout << "Correct! You got it in" << endl; // Notify the user guessed correctly.
	}
	tries++; // Track the number of guesses the user makes. 


Now the below code, it shows how to use the if/else statement to notify the user according to the number or tries (counter).
1
2
3
4
5
6
7
8
9
10
11
12
    if (tries < 5) // If the guesses are less than 5,
	{
		cout << "Either you know the secret or you got lucky!\n"; // This will be the output message.
	}
    else if (tries >= 5 && tries <= 7) // If the guesses are between 5 and 7,
	{
		cout << "You're pretty good at this!\n"; // This will be the output message.
	}
    else if (tries >= 8 && (tries <= 10 && num == random)) // If the guesses are between 8 and 10.
	{
		cout << "You'll do better next time.\n"; // This will be the output message.
	}


I hope it helps.
Last edited on

Okay I came up with this but I am not sure how to set it up.
1. Int totalVehicles =0
2. int ticketedVehicles =0

Every time I execute “inputData” add one to total vehicles.

Every time a ticket condition is met add 1 to ticketedVehicles. I will need “ticketedVehicles++” in each of my two ticket conditions.
Please code tag your code in your initial post. Hint: Edit your initial post, highlight your entire code, then hit the <> icon on the right side of your post.
okay I just finished the code tag
alright, give it a try and see if it works.

PS. Does your code compile?
Last edited on
Yeah it compiles. I'm using Microsoft Visual Studio.
My code doesn't work
You have compile error. Hence, it does not compile.

Take a look at line 104. It is a syntax error. Are you trying to run a for loop? Nevertheless, you may want to take a look at the line to figure out what do you want to do.

On line 106, you are calling the function inputData() , but, according to your function prototype (line 9) it supposed to take to two double arguments.

I know you are asking about the counters, but until you get the program running, it will be an issue.

Last edited on
oh I see what you mean. Yeah I am trying to have a for loop but I am not to sure how to set it up correctly.
Take a look at line 26-34:
I would recommend to change it to :

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
	int ticketCounter = 0; // Set counter
	int totalVehicles = 0; // Set counter

	// While loop
	while (plateNumber != sentinel)
	{
		inputData(vehicleSpeed, speedLimit);
		processData(vehicleSpeed, ticketAmount, speedOver, speedLimit, plateNumber);

		// Use the if/else statement to count the vehicles
		// being ticket and the total of vehicles pull over
		// according to the ticket amount.
		if (ticketAmount == 0)
			totalVehicles++;
		else if (ticketAmount > 0)
		{
			totalVehicles++;
			ticketCounter++;
		}
		cout << "\n";
		licenseP(plateNumber);

	}

	cout << "Number of Vehicles with tickets is " << ticketCounter << endl;
	cout << "Number of Vehicles without tickets is " << totalVehicles - ticketCounter << endl;
	cout << " Total of Vehicles ticketed/non-ticketed : " << totalVehicles << endl;


Take a look at line 72 to line 96:
I would recommend to change it like so:

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
void processData(double vehicleSpeed, double & ticketAmount, double & speedOver, double speedLimit, string plateNumber)
{
	speedOver = vehicleSpeed - speedLimit;

	if (speedOver < 5)

	{
		cout << "No ticket is issued to " << plateNumber << "." << endl;
		ticketAmount = 0.0; // Change from return to ticketAmount to be used in main to count vehicles.
	}
	else if (speedOver <= 20)
	{
		ticketAmount = 150.00 + (5.00 * speedOver);
		cout << " A ticket of $" << ticketAmount << " is issued to " << plateNumber << "." << endl;
	}
	else if (speedOver > 20 && speedOver < 50)
	{
		ticketAmount = 150.00 + (10.00 * speedOver);
		cout << " A ticket of $" << ticketAmount << " is issued to " << plateNumber << "." << endl;

	}
	else if (speedOver >= 50)
	{
		ticketAmount = 1000.00;
		cout << " A ticket of $" << ticketAmount << " is issued to " << plateNumber << "." << endl;
	}
}


I deleted the displayData function. However, if you need it, work on it and let us know if there are any issues.

I hope it helps.
I meant like this:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

//Prototype 
void programInfo();
void licenseP(string & plateNumber);
void inputData(double & vehicleSpeed, double & speedLimit);
void processData(double vehicleSpeed, double & ticketAmount, double & speedOver, double speedLimit, string plateNumber);
void displayData(double vehicleSpeed, double speedLimit);

int main()
{// Variables
	double vehicleSpeed =0;
	double ticketAmount=0;
	double speedOver=0;
	double speedLimit=0;
	string plateNumber=" ";
	string sentinel = "NONE";
	programInfo();
	cout << fixed << showpoint << setprecision(2);
	licenseP(plateNumber);

	int ticketCounter = 0; // Set counter
	int totalVehicles = 0; // Set counter

	// While loop
	while (plateNumber != sentinel)
	{
		inputData(vehicleSpeed, speedLimit);
		processData(vehicleSpeed, ticketAmount, speedOver, speedLimit, plateNumber);

		// Use the if/else statement to count the vehicles
		// being ticket and the total of vehicles pull over
		// according to the ticket amount.
		if (ticketAmount == 0)
			totalVehicles++;
		else if (ticketAmount > 0)
		{
			totalVehicles++;
			ticketCounter++;
		}
		cout << "\n";
		licenseP(plateNumber);

	}

	cout << "Number of Vehicles with tickets is " << ticketCounter << endl;
	cout << "Number of Vehicles without tickets is " << totalVehicles - ticketCounter << endl;
	cout << " Total of Vehicles ticketed/non-ticketed : " << totalVehicles << endl;
	//displayData();



	return 0;
}

//Definitions

// Display basic program information 
void programInfo()
{
	cout << "AHP Program version 2.1 by Gabriela Quevedo Rodriguez.";
	cout << "Enter the license plate number, vehicle's current speed, and speed limit in the zone." << endl;
	cout << "To stop repeating the process please enter 'NONE'." << endl;
	cout << "\n";

}

// Licence plate input
void licenseP(string & plateNumber)
{
	cout << "Enter a license plate number  --->" << setw(8);
	cin >> plateNumber;
}



// Vehicle speed and speed limit input
void inputData(double & vehicleSpeed, double & speedLimit)
{
	cout << "Enter current vehicle's speed --->" << setw(8);
	cin >> vehicleSpeed;
	cout << "Enter speed limit in the zone --->" << setw(8);
	cin >> speedLimit;
}

// Determine if a ticket will be given and total ticket cost
void processData(double vehicleSpeed, double & ticketAmount, double & speedOver, double speedLimit, string plateNumber)
{
	speedOver = vehicleSpeed - speedLimit;

	if (speedOver < 5)

	{
		cout << "No ticket is issued to " << plateNumber << "." << endl;
		ticketAmount = 0.0; // Change from return to ticketAmount to be used in main to count vehicles.
	}
	else if (speedOver <= 20)
	{
		ticketAmount = 150.00 + (5.00 * speedOver);
		cout << " A ticket of $" << ticketAmount << " is issued to " << plateNumber << "." << endl;
	}
	else if (speedOver > 20 && speedOver < 50)
	{
		ticketAmount = 150.00 + (10.00 * speedOver);
		cout << " A ticket of $" << ticketAmount << " is issued to " << plateNumber << "." << endl;

	}
	else if (speedOver >= 50)
	{
		ticketAmount = 1000.00;
		cout << " A ticket of $" << ticketAmount << " is issued to " << plateNumber << "." << endl;
	}
}

// Display ticketed vehicle amount out of total vehicles. Determine percent of ticketed vehicles and average ticket amount.
//void displayData(double vehicleSpeed, double speedLimit)
//{
//	int totalVehicles = 0;
//	int ticketVehicles = 0;
//
//	//for (totalVehicles = 0;;);
//
//	totalVehicles = inputData(vehicleSpeed, speedLimit);
//
//	cout << "Thank you for using our AHP program." << endl;
//} 
Thank you so much I can actually understand this much better than using the displayData function.
Good deal!
You need one more function in this program...

line 73 add: CheckPlate("E", "H");

1
2
3
4
5
6
7

int CheckPlate(string a,string b){
if (drivers initials == a+b)
{
 cout << "This driver is good to go. Let him off with a warning." << endl;
}
}



Yeah, that'll work...
Topic archived. No new replies allowed.