questions of a game's code.

There's three erro in my code which i don't understand after i search for these error:
error C2679;error C2679;error C2064;

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include<iostream>
#include<ctime>
#include<cstdlib>
int main(void)
{
	using std::cout;
	using std::cin;
	using std::srand;
	using std::time;
	using std::rand;
	using std::string;
	

	srand((unsigned int)time(0));
	

	bool end=false;
	bool lost;
	string name;
	

	int archers=50;
	int catapults=25;
	int swordmen=100;

	int g_archers=rand()%(51)+20;
	int g_catapults=rand()%(41)+10;
	int g_swordmen=rand()%(101)+50;

	cout<<"Welcome!\nWhat's your name?\n";
	cin>>name;
	
	cout<<"Well,welcome "<<name<<"\nThis is the Roman Commander Game!";

	int	archers_sent=0;
	int catapults_sent=0;
	int swordmen_sent=0;
	int menu_choice;

	while(!end);
	{
		cout<<"You have"<<archers<<"archers,"<<catapults<<"catapults,"<<swordmen<<"swordmen\n";
		cout<<"Gemania have"<<g_archers<<"archers,"<<g_catapults<<"catapults,"<<g_swordmen<<"swordmen\n";

		int fight_menu;
		do
		{
			int i=1;
			int archers_menu;
			int catapults_menu;
			int swordmen_menu;

			if (archers>0 && (archers-archers_sent)!=0)
			{
				archers_menu=i;
				cout<<"["<<i<<"] send archers\n";
				i++;
			}
			else archers_menu=0;
			if (catapults>0 && (catapults-catapults_sent)!=0)
			{
				catapults_menu=i;
				cout<<"["<<catapults_menu<<"] send catapults\n";
				i++;
			}
			else catapults_menu=0;
			if (swordmen>0 && (swordmen-swordmen_sent)!=0)
			{
				swordmen_menu=i;
				cout<<"["<<swordmen_menu<<"] send swordmen\n";
				i++;
			}
			else swordmen_menu=0;
			fight_menu=i;
			cout<<"["<<fight_menu<<"] Go fight!\n";
			cin>>menu_choice;
			
			if (menu_choice==archers_menu)
			{
				do{
					cout<<"\nHow many archers do you want to send?\n";
					cin>>archers_sent;
				}while (!(archers_sent>-1 && archers_sent<=archers));
			}
			else if (menu_choice==catapults_menu)
			{
				do{
					cout<<"\nHow many catapults do you want to send?\n";
					cin>>catapults_sent;
				}while (!(catapults_sent>-1 && catapults_sent<=catapults));
			}
			else if (menu_choice==swordmen_menu)
			{
				do{
					cout<<"How many swordmen do you want to send?\n";
					cin>>swordmen_sent;
				}while(!(swordmen_sent>-1 && swordmen_sent<=swordmen));
			}
		}while(menu_choice!=fight_menu);
			cout<<"\nEntering Battle!\n";

		int archers_dead,catapults_dead,swordmen_dead;
		int g_archers_dead,g_catapults_dead,g_swordmen_dead;

		archers_dead=2*g_catapults;
		catapults_dead=g_swordmen;
		swordmen_dead=3*g_archers;

		g_archers_dead=2*catapults_sent;
		g_catapults_dead=swordmen_sent;
		g_swordmen_dead=3*archers_sent;

		archers=(archers>archers_dead)?archers-archers_dead:0;
		catapults=(catapults>catapults_dead)?catapults-catapults_dead:0;
		swordmen=(swordmen>swordmen_dead)?swordmen-swordmen_dead:0;

		g_archers(g_archers>g_archers_dead)?g_archers-g_archers_dead:0;
		g_catapults=(g_catapults>g_catapults_dead)?g_catapults-g_catapults_dead:0;
		g_swordmen=(g_swordmen>g_swordmen_dead)?g_swordmen-g_swordmen_dead:0;

		cout<<"It was a long journey"
			<<archers_dead<<"archers died,"
			<<catapults_dead<<"catapults died,"
			<<swordmen_dead<<"swordmen died.\n";

			if((archers+catapults+swordmen)==0)
			{
				end=lost =true;
				
			}
			else if((g_archers+g_catapults+g_swordmen)==0)
			{
				end=true;	
				lost=false;
				
			}
			if(lost)
			{
				cout<<"You lost!\n";
				return 0;
			}
				cout<<"You win!!\n";
				return 0;
	}
	}


Output:
Welcome Adventurer, what is your name?
MarcusWell, Marcus welcome to the Roman Commander Game.
 
You are the commander of the Roman Army attacking German
You have 50 archers, 25 catapults, and 100 swordsmen.
 
Germania has 61 archers, 50 catapults, and 52 swordsmen.
 
[1] Send Archers
[2] Send Catapults
[3] Send Swordsmen
[4] Go Fight
1
How many archers would you like to send?
50 
[1] Send Catapults
[2] Send Swordsmen
[3] Go Fight
1How many catapults would you like to send?
25
[1] Send Swordsmen
[2] Go Fight
1
How many swordsmen would you like to send?
100
[1] Go Fight
1
Entering Battle...
It was a long battle.
100 archers died.
52 catapults died.
183 swordsmen died.
 
You lost. Try again next time.



The output may not look like the exactly the same as my code,but since i only change some sentences,so it doesn't matter..
Thank you!!
Line 40 should be:
while(!end)


Line 117 should be:

g_archers=(g_archers>g_archers_dead)?g_archers-g_archers_dead:0;
Last edited on
g_archers(g_archers>g_archers_dead)?g_archers-g_archers_dead:0;

This is good C++ programming? I took 3 classes on C++ and I never learned about otreven saw this syntax until I started to read up on C#.

I understand what is going on (if else) but is this from old C or something? Why are you using it? It doesn't look like any C++ I;ve been using.
With Moschops' correction (the =) it is.

g_archers=(g_archers>g_archers_dead)?g_archers-g_archers_dead:0;

Yes, it is inherited from C. But I would use the ?: form in this case rather than the equivalent if-else from:

1
2
3
4
if(g_archers>g_archers_dead)
    g_archers=g_archers-g_archers_dead;
else
    g_archers=0;


Andy

PS I assume the g_s are there because the variables were once globals? Now there local variable, the g_s should go!

EDIT I see g_ is for Gemania. It just so happens that g_ is commonly used to mark global variables.
Last edited on
I prefer
1
2
g_archers -= g_archers_dead;
if( g_archers<0 ) g_archers = 0;
What about

g_archers -= std::min(g_archers, g_archers_dead);
Last edited on
Well ?: is the only ternary operator of c/c++ but it's not equivalent to if/else in all cases.

The ?: is an expression and if-then-else is a statement so they can always be used in the same place.

Example in initialization list of base classes only the ternary operator can be used. Generally in conditional assignment situations only the ternary operator can be used.
This line (128):
end=lost =true;

Should be?:
1
2
end = true;
lost = true;

end=lost =true; is valid C/C++ syntax

But I would code it as you suggested, one per line, as I find it easier to follow.
Topic archived. No new replies allowed.