Answer to Question #249797 in C++ for Urooj

Question #249797

Write a program for an automatic fuel filling service. The program should ask the user for the fuel type using a char (e.g. 'p' for petrol or 'd' for diesel) and the number of litres needed (int), and finally display the price of the charge.


The program should have the following constraints:


It should be case sensitive to user input, e.g. "P" and "p" are treated equivalently.


The program should only display the price if a valid fuel type has been entered and fuel is actually needed!


You can only use one if-statement and one switch statement.


If you haven't already, demonstrate how the two statements can be nested without impacting the solution.


1
Expert's answer
2021-10-12T00:37:06-0400
#include<iostream>
using namespace std;
int main()
{
	char n;
	int x;
	cout<<"Enter the character for the type of fuel: ";
	cin>>n;
	cout<<"Enter the number of litres needed: ";
	cin>>x;
	if(n=='P' || n=='p' ||n=='d' ||n=='D'){
		int price=100*x;
		cout<<"The price of the charge is: "<<price;
	}
	else{
		cout<<"Invalid type of fuel";
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS