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