Shields up!

I'm very new to programming and i'm trying to get the hang of functions. In order to do this i thought up a little game i'm trying to build. I want the status of the players ship to display often and give a few status; easy enough. but i also want to say if shields are up or down. I make some code to do this, but my problem is getting it to update on the frequent status updates. I figured a function is the best way to go about it, so i put my "shield check" code in a function, but at this point i can tell i'm missing something fundamental.

Could someone take a look at my code and help me figure this out? Thanks in advance.

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
  #include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

string UpShields = "Err";
string cName = "none";
int Shields = 3;							//if Shields are >=3 shields are up.
int Hull = 100;
int Power = 100;
int ShieldsUp = 0;


int main()
{
	
	void ShieldTest ();
		{
		ShieldsUp = (Shields > 2);			//if Shields are >=3 shields are up.
	
		while (ShieldsUp == 1)
		{
			UpShields = "UP";
			break;
		}
		while (ShieldsUp == 0)				//if Shields are <3 shields are down
		{
			UpShields = "Down";
			break;
		}
		
	}
	
	
	
	srand(static_cast<unsigned int>(time(0))); //seed random number generator
	int randomNumber = rand(); //generate random number
	int die = (randomNumber % 6) + 1; // get a number between 1 and 6
	
	cout << "Please name your Captian. :";
	cin >> cName;
	
	
	
	cout << "Captian " << cName << ", You have been called here. ((insert story here)) \n \n \n \n";
	cout << "Shields:\t" << UpShields << "\n   Hull:\t" << Hull << "\n  Power:\t" << Power << "\n \n";   //This line is used for ship status. copy and paste this.
		
	die = 1; //remove this!!!!!!
		if (die == 1)
		{
			cout << "A Pirate suddenly apperas and fires an EMP before you can reacte! \n" << "Shields are offline. \n";
			Shields = 0;
			int ShieldTest();
			cout <<"Your Ship. \t\t\t Enemy Ship.\n" << "Shields:\t" << UpShields << "\n   Hull:\t" << Hull << "\n  Power:\t" << Power << "\n \n";
			
			
			
		}
	

	
	return 0;
}
Last edited on
specifically after my mock dice roll (forced a 1) it should display a message, then shange the value of Shields to 0. Than when i displays the status again, i want it to show the shields as "down" but it never seams to run the ShieldTest function again, so the shields always say they are up.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void ShieldTest ();
		{
		ShieldsUp = (Shields > 2);			//if Shields are >=3 shields are up.
	
		while (ShieldsUp == 1)
		{
			UpShields = "UP";
			break;
		}
		while (ShieldsUp == 0)				//if Shields are <3 shields are down
		{
			UpShields = "Down";
			break;
		}
		
	}

Actually compiles, but doesn't do what you want :)

The first line has a trailing semicolon, so it says that a function named ShieldTest exists, and that it accepts no arguments.

The rest of the lines introduce a new local scope, not bound to any new function, which gets run first thing in main.

Take that entire snippet of code out of main. Functions cannot be defined inside other functions, although they can be declared locally (to, e.g., change default arguments in the lexical scope). Then, remove the semicolon from the end of the first line.

Also:
int ShieldTest();
Can't have int in front of it for similar reasons. Function calls don't need a return type in front of them; the compiler already knows (and that changes the meaning).

Last edited on
ok i did that, but now i just get err for shields up (glad i put that there ;)) i think the problem is that the ShieldTest isn't returning anything, but that definitely progress.
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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

string UpShields = "Err";
string cName = "none";
int Shields = 3;							//if Shields are >=3 shields are up.
int Hull = 100;
int Power = 100;
int ShieldsUp = 0;


void ShieldTest ()
		{
		ShieldsUp = (Shields > 2);			//if Shields are >=3 shields are up.
	
		while (ShieldsUp == 1)
		{
			UpShields = "UP";
			break;
		}
		while (ShieldsUp == 0)				//if Shields are <3 shields are down
		{
			UpShields = "Down";
			break;
		}
		
	}

int main()
{
	

	srand(static_cast<unsigned int>(time(0))); //seed random number generator
	int randomNumber = rand(); //generate random number
	int die = (randomNumber % 6) + 1; // get a number between 1 and 6
	
	cout << "Please name your Captian. :";
	cin >> cName;
	
	
	int ShieldTest();
	cout << "Captian " << cName << ", You have been called here. ((insert story here)) \n \n \n \n";
	cout << "Shields:\t" << UpShields << "\n   Hull:\t" << Hull << "\n  Power:\t" << Power << "\n \n";   //This line is used for ship status. copy and paste this.
		
	die = 1; //remove this!!!!!!
		if (die == 1)
		{
			cout << "A Pirate suddenly apperas and fires an EMP before you can reacte! \n" << "Shields are offline. \n";
			Shields = 0;
			int ShieldTest();
			cout <<"Your Ship. \t\t\t Enemy Ship.\n" << "Shields:\t" << UpShields << "\n   Hull:\t" << Hull << "\n  Power:\t" << Power << "\n \n";
			
			
			
		}
	

	
	return 0;
}
No, ShieldTest() doesn't return anything, but that's not the problem; the function has side effects (it changes a global variable).

Line 44, line 53:
Remove the leading int from the invocations of ShieldTest().
If there's a type name in front of what you intend to be a function call, it's not a function call anymore.

Note:
The definition of ShieldTest() can be simplified to
1
2
3
4
if (Shields > 2) 
  UpShields = "Up";
else 
  UpShields = "Down";

Or even further to
UpShields = Shields > 2? "Up": "Down";
Last edited on
mbozzi your the best!

"Line 44, line 53:
Remove the leading int from the invocations of ShieldTest().
If there's a type name in front of what you intend to be a function call, it's not a function call anymore. "

is the part that made all the difference. Thank you so much for the help!.
Topic archived. No new replies allowed.