BTW calculation

Hello World,

im new in C++ programming. I would like to calculate the VAT rate of 6 items.
I want to design a program that increases the price of an item with the correct VAT rate. The user enters the item price and item code of 6 items. The article codes are: 1, 2, 3, 4, 5 and 6. The articles with codes 1 to 3 are subject to the 21% VAT rate. Articles 3 to 6 are subject to the 9% VAT rate. Determine the total amount of the imported items. Determine the total VAT amount. Display the total gross amount of the imported items and the total VAT amount on the screen.
can someone point me in the right direction?

thanks!
J
When starting something like this, the best starting point is to first work out how to do it using pen & paper. Then set out the steps that need to be done to do it. Then expand those steps so that they can be given to someone else who doesn't know anything about the problem so that they can do it as well. These steps are your starting point. Then expand this into a program design and then finally code the program from the design. In programming, the last thing you do is write the code!

Re the code. Do you know how to get user input from the keyboard? How to output data to the screen?
Thank you for the advice seeplus! I made the following program, but I think that is could be smaller but I don’t understand how to use a loop yet


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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>

using namespace std;

int main()
{
	float bedragArtikel1;
	cout << "Geef de prijs van artikel 1\n";
		cin >> bedragArtikel1;

		int artikelCode1;
		cout << "Geef de code van artikel \n";
		cin >> artikelCode1;

			if (artikelCode1 > 6)
				cout << "Voer een artikelcode in tussen de 1 en de 6\n";


	float bedragArtikel2;
	cout << "Geef de prijs van artikel 2\n";
		cin >> bedragArtikel2;

		int artikelCode2;
		cout << "Geef de code van artikel 2\n";
		cin >> artikelCode2;

			if (artikelCode2 > 6)
				cout << "Voer een artikelcode in tussen de 1 en de 6\n";


	float bedragArtikel3;
	cout << "Geef de prijs van artikel 3\n";
		cin >> bedragArtikel3;

		int artikelCode3;
		cout << "Geef de code van artikel 3\n";
		cin >> artikelCode3;

			if (artikelCode3 > 6)
				cout << "Voer een artikelcode in tussen de 1 en de 6\n";


	float bedragArtikel4;
	cout << "Geef de prijs van artikel 4\n";
		cin >> bedragArtikel4;

		int artikelCode4;
		cout << "Geef de code van artikel 4\n";
		cin >> artikelCode4;

			if (artikelCode4 > 6)
				cout << "Voer een artikelcode in tussen de 1 en de 6\n";


	float bedragArtikel5;
	cout << "Geef de prijs van artikel 5\n";
		cin >> bedragArtikel5;

		int artikelCode5;
		cout << "Geef de code van artikel 5\n";
		cin >> artikelCode5;

			if (artikelCode5 > 6)
				cout << "Voer een artikelcode in tussen de 1 en de 6\n";


	float bedragArtikel6;
	cout << "Geef de prijs van artikel 6\n";
		cin >> bedragArtikel6;

		int artikelCode6;
		cout << "Geef de code van artikel 6\n";
		cin >> artikelCode6;

			if (artikelCode6 > 6)
				cout << "Voer een artikelcode in tussen de 1 en de 6\n";


	cout << "Het totale brutobedrag is: " << bedragArtikel1 + bedragArtikel2 + bedragArtikel3 + bedragArtikel4 + bedragArtikel5 + bedragArtikel6 << " Euro\n";
	
	

	if (artikelCode1 <= 3)
	{
		bedragArtikel1 = (bedragArtikel1 * 0.21);
	}
	if (artikelCode1 > 3)
	{
		bedragArtikel1 = (bedragArtikel1 * 0.09);
	}

	if (artikelCode2 <= 3)
	{
		bedragArtikel2 = (bedragArtikel2 * 0.21);
	}
	if (artikelCode1 > 3)
	{
		bedragArtikel2 = (bedragArtikel2 * 0.09);
	}

	if (artikelCode3 <= 3)
	{
		bedragArtikel3 = (bedragArtikel3 * 0.21);
	}
	if (artikelCode1 > 3)
	{
		bedragArtikel3 = (bedragArtikel3 * 0.09);
	}

	if (artikelCode4 <= 3)
	{
		bedragArtikel4 = (bedragArtikel4 * 0.21);
	}
	if (artikelCode4 > 3)
	{
		bedragArtikel4 = (bedragArtikel4 * 0.09);
	}

	if (artikelCode5 <= 3)
	{
		bedragArtikel5 = (bedragArtikel5 * 0.21);
	}
	if (artikelCode5 > 3)
	{
		bedragArtikel5 = (bedragArtikel5 * 0.09);
	}

	if (artikelCode6 <= 3)
	{
		bedragArtikel6 = (bedragArtikel6 * 0.21);
	}
	if (artikelCode6 > 3)
	{
		bedragArtikel6 = (bedragArtikel6 * 0.09);
	}


	cout << "De totale BTW is " << bedragArtikel1+bedragArtikel2+bedragArtikel3+bedragArtikel4+bedragArtikel5+bedragArtikel6 << " Euro";

		
	return 0;


}


How can I make this program smaller?
Last edited on
It could be simplified using a basic for loop:

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

int main()
{
	double total_gross = 0;
	double total_vat = 0;

	for (int i = 0; i < 6; ++i) {
		double val;

		std::cout << "Please enter price: ";
		std::cin >> val;

		int code;

		std::cout << "Please enter code: ";
		std::cin >> code;

		const double vat = code <= 3 ? val * 0.21 : val * 0.09;

		total_vat += vat;
		total_gross += val + vat;
	}

	std::cout << "Total gross value is: " << total_gross << std::endl;
	std::cout << "Total VAT is: " << total_vat << std::endl;
}


Note no input checking!

The for loop starts with i = 0, it then tests if i < 6 and if it is, executes the body of the for loop (between the { and } ). i is then incremented. if i < 6 executes the body, increments i etc until i is not < 6 when the statement following the for body is executed.
Last edited on
code will get smaller as you learn more things. Right now, without knowing anything about a loop or a container, the only major change you can do is eliminate the double if statements. A container is not strictly needed if you only want this much; it would be needed if you want the individual inputs again later for another purpose.

consider:

if (artikelCode6 <= 3)
{
bedragArtikel6 = (bedragArtikel6 * 0.21);
}
else //if (artikelCode6 > 3) //you know this if it is not <= 3 then it must be > 3
{
bedragArtikel6 = (bedragArtikel6 * 0.09);
}

also basic math will help
1.09 * x
is the same as adding 9% to the original.
eg in the above code:

const double vat = code <= 3 ? val * 0.21 : val * 0.09;
total_vat += vat;
can be done with simply:
total_gross += code <= 3 ? val * 1.21 : val * 1.09;

if you need the total tax and total price distinct, you can't take this shortcut, if you just need the final total, you can...
Last edited on
Hello Jamesboom,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Looking at your program you wrote this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int main()
{
    float bedragArtikel1;

    cout << "Geef de prijs van artikel 1\n";
    cin >> bedragArtikel1;

    int artikelCode1;

    cout << "Geef de code van artikel \n";
    cin >> artikelCode1;

    if (artikelCode1 > 6)
        cout << "Voer een artikelcode in tussen de 1 en de 6\n";


    float bedragArtikel2;

A good start.
First I would suggest using "double"s instead of "float"s. These days a "double" is the preferred floating point type. Although "double"s" and float"s do have problems storing some decimal numbers.

Line 17 , the if statement, has some problems.

First you are only checking for numbers greater than 6. What about (0) zero or numbers less than zero?

Then should the if statement be true you print out an error message and continue with line 21 keeping the invalid number. Not what you want.

Doing a little work on what you started with I came up with this:
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
#include <iostream>
#include <iomanip>  // <--- used for "std::fixed and std::setprecision".

using namespace std;

int main()
{
    constexpr double VAT21{ 0.21 }, VAT9{ 0.09 };

    double totaleBruto{}, totaleBTW{};

    double bedragArtikel1;

    std::cout << std::fixed << std::setprecision(2);

    cout << "\nGeef de prijs van artikel 1: ";  // <--- Changed.
    cin >> bedragArtikel1;

    totaleBruto += bedragArtikel1;

    int artikelCode1;

    do
    {
        cout << "Geef de code van artikel 1: ";  // <--- Changed.
        cin >> artikelCode1;

        if (artikelCode1 < 1 || artikelCode1 > 6)
            cout << "\n     Voer een artikelcode in tussen de 1 en de 6\n\n";  // <--- Changed.

    } while (artikelCode1 < 1 || artikelCode1 > 6);

    double bedragArtikel2{};

When it comes to "bedragArtikel1" I am not sure what to do to consider this a valid number.

Line 14 is used for output later in the program.

Lines 23 - 31. I do not know of any other way of doing this. It is possible that there is, but I am not familiar with it.

You can read about the do/while loop at https://www.learncpp.com/ and specifically at: https://www.learncpp.com/cpp-tutorial/56-do-while-statements/

For "for loops" at https://www.learncpp.com/cpp-tutorial/57-for-statements/

I have found this site to be very helpful.

As you progress you will find that loops become part of most programs.

As jonnin mentioned some of the if statements can be shortened.
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
double vat = VAT9;

if (artikelCode1 < 4)
{
    vat = VAT21;
    //bedragArtikel1 = (bedragArtikel1 * VAT21);
}

bedragArtikel1 *= 1 + vat;

totaleBTW += bedragArtikel1 * vat;

//if (artikelCode1 > 3)
//{
//    bedragArtikel1 = (bedragArtikel1 * VAT9);
//}

vat = VAT9;

if (artikelCode2 < 4)
{
    vat = VAT21;
}

bedragArtikel2 *= 1 + vat;

totaleBTW += bedragArtikel2 * vat;

This may be my personal choice, but I try to avoid using "<=" as much as possible. It does have its use on occasion, but could be a problem in most for loops. You are free to use what you like or are use to.

Line 9 is a shorter way of writing bedragArtikel1 = (bedragArtikel1 * 0.09);. The () are not needed as what is on the rhs of "=" is done first. The () make no difference here.

Your 2 "cout" statements can be shortened to just this:
1
2
3
cout << "\nHet totale brutobedrag is: " << totaleBruto << " Euro\n";

cout << "De totale BTW is " << totaleBTW << " Euro";

Note the use of the "\n"s in line 1.

Also writing this cout << "\nGeef de prijs van artikel 1: "; with out any "\n" or "endl" will put the following "cin" on the same line. To me it just looks better that way.

With what changes I have made the output looks like this:
Geef de prijs van artikel 1: 10.25
Geef de code van artikel 1: -5

     Voer een artikelcode in tussen de 1 en de 6

Geef de code van artikel 1: 0

     Voer een artikelcode in tussen de 1 en de 6

Geef de code van artikel 1: 9

     Voer een artikelcode in tussen de 1 en de 6

Geef de code van artikel 1: 1

Het totale brutobedrag is: 30.75 Euro
De totale BTW is 4.62 Euro


I think should work for what you want.

Andy
Last edited on
@seeplus

Again thank you for your help and tips! that's the loop i wanted to use but didn't know how.

@jonnin

Thanks, your advice is very helpful!

@Handy Andy

I changed the code in my comment with the correct CODE TAGGS. My apologies that I didn't used it in the first place.

"First you are only checking for numbers greater than 6. What about (0) zero or numbers less than zero?
Then should the if statement be true you print out an error message and continue with line 21 keeping the invalid number. Not what you want."

I was trying to solve this problem u mentioned. So I am very gratefull u helped me with this solution!

Now im going to rewrite my program with all your tips and tricks!
If I got any more questions I will post here.
again thank you all

(ps: sorry for my poor English)
Last edited on
I got one more question,

do you guys know if it is possible to program with C++ on MacOS? without using bootcamp or a similar windows mirroring program.

James
yes, there are tools for Mac.
Ive lost track of the mac world, but I thought their 'new' os (os X and since) was unix with their goofy gui overlaid on it. so you should also be able to use linux / etc unix tools + whatever special snowflake IDES there are for mac, and I think there is also a way to run visual studio without a VM (not 100% sure on that last).

Check the web, here is an older post that popped up quickly with a half done search:
https://kig.re/2018/09/20/c++-newbie-tour-how-to-get-started-with-c++-on-mac-osx.html

** avoid eclipse. It is horrible for c++.
-- the page says that GCC is already on the system.
Last edited on
I would have recommended Code::Blocks as a free choice for MacOS, but the Mac 'flavor' hasn't been updated in almost seven years.
http://www.codeblocks.org/downloads/binaries#mac

Without spending a lot of time searching the internet (something you should do) it looks to be command-line compilers are the options available. Clang or GCC seems to be what pops-up the most.
https://duckduckgo.com/?q=c%2B%2B+compiler+for+macos&t=ffsb&ia=web
There is the XCode IDE:
https://developer.apple.com/xcode/ide/
Last edited on
Is XCode free? With my admittedly very quick 'net search I found more links that talked about it was paid/subscription-based.

Not that I have any need to do Mac development so any good advice is pure gravy to me. :)
I'm not sure, but I think XCode is free, while a developer license is required to publish the software you write on Apple's platforms.

https://developer.apple.com/support/compare-memberships/
Says USD $99/year at the bottom.
For macOS XCode is the way to go. It is completely free and unlimited. (AppleID is free to anybody whether or not they are an Apple customer.)

QtCreator is another one to consider later on. The Open Source version is free and unlimited too.
https://www.qt.io/product/development-tools

just one small note, but i think worth saying: you don't need to buy an Apple Developer account to build binary distributions(ie .app, .pkg etc), however if you don't buy an account, any app you publish will require the end user to click through various security settings under System Preferences to get your app to run the first time they open it... The $99/year is basically just to get a security certificate for code-signing; and also allows you to publish via the AppStore. It's perhaps a good idea if you are trying to enhance your visibility, although I'm not really sure it make computing any safer for the end user(and probably contributes to a false sense of security).
Last edited on
Topic archived. No new replies allowed.