Need help with code.

Write a C++ program that includes a function to calculate the discount applicable on the price of an item. Your function should have three arguments: the price as a reference parameter to a double, the discount as a double value, and a bool to indicate if the discount is calculated as a percentage or a fixed amount. Call the parameter to indicate whether the discount is a fixed amount, or a percentage, fixed. When fixed is true, it will indicate that the discount is a fixed amount and when it is false, the discount is a percentage. Your function should calculate the discount and modify the price of the item accordingly.
Your function should check that the discount is not negative and that the price does not drop to zero or below zero after applying the discount. Use the assert() function to ensure that the discount is not negative and that the price does not drop to zero or below zero once the discount is applied.
Test you program with the following input :

235.97 7.35 false
5430.55 120.00 true
856.00 -12.5 false
120.00 130.00 true

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>
#include<cassert>

double calcDiscount(double& price, double discount, bool type);

int main()
{
  char type;
  double price, discount, disAmt;
  std::cout<<"Enter price of the item = ";
  std::cin>>price;
  disAmt = calcDiscount(price, discount, type);
  price = price - disAmt;
  assert(price > 0);
  std::cout<<"Price after discount = "<<price<<"\n";
}

double calcDiscount(double& price, double discount, bool type)
{
  std::cout<<"Enter 1 to calculate discout as percentage.\n"
           <<"OR\n"
           <<"Enter 0 to calculate discount as fixed amount.\n";
  std::cin>>type;
  if(type)
  {
    std::cout<<"Enter discount percentage = ";
    std::cin>>discount;
    discount = (price * (discount/100.0));
  }
  else
  {
    std::cout<<"Enter discount amount = ";
    std::cin>>discount;
  }
  assert(discount >= 0);
  return(discount);
}


This is what I got so far
Shouldn't discount and discount type be obtained in main() and used in calcDiscount()

Also calcDiscount() should calculate the discount and apply it to the price. There is no indication in the requirements for this function to return a value.
so updated main and calcDiscount

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
#include<cassert>

double calcDiscount(double& price, double discount, bool type);

int main()
{
  double price;
  std::cout << "Enter price of the item = ";
  char type;
  std::cin >> price;
  std::cout << "Enter 1 to calculate discount as percentage.\n"
            << "OR\n"
            << "Enter 0 to calculate discount as fixed amount.\n";
  std::cin >> type;
  double effective_discount;
  if (type == '1') {
    std::cout << "Enter discount percentage = ";
    double discountPercent;
    std::cin >> discountPercent;
    effective_discount = calcDiscount(price, discountPercent, true);
  } else {
    std::cout << "Enter discount amount = ";
    double discount;
    std::cin >> discount;
    effective_discount = calcDiscount(price, discount, false);
  }
  std::cout << "discount = " << effective_discount << "\n";
  std::cout << "Price after discount = " << price << "\n";
}

double calcDiscount(double& price, double discount, bool as_percentage)
{
    assert(discount >= 0);
    if (as_percentage) {
        discount = price * discount / 100.0;
    }
    price -= discount;
    assert(price > 0);
    return discount;
}
Perhaps something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <cassert>

void calcDiscount(double& price, double discount, bool type);

int main() {
	double price {}, discount {};
	bool type {};

	std::cout << "Enter price of the item = ";
	std::cin >> price;

	std::cout << "Enter 1 to calculate discount as percentage.\n"
		<< "OR\n"
		<< "Enter 0 to calculate discount as fixed amount.\n";
	std::cin >> type;

	if (type)
		std::cout << "Enter discount percentage = ";
	else
		std::cout << "Enter discount amount = ";

	std::cin >> discount;

	calcDiscount(price, discount, type);
	std::cout << "Price after discount = " << price << '\n';
}

void calcDiscount(double& price, double discount, bool as_percentage) {
	assert(discount >= 0);

	if (as_percentage)
		discount = price * discount / 100.0;

	price -= discount;
	assert(price > 0);
}

Topic archived. No new replies allowed.