Redefinition and uninitialized memory failures

So right after seeplus helped me with my previous problem (which was pretty lame on my side) another problem occured.

It has to be something i completely miss and i will probably feel stupid at the end (again) but im really stuck here.

Underneath is my code, but it keeps saying redefinition and using unitialized memory.

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

#include <iostream>
#include <math.h>
#include <string>

using namespace std;

int main()
{
	int ingevoerdbedrag;
		cout << "Voer een bedrag in tussen 0 en 100" << endl;
		cin >> ingevoerdbedrag; 
		if (ingevoerdbedrag < 0)
			cout << "Vul een positief getal in, groter dan 0" << endl; 
		if (ingevoerdbedrag > 0)
			cout << "U krijgt:" << endl;
		if (ingevoerdbedrag = 0)
			cout << "Helaas, dit bedrag kan niet met de aanwezige munten/biljetten, probeer een ander getal" << endl;


		int aantal50, restbedrag50, aantal20, restbedrag20, aantal10, restbedrag10, aantal5, restbedrag5, aantal2, restbedrag2, aantal1;

		if (ingevoerdbedrag >= 50 )
		{
			int aantal50 = (ingevoerdbedrag / 50);
			if (aantal50 > 0)
				cout << aantal50 << " briefje(s) van 50" << endl; 
		}

		int restbedrag50 = (ingevoerdbedrag - (aantal50 * 50));

		if (restbedrag50 >= 20)
		{
			int aantal20 = (restbedrag50 / 20);
			if (aantal20 > 0)
				cout << aantal20 << "briefje(s) van 50" << endl;
		}

		int restbedrag20 = (restbedrag50 - (aantal20 * 20));

		if (restbedrag20 >= 10)
		{
			int aantal10 = (restbedrag20 / 10);
			if (aantal10 > 0)
				cout << aantal10 << "briefje(s) van 10" << endl; 
		}

		int restbedrag10 = (restbedrag20 - (aantal10 * 10));

		if (restbedrag10 > 0)
		{
			int aantal5 = (restbedrag10 / 5);
			if (aantal5 > 0)
				cout << aantal5 << "briefje(s) van 5" << endl;
		}
		 
		int restbedrag5 = (restbedrag10 - (aantal5 * 5));
		
		if (restbedrag5 > 0)
		{
			int aantal2 = restbedrag5 / 2;
			if (aantal5 > 0)
				cout << aantal5 << "munt(en) van 2" << endl;
		}

		int restbedrag2 = (restbedrag5 - (aantal2 * 2));

		if (restbedrag2 > 0)
		{
			int aantal1 = restbedrag2 / 1;
				if (aantal1 > 0)
					cout << aantal1 << "munt(en) van 1" << endl; 
		}


}


Again:

Thanks for your time and for all the help / tips / tricks in advance!
Line 17, if (ingevoerdbedrag = 0) should be if (ingevoerdbedrag == 0).

You are assigning 0 to your variable, not comparing the value.
true, that was one mistake. but it still says redefinition for the others.

by others i mean:
"restbedrag50"
"restbedrag20"
"restbedrag10"
"restbedrag5"
"restbedrag2"

it supposed to show how many bills the given number is.
for example 80 euros as cin :
has to show:
1 bill of 50, 1 bill of 20 and one bill of 10.
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.
Last edited on
1
2
3
4
5
6
7
8
9
10
	int aantal50, restbedrag50, aantal20, restbedrag20, aantal10, restbedrag10, aantal5, restbedrag5, aantal2, restbedrag2, aantal1;

		if (ingevoerdbedrag >= 50 )
		{
			int aantal50 = (ingevoerdbedrag / 50);
			if (aantal50 > 0)
				cout << aantal50 << " briefje(s) van 50" << endl; 
		}

		int restbedrag50 = (ingevoerdbedrag - (aantal50 * 50));


you should remove the int on the second one. similar for others.
Last edited on
Thank you guys so much!

decided to try your way furry guy.
These things makes more sense now!

I like to define variables as close to first use as possible, instead of near to the top of the block as is done in C.

Taking some time to move some variable definitions around, and rewriting some "old school initializations" to "uniform initialization" style I'd come 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
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
#include <iostream>

int main()
{
   std::cout << "Voer een bedrag in tussen 0 en 100\n";
   int ingevoerdbedrag { };
   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 { };

   if (ingevoerdbedrag >= 50)
   {
      aantal50 = (ingevoerdbedrag / 50);

      if (aantal50 > 0)
      {
         std::cout << aantal50 << " briefje(s) van 50\n";
      }
   }

   int restbedrag50 { (ingevoerdbedrag - (aantal50 * 50)) };
   int aantal20     { };

   if (restbedrag50 >= 20)
   {
      aantal20 = (restbedrag50 / 20);

      if (aantal20 > 0)
      {
         std::cout << aantal20 << "briefje(s) van 50\n";
      }
   }

   int restbedrag20 { (restbedrag50 - (aantal20 * 20)) };
   int aantal10     { };

   if (restbedrag20 >= 10)
   {
      aantal10 = (restbedrag20 / 10);

      if (aantal10 > 0)
      {
         std::cout << aantal10 << "briefje(s) van 10\n";
      }
   }

   int restbedrag10 { (restbedrag20 - (aantal10 * 10)) };
   int aantal5      { };

   if (restbedrag10 > 0)
   {
      aantal5 = (restbedrag10 / 5);

      if (aantal5 > 0)
      {
         std::cout << aantal5 << "briefje(s) van 5\n";
      }
   }

   int restbedrag5 { (restbedrag10 - (aantal5 * 5)) };
   int aantal2     { };

   if (restbedrag5 > 0)
   {
      aantal2 = restbedrag5 / 2;

      if (aantal5 > 0)
      {
         std::cout << aantal5 << "munt(en) van 2\n";
      }
   }

   int restbedrag2 { (restbedrag5 - (aantal2 * 2)) };
   int aantal1     { };

   if (restbedrag2 > 0)
   {
      aantal1 = restbedrag2 / 1;

      if (aantal1 > 0)
      {
         std::cout << aantal1 << "munt(en) van 1\n";
      }
   }
}


About "uniform initialization:"
https://mbevin.wordpress.com/2012/11/16/uniform-initialization/

One nice (yet annoying feature to newbies) is uniform initialization makes this int x { 3.8 }; throw an error ("Error C2397 conversion from 'double' to 'int' requires a narrowing conversion").

int x = 3.8; does a conversion of 3.8 (double) to 3. With uniform initialization the compiler flags what could be a programmer error, truncating a double to an int.

BTW, line 93, dividing by 1, really isn't needed. Any number divided by 1 will always be the original number. That last if block could be rewritten as:
88
89
90
91
92
93
   int restbedrag2 { (restbedrag5 - (aantal2 * 2)) };

   if (restbedrag2 > 0)
   {
      std::cout << restbedrag2 << "munt(en) van 1\n";
   }
Couple of further points

- if a problem is detected which isn't allowed - such as ingevoerdbedrag < 0, then as well as displaying a message, the program would either terminate or, better, re-prompt the user for the data. It wouldn't just continue with the invalid value.

- if a variable is initialised with a value and that value doesn't change, then its good practice to specify the variable as const - meaning that if an attempt is made to change its content later then the compiler will show an error. This helps with 'accidentally' changing the value of a variable when it shouldn't be changed. Consider:

 
const int restbedrag50 {(ingevoerdbedrag - (aantal50 * 50))};


etc
Topic archived. No new replies allowed.