Integer inproperly initialized

I am writing a Duck Hunt program. The problem in detail is that when I type the correct number, the score variable does not go up. I was wondering if the variable "duckL" was being initialized more than once and that "board1" was not being updated.

Board.h

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
#include <iostream>
#include <Windows.h>

int location = 0;
int score = 0;

class Board {
public:
	std::string board1 = "------------";

	void drawBoard() {
		std::cout << board1 << "\n";
		std::cout << "                     " << score << "\n";
		std::cout << "\n\n\n";
		Sleep(100);
			srand((int)time(0));
		int duckL = (rand() % 12) + 1;

		Sleep(500);

		if (duckL == 1) {
			location = 1;
			board1 = "O-----------";
		}
		else if (duckL == 2) {
			location = 2;
			board1 = "-O----------";
		}
		else if (duckL == 3) {
			location = 3;
			board1 = "--O---------";
		}
		else if (duckL == 4) {
			location = 4;
			board1 = "---O--------";
		}
		else if (duckL == 5) {
			location = 5;
			board1 = "----O-------";
		}
		else if (duckL == 6) {
			location = 6;
			board1 = "-----O------";
		}
		else if (duckL == 7) {
			location = 7;
			board1 = "------O-----";
		}
		else if (duckL == 8) {
			location = 8;
			board1 = "-------O----";
		}
		else if (duckL == 9) {
			location = 9;
			board1 = "--------O---";
		}
		else if (duckL == 10) {
			location = 10;
			board1 = "---------O--";
		}
		else if (duckL == 11) {
			location = 11;
			board1 = "----------O-";
		}
		else {
			location = 12;
			board1 = "-----------O";
		}
	}
};

DuckHunt.cpp:

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 "Board.h"

#include <iostream>
#include <ctime>
#include <Windows.h>

Board board;


int main() {
game:
	board.drawBoard();
	int xyz;
	std::cin >> xyz;

	if (xyz == location) {
		score += 1;
		Sleep(100);
		goto game;
	}
	else {
		Sleep(100);
		goto game;
	}
}


Unfortunately, I do not know where the problem occurs, except for maybe the if/else if/else chain in Board.h.
Last edited on
What do you mean? The score does go up:
------------
                     0

2
----O-------
                     0

2
-----O------
                     0

2
-------O----
                     0

2
-----O------
                     0

2
-O----------
                     1

2
-O----------
                     2

2
-------O----
                     2
Well, do you think it is a Visual-Studio glitch then?
Ooh... I think it might need to be the line beneath the input. E.G:

------------
                     0

2
----O-------
                     0

2
-----O------
                     0

2
-------O----
                     0

2
-----O------
                     0

2
BENEATH score goes up.
-O----------
                     1

2
BENEATH score goes up. (also above).
-O----------
                     2

2
-------O----
                     2


EDIT:
I tested it, and yes that is the case.
Last edited on
What can you say about this version:
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>

class Board {
public:
    std::string board1 = "------------";
    int location = 0;
    int score = 0;

	void drawBoard() {
		std::cout << board1 << "\n";
		std::cout << "                     " << score << "\n";
		std::cout << "\n";
		srand((int)time(0));
		int duckL = (rand() % 12) + 1;

		location = duckL;
		board1 = "------------";
		board1[location-1] = 'O';
	}
};

int main() {
  Board board;
  while (true) {
	board.drawBoard();
	int xyz;
	std::cin >> xyz;

	if (xyz == board.location) {
		board.score += 1;
	}
  }
}
Still the same, unfortunately...
I don't understand what the problem is.
You might want to move the srand call outside of the loop, but I don't think that's the issue you're talking about.
Hello HypeCoderPanda,

In Board.h in the function "drawBoard()" put the call to "srand" in "main" not here. This function only needs called once. Not everytime that you call the function. If you call "srand" to soon you could end up with the same number.

In Board.h in the function "drawBoard()" you define int duckL = (rand() % 12) + 1;. When you reach the closing } of the function this variable is destroyed and its value lost. Try making "duckL" a static variable. That way when the function ends the variable and the value will not.

In the function "main" use a while or do/while loop and get rid of the "goto"s. "goto" still has a rare use, but this is not 1 of them. the loop is much better.

"Sleep" is good, lol, but 100 milliseconds does not give much of a noticeable delay. You need something like 200 milliseconds or above.

keskiverto while loop example is a start, but there is no way out of it.

Andy
Geez, Handy Andy is pretty handy. That worked for me!
Hello HypeCoderPanda,

Sorry about that. I have no idea how I got fixed on "duckL" being the problem. The first 2 paragraphs I wrote have nothing to do with the problem.

After running the code a few times I realized that "int location" and "int score;" are defined as global variables in the header file and so become global variables in the ".cpp" file. So adding to the "score" does not become a problem.

When I ran the code in MSVS 2017 the program appeared to do what it should, the best I could tell.

I did have a problem with trying to figure out what needed to be entered and when. Your output to the screen could use a little work.

I was wondering if the class is required because I do not see much use for it right now. I could say that you could just as easily use a "struct", but that is still more than you need.

The variables that you need are all global outside the class and the function does not need a class to work. It just seems like extra work that is not needed.

If you got the program to work for you that is good.

Andy
Topic archived. No new replies allowed.