2 errors, no sense at all....

Hey guys, I am trying to make something like a tactical simulator, just a game, to pass my free time and practice my C++... I am a starter, but I actualy managed to make a couple of my personal projects work perfectly.
Getting to the point, I have 2 ships moving on a grid, and I wanted to make a function that would calculate their distance, so I would later limit the ships from firing out of their range. The code initialy worked, but I would get distance of 6.74 million when their coordinates where (2,2) and (9,9). after adding the comments, in order to post my code here, the compiler (default dev-c++ 4.9.9.2) stopped recognising my structure "Ship", so I can't compile any more... (ok, fixed)
What the program should do so far is:
1) show the coordinates of each ship
2) switch between players
3) command the ships to move by 1 square per turn
4) calculate the distance between them
here is my code
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227

#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

bool player = 0; // current player
int choice = 1; // player input 1 -- main menu
int c2 = 1; // selection 2 -- no idea where i use it
int x[10]; // grid x axis for future use
int y[10]; // grid y axis likewise
int theDistance; // distance between 2 selected ships


struct Ship // general characteristics of ship units
{
      bool owner; // owning player
      string name; // ship name, future use
      int firepower; // attack value
      int HP; // hit points
      int visibility; // distance in which enemies are visible, future use
      int range; // obviously the range
      int xp; // x position coordinate
      int yp; // y position coordinate
      int speed; // max movement per round
};

Ship BB1; // declaring a couple of ships
Ship BB2; // declaring a couple of ships

void DeclaringShips() // puting the unit stats to the ships
{
BB1.owner=0;
BB1.firepower = 50;
BB1.HP = 300;
BB1.name = "BB1";
BB1.visibility = 4;
BB1.range = 3;
BB1.xp = 2;
BB1.yp = 2;
BB1.speed = 1;

BB2.owner=1;
BB2.firepower = 50;
BB2.HP = 300;
BB2.name = "BB1";
BB2.visibility = 4;
BB2.range = 3;
BB2.xp = 9;
BB2.yp = 9;
BB2.speed = 1;
}

void changePlayer() // players switch turns
{
     if(player == 0) // if it's player 0's turn..
     {
     player = 1; // current player is now 1
     }
     else // if not (so player is 1)
     {
     player = 0; // current player is 0
     }
}

void display() // view position of each ship
{
     cout << "BB1's position: " << BB1.xp << "," << BB1.yp << endl;
     cout << "BB2's position: " << BB2.xp << "," << BB2.yp << endl;
     return;
 }
 
int calculatedistance(int x1, int y1, int x2, int y2) // actual distance calculation
{
    double distSqr; // create a double for the square of the distance
    double x12 = (double)x1; // creating double version of int coordinates
    double x22 = (double)x2; 
    double y12 = (double)y1;
    double y22 = (double)y2;
    double difX; // diference between x coordinates
    double difY; // ----------------- y -----------
    difX = fabs(x12 - x22); // difX is equal to absolute x1 - x2
    difY = fabs(y12 - y22); // likewise for y
    distSqr = difX * difX + difY * difY; // the square of the distance
    theDistance = (int)sqrt(distSqr); // actual distance is square root of distSqr... and is like 6.74milion..
}

void finddistance() // the 'menu' for choosing which shpis' distance we want
{
     int code1; // ship 1
     int code2; // ship 2
     cout << "insert codes of ships" << endl;
     cin >> code1;
     cin >> code2;
     int X1,X2,Y1,Y2; // these actualy work as pointers, figured this was simpler..
     switch (code1)
     {
     case 1:
     {
           X1 = BB1.xp; 
           Y1 = BB1.yp;
           break;
     }
     case 2:
     {
           X1 = BB2.xp;
           Y1 = BB2.yp;
           break;
     }
     }
     switch (code2)
     {
     case 1:
     {
           X1 = BB1.xp;
           Y1 = BB1.yp;
           break;
     }
     case 2:
     {
           X1 = BB2.xp;
           Y1 = BB2.yp;
           break;
     } // yes, the player might insert the same ship twice, but i trust he is not so.. ignorant
     }
     calculatedistance(X1,Y1,X2,Y2); // proceed with previous function
     cout << "distance is " << theDistance << endl; // output the result
}

void movements()
{
     int code;
     cout << "enter code : for BB1:1 for BB2:2" << endl;
     cin >> code;
     if (code == 1)
     {
              if (BB1.owner != player)
              return;
              else
              {
                  cout << "for north movement type 1, east 2, south 3, west 4" << endl;
                  int dir;
                  cin >> dir;
                  if (dir == 1)
                  {
                          BB1.yp++;
                  }
                  if (dir == 2)
                  {
                          BB1.xp++;
                  }
                  if (dir == 3)
                  {
                          BB1.yp--;
                  }
                  if (dir == 4)
                  {
                          BB1.xp--;
                  }
              }
     }
     if (code == 2)
     {
              if (BB2.owner != player)
              return;
              else
              {
                  cout << "for north movement type 1, east 2, south 3, west 4" << endl;
                  int dir;
                  cin >> dir;
                  if (dir == 1)
                  {
                          BB2.yp++;
                  }
                  if (dir == 2)
                  {
                          BB2.xp++;
                  }
                  if (dir == 3)
                  {
                          BB2.yp--;
                  }
                  if (dir == 4)
                  {
                          BB2.xp--;
                  }
              }
     }
     return;
 }
 
void mainmenu()
{
          cout << "It is player " << player << "'s turn" << endl;
          cout << "do you want an overview of the combat area (1) to give your orders (2) or view distance between 2 ships? (3)" << endl;
          cin >> choice;
          switch(choice)
          {
                        case 1:
                             display();
                             break;
                        case 2:
                             for(;c2 != 0;)
                             {
                                     movements();
                                     break;
                             }
                        case 3:
                             finddistance();
          }
}
          

int main(int argc, char *argv[])
{
    DeclaringShips();
    cout << "welcome admiral, your fleet awaits you" << endl;
    for(; choice != 0;)
    {
    mainmenu();
    changePlayer();
    }
    system("title WWIINavalSim");
    system("PAUSE");
    return EXIT_SUCCESS;
}

I think it is well written, I avoided stuff I know I can't handle and commented the part where the trouble is made.
ignore the movement function, it works. If you copy the code please use dev-c++ so we know we are working on the same compiler.
Thanks for any help, I will keep trying to fix the issue with the struct, I won't touch the distance finding functions, since I now rely on your help.
Sorry for any troublesome piece of code, I will answer questions, thanks in advance.
//compiling error was fixed, please check the logical error
Last edited on
closed account (zb0S216C)
What are the errors you speak of?
I'll admit I didn't read the post and hardly looked at the code, but around line 204 is very, very unusual. That for loop always breaks and then that switch clause falls through to the next case.
ok, error 1: compiler says [line 28] "'Ship' does not name a type"
[line 33, 68, 100, 137] 'BB1' undeclared (first use this function)

before adding comments this didn't happen, I can't see why it's happening now...

error 2: assuming you can get this to work like before error 1 occured, when it would calculate the distance, BB1 being at (2,2) and BB2 at (9,9), the result would be 6.74something million! that became 6.75 milion when I moved BB2 at (9,8)... should have decreased, right?

the for loop does indeed break, thanks for noticing, I guess I do want to change that, but It will cause some problems at this stage, probably just remove the loop... anyway, that does not cause any annoying logical error, so don't bother with it...
thanks in advance for any help....
Last edited on
You're missing a semicolon after the closing brace for struct Ship.
oh, thanks, sorry about that.. ehm.... any idea about the 6.74 million distance? feel free to copy my code, this problem insists..

thanks Zhuge, I'll edit the post fixing the faulty code..
ok, I went through the code again, error 2 is solved, I had put the same values for the second ship, or rather it would just use it's stats twice, my fault, sorry for the trouble..
Topic archived. No new replies allowed.