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.
#include<iostream>
using namespace std;
int main(){
char type;
cout<<"Enter the fuel type: \n";
cin>>type;
int number_of_litres;
cout<<"Enter the number of litres needed\n";
cin>>number_of_litres;
char c = toupper(type);
if(number_of_litres>1){
switch(c){
case 'P':
cout<<"The price is:"<< number_of_litres * 200<<" \n"; break;
case 'D': cout<<"The price is:"<< number_of_litres * 150<<" \n"; break;
default:
cout<<"Wrong fuel type\n";
}
}
}
Comments
Leave a comment