Question

In: Computer Science

Write a program that contains a function that takes in three arguments and then calculates the...

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>

Solutions

Expert Solution

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;
}


Related Solutions

Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
Write a program that takes two command line arguments at the time the program is executed....
Write a program that takes two command line arguments at the time the program is executed. You may assume the user enters only decimal numeric characters. The input must be fully qualified, and the user should be notified of any value out of range for a 23-bit unsigned integer. The first argument is to be considered a data field. This data field is to be is operated upon by a mask defined by the second argument. The program should display...
JavaScript Write a function called "first" that takes in two arguments - the first is an...
JavaScript Write a function called "first" that takes in two arguments - the first is an argument called arr that is an array of numbers, the second is an optional number argument called num(hint: you'll need a default value - look back at the slides). If "num" was not passed in (since it's optional), the "first" function will return an array containing the first item of the array. If a value for "num" was given, the "first" function will return...
Write a function called draw_card. It takes no arguments and returns an integer representing the value...
Write a function called draw_card. It takes no arguments and returns an integer representing the value of a blackjack card drawn from a deck. Get a random integer in the range 1 to 13, inclusive. If the integer is a 1, print "Ace is drawn" and return 1. If the integer is between 2 and 10, call it x, print "<x> is drawn" and return x (print the number, not the string literal "<x>"). If the number is 11, 12,...
Write a function named hasNValues which takes an array and an integer n as arguments. It...
Write a function named hasNValues which takes an array and an integer n as arguments. It returns true if all the elements of the array are one of n different values. If you are writing in Java or C#, the function signature is int hasNValues(int[ ] a, int n) If you are writing in C or C++, the function signature is int hasNValues(int a[ ], int n, int len) where len is the length of a Note that an array...
The C function funsum below takes three arguments -- an integer val and two function pointers...
The C function funsum below takes three arguments -- an integer val and two function pointers to functions f and g, respectively. f and g both take an integer argument and return an integer result. The function funsum applies f to val, applies g to val, and returns the sum of the two results. Translate to RISC-V following RISC-V register conventions. Note that you do not know, nor do you need to know, what f and g do, nor do...
In a program, write a function that accepts two arguments: a list, and a number n....
In a program, write a function that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display all of the numbers in the list that are greater than the number n. The program should ask for a list of numbers from the user as well as a value (a, b, c)--> inputs from user. After that, each number in that list should be compared to that value (a or b or...
Write a function which takes two words as string arguments and checks whether they are anagrams.
Write a function which takes two words as string arguments and checks whether they are anagrams.
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
Write functions in Python IDLE that do the following: i) A function that takes 2 arguments...
Write functions in Python IDLE that do the following: i) A function that takes 2 arguments and adds them. The result returned is the sum of the parameters. ii) A function that takes 2 arguments and returns the difference, iii) A function that calls both functions in i) and ii) and prints the product of the values returned by both.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT