In: Computer Science
Write a program that contains a function that takes in three arguments and then calculates the cost of an order. The output can be either returned to the program or as a side effect.
1. Ask the user via prompt for the products name, price, and quantity that you want to order.
2. Send these values into the function.
3. Check the input to make sure the user entered all of the values. If they did not, or they used a string where a number should be, output an error message to the console.
4. Otherwise, do the necessary calculation and output to the console the following:
<x number> of <product name> will cost you <calculated value>
Outputs:
Enter product name bread
Enter quantity 30
Enter price 15
30 of bread will cost you 450
// If user enters wrong input it declares mesage in the console.
Enter product name toy
Enter quantity 0
Enter price 0
Sorry wrong Input !
Code:
#include <iostream>
using namespace std;
//This function helps to calculate the amount of the item by multiplying the price and quantity.
void function(string name, int quantity,double price)
{
double res=price*quantity;
cout<< quantity <<" of "<<name << " will
cost you "<<res;
}
int main()
{
//variable declaration
string productName;
int quantity;
double price;
//Try catch is for wrong input handlings
try {
// get user inputs
cout<<"Enter product name ";
cin>>productName;
cout<<"Enter quantity ";
cin>>quantity;
cout<<"Enter price ";
cin>>price;
if( quantity==0 || price==0 )
{
throw "Sorry wrong Input !";
}
else
{
function(productName,quantity,price);
}
}
//Prints the message passed by the throw call.
catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}