exercise with function c++

Define a function that, given the sales price of a total VAT product, returns the VAT-free price and the VAT amount (VAT at 20%)

I write this code is good for you?

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
  #include <iostream>

using namespace std;

double withIva(double &t,double &i,double &n)

{

n=(100 * t) / 120;
i=t-n;

}

int main()
{
    double totale;
    double netto;
    double iva;
    cin>>totale;
withIva(totale,iva,netto);
cout<<netto<<endl<<iva;



}
Last edited on
VAT amount = Total * 20/100
VAT free price = Total - VAT amount


Percentage Value = Number * Percentage / 100

1
2
3
4
5
6
7
8
double withIva(double &t,double &i,double &n)

{

n= t * 20/100;
i=t-n;

}
Ok but in the cose I must use "void" or it is right?
If the total (vat included) is 80 the vat results 16 and the vat-free price results 64 that isn't right.....
Last edited on
Yeah you need to make the function return void. You can also make double &t a const (const double &t for stressing that it won't be changed by the function.
I understand about const double &t , but I don't understand well about I must use void in the code, can you rewrite all the code right please?
12:1: warning: no return statement in function returning non-void [-Wreturn-type]

You have promised to return a double:
double withIva( double&, double&, double& );

The compiler warns, for you have no return statement in your function.

If you want to return a value, then do so.
If there is no value to return, then say so:
void withIva( double&, double&, double& );


Note that all your function parameters are by reference.
Why? Do they all need to be?
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
#include <iostream>
#include <iomanip>

const double percent_vat = 20.0 ;

void decompose( double sales_price, double& price_before_vat, double& vat_amount )
{
    // invariant: sales_price >= 0.0
    price_before_vat = sales_price * 100 / ( 100 + percent_vat ) ;
    vat_amount = sales_price - price_before_vat ;
}

int main()
{
    const double list_price = 123.43 ;
    const double vat = list_price * percent_vat / 100 ;
    const double sales_price = list_price + vat ;
    std::cout << std::fixed << std::setprecision(2)
              << "    billed: " << list_price << " + " << vat
              << " == " << sales_price << '\n' ;

    double price_before_vat ;
    double vat_amount ;
    decompose( sales_price, price_before_vat, vat_amount ) ;

    std::cout << "decomposed: " << price_before_vat << " + " << vat_amount
              << " == " << sales_price << '\n' ;
}
I understand about const double &t , but I don't understand well about I must use void in the code, can you rewrite all the code right please?

void is simply a keyword in the language that means 'no type' or 'nothing' depending on context.

void foo()
{
cout <<"boring function";
}

this function does not return any values that it computed, because it had none. The type of the function is void, because that is how c++ works: you must put a return type on a function in c++, and if you don't have one, that type is the void keyword.

compare to
double f(double x);
{
return x/3.14;
}
this one returns a double, a value that you can use and of a type you can do things with.
http://www.cplusplus.com/forum/beginner/250697/#msg1103918
with codeblocks I haven't this warning....
http://www.cplusplus.com/forum/beginner/250697/#msg1103924
But if I must "returns the VAT-free price and the VAT amount (VAT at 20%)" so I can' t use voi or I make a mistake?
The "returns" is about what the function achieves, not how.
Your:
1
2
3
double netto;
double iva;
withIva( 42, iva, netto );

function call writes something to the caller's variables netto and iva.

Logically, it does return data via is reference parameters.


There several ways to "return" two values:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// return a value, update other via reference
double taxless( double gross, double& tax ) {
  tax = ... // iva
  return ... // netto
}

// update both via reference, return nothing
void taxless( double gross, double& tax, double& net ) {
  tax = ... // iva
  net = ... // netto
}

// return complex object that contains multiple member values
std::tuple<double,double> taxless( double gross ) {
  double iv = ...
  double ne = ...
  return std::tuple<double,double>( ne, iv );
}
In this exercise I must calculate and return TWO values so what I must write the code?? In this case I can't use void....
Last edited on
there are two ways to do this.

method 1:
void returnstwo( double in1, double in2, double &out1, double&out2)
{
blah blah;
out1 = result1;
out2 = result2;
}
..
main
returnstwo(11.3,2.718, r1,r2);
//here r1 and r2 are your two answers.

method 2, create your own return type or use standard pair or other container for it. (I made my own because one day you will need to see how to do this, pair is fine for 2 values)

struct s
{
double res1;
double res2;
}

s returns2(double in1, double in2);
{
s ans;
ans.res1 = result1;
ans.res2 = result2;
return ans;
}

etc. so the point is that you can use any container (vector, array/pointer, pair/point) or your own user defined type (class or struct) to return a group of values from a function. Use a container if you can, and make your own type only if you need that type anyway OR there is no better way to do it (say you had a string, a double, and a myclass to return instead of just a couple of doubles... that would be tough to do without a custom wrapper).

method 1 is preferred here because the problem is very simple, but method 2 is what you will see more and more as you get into more advanced coding problems.
Last edited on
I Haven't studied "struct" so I must use first method.
But what are 11.3,2.718?

Can you write all code please?
Really many thanks!

void returnstwo( double in1, double in2, double &out1, double&out2)
{
blah blah; ?????
out1 = result1;
out2 = result2;
}
..
main
returnstwo(11.3,2.718, r1,r2);
But what are 11.3,2.718?

examples.

Can you write all code please?
Really many thanks!

yes, I can write the code. Can you? Give it a try. Me writing the code won't help you pass the class, it just puts you further behind than you already are.

start small. I will give you working code, this time, since the pseudocode seems to have confused you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void getpiande(double &pi, double &e)
{
    pi = 3.14;
    e = 2.718;
}

int main()
{
   double PI = 0.0;
   double E = 0.0;
   getpiande(PI,E);
   cout << PI << endl;
   cout << E << endl;
}

that demonstrates what you asked. Now use that idea in your code.
Last edited on
But this isn't right?

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


#include <iostream>


using namespace std;

void withVAT(const double t, double &i,double &n)

{

n=(100 * t) / 120;
i=t-n;


}


int main()
{
    double total;
    double netto;
    double vat;
    cin>>totale;
withVAT(total,vat,netto);
cout<<netto<<endl<<vat;



}

Last edited on
cin>>totale; //typo

just make it a habit when dealing with doubles to make doubles. divide by 120.0. You don't need that here, because t helps you out, but one day you will be glad you are doing this as a habit.
http://www.cplusplus.com/forum/beginner/250697/#msg1103918
I have seen this:
12:1: warning: no return statement in function returning non-void [-Wreturn-type]

What kind of compiler you use? I use codeblocks but I had not this warning.
Many thanks.
Codeblocks (CB) is not a compiler. It uses a compiler. Its installer probably installed a compiler too.

When you say "compile" in CB, the CB runs the compiler with some options (program code's filename, etc).
There must be in CB a way to select/add/change those options.

The most likely compiler under CB is (some version) GCC. GCC has many options.

The warning message shows that if one has option -Wreturn-type, then one gets this warning.
If one has option -Wall, then one has -Wreturn-type too.

Options -Wall -Wextra is quite good.


GCC has also option -std. It requires a value. For example: -std=c++11. The possible values depend on the version of GCC. The std option determines which version of C++ is supported.
Topic archived. No new replies allowed.