0% found this document useful (0 votes)
118 views3 pages

C++ Bus Fare Calculation Program

The document describes a C++ program to calculate bus fare based on distance traveled. The fare is Rs. 5 for 0-10 km, Rs. 10 for 10-20 km, Rs. 20 for 20-30 km, and Rs. 1.5 per km for distances over 30 km. The program takes distance as input and outputs the calculated fare.

Uploaded by

Musariri Talent
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views3 pages

C++ Bus Fare Calculation Program

The document describes a C++ program to calculate bus fare based on distance traveled. The fare is Rs. 5 for 0-10 km, Rs. 10 for 10-20 km, Rs. 20 for 20-30 km, and Rs. 1.5 per km for distances over 30 km. The program takes distance as input and outputs the calculated fare.

Uploaded by

Musariri Talent
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Write a C++ program to calculate the fare for the passengers travelling in a bus.

When a passenger
enters the bus, the conductor should ask “ what distance will you travel ? “ On knowing the distance
from passenger the conductor mentions the fare to the passenger, according to the following criteria-

If the passenger is in b/w greater than or 0-10 km then fare is 5/-

If the distance is b/w 10-20 km then fare is 10/-

If the distance is b/w 20-30 km then fare is 20/-

If it is greater than 30 km then calculate fare as ₹ 1.5 per kilometer.

THEORY:

Basic Input / Output in C++

C++ comes with libraries which provide us many ways for performing input and output. In C++ input and
output is performed in the form of sequence of bytes or more commonly known as streams.

Input Stream: If the direction of flow of bytes is from device(for example: Keyboard) to the main
memory then this process is called input.

Output Stream: If the direction of flow of bytes is opposite, i.e. from main memory to device( display
screen ) then this process is called output.

Header files available in C++ for Input – Output operation are:

iostream: iostream stands for standard input output stream. This header file contains definitions to
objects like cin, cout, cerr etc.

iomanip: iomanip stands for input output manipulators. The methods declared in this files are used for
manipulating streams. This file contains definitions of setw, setprecision etc.

fstream: This header file mainly describes the file srteam. This header file is used to handle the data
being read from a file as input or data being written into the file as output.

In C++ articles, these two keywords cout and cin are used very often for taking inputs and printing
outputs. These two are the most basic methods of taking input and output in C++. For using cin and cout
we must include the header file iostream in our program.
In this article we will mainly discuss about the objects defined in the header file iostreamlike cin and
cout.

Standard output stream (cout): Usually the standard output device is the display screen. cout is the
instance of the ostream class. cout is used to produce output on the standard output device which is
usually the display screen. The data needed to be displayed on the screen is inserted in the standard
output stream (cout) using the insertion operator (<<).

#include <iostream>

using namespace std;

int main()

char sample[] = “GeeksforGeeks”;

cout<<samle<<” – A computer science portal for geeks”;

return 0;

As you can see in the above program the insertion operator(<<) insert the value of the string variable
sample followed by the string “A computer science portal for geeks” in the standard output stream cout
which is then displayed on screen.

standard input stream (cin): Usually the input device is the keyboard. cin is the instance of the class
istream and is used to read input from the standard input device which is usually keyboard.

The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator
extracts the data from the object cin which is entered using the keboard.

SOUCE CODE:

#include<iostream>

using namespace std;

int main()

float fare,km;
cout<<“How much distance will you travel: “;

cin>>km;

if(km>=0&&km<=10)

cout<<“fare is 5/-“;

else if(km>=11&&km<=20)

cout<<“fare is 10/-“;

else if(km>=21&&km<=30)

cout<<“fare is 20/- “;

else if(km>=30)

fare=km*1.5;

cout<<“fare is “<<fare;

return 0;

OUTPUT:

How much distance will you travel: 53

Fare is: 79.5

Common questions

Powered by AI

Implementing a switch statement in place of the current if-else if construct for fare calculation might not offer significant advantages, as switch statements are typically best suited for exact matching cases rather than range evaluations. Since the program involves checking ranges of distances to determine fares, the current if-else structure is more appropriate and flexible. Switch statements in C++ do not handle ranges directly without additional logic, making them less efficient in this context .

The program prompts the user to input the distance they plan to travel. It then uses a series of if-else statements to determine the fare based on the distance entered. If the distance is between 0 and 10 km, the fare is 5/-; between 11 and 20 km, the fare is 10/-; between 21 and 30 km, the fare is 20/-. For distances greater than 30 km, the fare is calculated as 1.5 times the distance in km .

For distances greater than 30 km, the program calculates the fare using the formula fare = distance * 1.5. This calculation is performed within an else-if block that checks if the distance is equal to or greater than 30 km. The calculated fare is then outputted to the user .

To manage invalid input, the program could integrate error checking mechanisms such as verifying the input type and range. This can be done by checking if 'cin.fail()' returns true when a non-numeric input is provided or if the distance entered is negative. The 'cin' stream should be cleared with 'cin.clear()' and erroneous data discarded with 'cin.ignore()' to ensure the program continues to run smoothly. Moreover, providing user prompts to re-enter correct data can guide recovery from such input errors .

The primary benefit of using 'cout' and 'cin' is their ease of use and integration within C++, providing straightforward mechanisms for displaying output and capturing input. They simplify the creation of basic command-line user interfaces and support formatted input and output. However, a drawback is that 'cin' can be less intuitive for handling complex data inputting needs, as it does not inherently support error checking or recovery without additional, often cumbersome, logic. Additionally, for large-scale data processing or file-based operations, using 'cin' and 'cout' might not be as efficient as file streams or advanced libraries designed for high-performance I/O operations .

The 'cin' and 'cout' objects in C++ are essential for input and output operations. 'cin' is used to take input from the standard input device, usually a keyboard. It is employed with the extraction operator ('>>') to read data entered by the user. On the other hand, 'cout' is used to send output to the standard output device, generally a display screen, and is used with the insertion operator ('<<'). Their implementation in the program facilitates communication between user input and output display, highlighting their role in creating interactive command-line applications .

In C++, header files are used to include declarations and definitions for functions, objects, or libraries needed in a program. They help structure programs by allowing code reuse and modularity. In the provided program, the '#include<iostream>' directive imports the iostream library, which contains the declarations for input/output stream classes and objects like 'cin', 'cout', essential for performing input and output operations effectively .

The iostream library is included in the program to provide functionalities for input and output operations. It defines objects like 'cin' for taking input from the user (typically from the keyboard) and 'cout' for displaying output (typically on the screen). These objects are used in conjunction with the extraction operator ('>>') and the insertion operator ('<<') respectively .

The program might encounter several edge cases such as handling negative values of distance, which are nonsensical in the context of travel. Since the program does not explicitly check for negative inputs, entering such a value would result in no output or an improper result. Another limitation is that the fare calculation for the boundary of 30 km is not exclusive to integer distances greater than 30; it potentially misleads as "greater than or equal to 30". Such scenarios can lead to logical errors if not appropriately managed with input validation checks .

Including input validation would enhance the program's robustness by ensuring only valid input is processed, thus preventing erroneous calculations or crashes. To implement this, one could introduce a loop that prompts for input until a positive numeric value is entered. For example: place the 'cin>>km;' inside a loop that checks if 'cin.fail()' is true, indicating a failed extraction due to wrong data type, or if 'km < 0'. This loop would clear the input buffer using 'cin.clear()' and disregard erroneous inputs with 'cin.ignore()', prompting for correct input thereafter .

You might also like