You define multiple variables at line 21 without assigning them any value(s).
Multiple variables at line 21 you later define at the same scope. restbedrag50, for instance. line 21, also defined at line 30.
Block scope issues are also a source of problems. You are defining a lot of variables on line 21 at one scope. aantal150, for example. Defined on line 21, and then defined again in the if block starting at line 23. the aantal150 inside the if block is NOT the same variable you defined on line 21.
I personally define one variable per line, using uniform initialization.
int x { };
Can also be
int x = 0;
I'd rewrite (and reformat) your code to:
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
|
#include <iostream>
int main()
{
int ingevoerdbedrag;
std::cout << "Voer een bedrag in tussen 0 en 100\n";
std::cin >> ingevoerdbedrag;
if (ingevoerdbedrag < 0)
{
std::cout << "Vul een positief getal in, groter dan 0\n";
}
if (ingevoerdbedrag > 0)
{
std::cout << "U krijgt:\n";
}
if (ingevoerdbedrag == 0)
{
std::cout << "Helaas, dit bedrag kan niet met de aanwezige munten/biljetten, probeer een ander getal\n";
}
int aantal50 { };
int aantal20 { };
int aantal10 { };
int aantal5{ };
int aantal2 { };
int aantal1 { };
if (ingevoerdbedrag >= 50)
{
aantal50 = (ingevoerdbedrag / 50);
if (aantal50 > 0)
{
std::cout << aantal50 << " briefje(s) van 50\n";
}
}
int restbedrag50 = (ingevoerdbedrag - (aantal50 * 50));
if (restbedrag50 >= 20)
{
aantal20 = (restbedrag50 / 20);
if (aantal20 > 0)
{
std::cout << aantal20 << "briefje(s) van 50\n";
}
}
int restbedrag20 = (restbedrag50 - (aantal20 * 20));
if (restbedrag20 >= 10)
{
aantal10 = (restbedrag20 / 10);
if (aantal10 > 0)
{
std::cout << aantal10 << "briefje(s) van 10\n";
}
}
int restbedrag10 = (restbedrag20 - (aantal10 * 10));
if (restbedrag10 > 0)
{
aantal5 = (restbedrag10 / 5);
if (aantal5 > 0)
{
std::cout << aantal5 << "briefje(s) van 5\n";
}
}
int restbedrag5 = (restbedrag10 - (aantal5 * 5));
if (restbedrag5 > 0)
{
aantal2 = restbedrag5 / 2;
if (aantal5 > 0)
{
std::cout << aantal5 << "munt(en) van 2\n";
}
}
int restbedrag2 = (restbedrag5 - (aantal2 * 2));
if (restbedrag2 > 0)
{
aantal1 = restbedrag2 / 1;
if (aantal1 > 0)
{
std::cout << aantal1 << "munt(en) van 1\n";
}
}
}
|
Whether the program logic is correct, I can't say. I don't speak Dutch.
I only include headers I know are needed. <cmath>/<math.h> and <string> are not needed.