Someone tell me what I'm doing wrong here?

Alright so I can get this working fine if i take out the code from SetNumberOfEnemies() and put it inside StartUp(). But now that it's in its own function it'll compile but it wont tell me how many enemies I've created etc.

In the if statement that line of code will never run. After the user inputs how many enemies he wants the game just finishes and doesn't display the output.

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
void StartUp()
{
	bool isGameAlive = true;

	int *numberOfEnemies = new int;

	SetNumberOfEnemies(numberOfEnemies);

	do
	{
		
	}while(isGameAlive == true);
}

void SetNumberOfEnemies(int *enemies)
{
	while(*enemies < 1 || *enemies > 20)
	{
		printf("Enter the amount of enemies you would like to fight between 1 and 20:\n\n  >> ");
		scanf_s("%d", *enemies);

		if((*enemies > 0 || *enemies < 21))
		{
			printf("\nThe Number of Enemies You Will Fight Is : %d\n", *enemies);
		}
	}
}
You haven't set a default value for numberOfEnemies when you allocated the space. Also, you haven't de-allocated the memory.

Why not use a local int and pass it by reference?
Ah well - basically as you know when its for study it does have requirements. I've only wrote this bit of code quick in my actual bigger version I have de-allocated it. I did have the int set to a a value but that wasn't working before. I'll try it again now though

Thanks Zaita

EDIT:

Changed the specifications to how you said. Still isn't working. Preferably wouldn't do it like this but yeah. Damn requirements but at the same time I'm learning more.

Cheers :)
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
void StartUp()
{
	DisplayMap();
	bool isGameAlive = true;

	int *numberOfEnemies = new int;
	*numberOfEnemies = 0;

	SetNumberOfEnemies(numberOfEnemies);

	do
	{
		
	}while(isGameAlive == true);

	delete numberOfEnemies;
}

void SetNumberOfEnemies(int *enemies)
{
	while(*enemies < 1 || *enemies > 20)
	{
		printf("Enter the amount of enemies you would like to fight between 1 and 20:\n\n  >> ");
		scanf_s("%d", *enemies);

		if((*enemies > 0 || *enemies < 21))
		{
			printf("\nThe Number of Enemies You Will Fight Is : %d\n", *enemies);
		}
	}
}
Last edited on
I don't use VC++ and scanf_s is non-portable. So I had to use scanf();

1
2
3
    printf("Enter the amount of enemies you would like to fight between 1 and 20:\n\n  >> ");
    fflush (stdout);
    scanf("%d", enemies);
Cheers mate, I had actually been using scanf() but I just thought scanf_s was just an updated version. Well my mistake. Thanks heaps Zaita :)

EDIT: One quick question if you read this again. How is fflush helping me here?

Thanks
Last edited on
fflush is flushing the output buffer. So it displays what printf() has in the stdout buffer to screen, then it goes to scanf();

Previously, it'd printf() but not display and continue on.
Ah yes, I see what you mean. Thanks a heap for that help Zaita.

Thanks,
Myth.
Topic archived. No new replies allowed.