Assignment
Problem Statement: Challan Calculator for Traffic Violation
You are required to write a program for calculating challan for traffic violations. The basic idea is that user will enter the type of violation and number of days passed till date of challan. The challan calculator will calculate the challan based upon the type of violation and number of days.
Detailed Description:
You are required to take input from user for type of traffic violation.
User will enter ‘1’ if the violation type is “For breaking traffic signal”
User will enter ‘2’ if the violation type is “For over speeding”
User will enter ‘3’ if the violation type is “For not wearing seat belt”
If user enters any other number except 1, 2 or 3, the program will print a message “Enter a valid type of traffic violation”.
After printing this message program will start again from beginning and prompt user to enter the valid type of traffic violation.
If user enters ‘1’ the amount of fine is Rs.500, if ‘2’ then Rs.300 and if user enters ‘3’ then fine will be Rs.200.
Now, program will prompt user to enter number of days passed till challan date.
If number of days are less than or equal to 10, then the total challan will be same as amount of fine.
If number of days are more than 10 or less than/equal to 30, then total challan will be double of amount of fine.
If number of days are more than 30, then 50% of fine will be added in double of amount of fine. i.e. 2 times fine + 1/2 times fine.
After displaying the total challan to user, the program should ask if the user wants to calculate another challan. The user can enter ‘y’ or ‘n’ as choices for yes or no. Program will handle user’s choice in both upper and lower cases.
If the user enters ‘y’ or ‘Y’, then program should clear the screen and start the whole process again or terminate the program otherwise.
Sample Output:
In the following screenshot, user entered 2 as violation type and 20 as number of days passed till challan. After showing the calculated challan, program prompts user whether he/she wants to calculate another challan.
User entered y as choice and the program has started the whole process from beginning again.
After showing the error message, the program starts the whole process again upon user presses any key from the keyboard.
Solution:-
using namespace std;
#include<iostream>
#include<conio.h>
main()
{
int type; //to store the type to traffic violation
int fine; //to store the amount of fine against violation type
int duration; //to store the number of days passed till challan date
int challan; //to store the challan against violation type and number of days passed
char choice = ‘y’; //to get user’s choice whether to calculate another challan or not
do {
system(“cls”); //clear screen function
//following 4 lines of code are printing information about how to select a specific traffic violation
cout << “******* CHALLAN CALCULATOR FOR TRAFFIC VIOLATIONS *******” << endl <<endl;
cout << “Enter ‘1’ for breaking signal” << endl;
cout << “Enter ‘2’ for over speeding” << endl;
cout << “Enter ‘3’ for not wearing seat belt” << endl;
// Taking input from user to select a violation type in variable “type”
cout << endl << “Enter the type of traffic violation (1, 2, or 3) : ” ;
cin >> type;
//if user enters anything except 1, 2, or 3, the error message will be shown and do-while loop will start again due to continue statement
if(type <1 || type > 3)
{
cout << endl <<“Enter a valid type of traffic violation”;
//getch() function will get a character from user. Its used here to hold the output screen before executing continue statement so that user can see the error message before screen gets cleared
getch();
continue;
}
//Taking input for number of days passed till challan date in variable duration
cout << “Enter the number of days passed till challan date : “;
cin >> duration;
//Checking value of “type” variable and assigning corresponding amount of fine to variable “fine”
switch(type)
{
case 1:
fine = 500;
break;
case 2:
fine = 300;
break;
case 3:
fine = 200;
break;
}
//Calculating total challan according to number of days passed till challan date
if(duration <=10)
{
challan = fine;
}
else if(duration <=30)
{
challan = fine * 2;
}
else
{
challan = fine * 2 + (fine / 2);
}
cout << “The total challan is Rs.” << challan << endl << endl;
//taking user’s choice whether to calculate another challan or not
cout << “Do you want to calculate another challan? (y/n) : “;
cin >> choice;
//loop will start again only if the choice is y or Y
}while(choice == ‘y’ || choice == ‘Y’);
}
Download Solution file from here
CS201_Assignments_Solution_1_Spring_2012