I must create a function that receives the price of a product and the payment method, it must calculate its discounted price, taking into account that the discount is 20% if the purchase is in cash and 0% if it is on credit.
The problem is that when I am going to call the function, I don't know how to make it receive the parameter that I want, in this case I want to use the variable (priceProduct) so that when I call the function I relate it to the variable that it creates in the same function which is (p) so that you know that this value is the one that corresponds to it.
#include <iostream>
using namespace std;
int main (){
int precioProducto;
cout<<"Ingrese el precio del producto: "<<endl;
cin>>precioProducto;
producto(precioProducto);
return 0;
}
void producto (int p){
int total,op,totalProvicional;
switch(op){
case 1: totalProvicional = (p * 20 / 100);
total = p - totalProvicional;
cout<<"El precio que debe pagar es: "<<total;break;
case 2: cout<<"El precio que debe pagar es: "<<p;break;
default: for(int i=0; i<1; i++){
cout<<"Numero invalido, ingrese uno que lo sea: "<<endl;
cin>>op;
i--; }
}}
This is the problem that appears
The compiler is telling you that it doesn't know the function
producto
exists because it comes after the functionmain
.You can fix it by defining it before the
main
.Or if you want the function to stay in place, tell it of its existence by declaring it first. So:
If you look closely, the declaration is very similar to the definition, only without a body or brackets and ends with
;
.