Program is giving wrong values!!!

Ok, so I'm making a battleship game and I've only gotten to the point where the user enters his or her coordinates for the ships. It would take forever to explain what I have so I'll past the commented code. The problem is that when I set playerGrid[int(x)][y] = 1 // Means that there is a ship on that coordinate
I get a number thats like a huge negative or positive number. Here's the 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
// BattleShip.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

using namespace std;

void setGrids();

int main()
{
	setGrids();
}

void setGrids()
{
	string entry;
	int ships = 0;
	int playerGrid[8][8] = {0};
	int computerGrid[8][8] = {0};

	//Setting up Computer's ships - Max 7
	while (ships < 8)
	{
		int value1 = rand() % 8+1; //Sets a ship at a random x and y position from 1-7
		int value2 = rand() % 8+1;
		computerGrid[value1][value2] = 1;
			ships++;
	}

	//Sets Player's ships - Max 7
	for (int n = 1; n < 8; n++)
	{
retry:
		cout << "Coordinates of Ship " << n << " from A-H and 1-7 (i.e. E4): ";
		cin >> entry; //User entry, i.e. E4
		char x = entry.at(0); //Letter part of entry, i.e. E
		int y = entry.at(1); //Number part of entry, i.e. 4 in ASCII becomes 52
		y = y - 48; //Converts the ASCII integer to regular integer, i.e. 52-48=4
		
		//Checks to see if values were already used
		if (playerGrid[int(x)][y] == 1) //int(x) makes sure it's not a symbol, because x is a char originally
			goto retry;
		cout << playerGrid[int(x)][y]; // These 3 lines are for debugging purposes, you can see this value is huge...
		cout << x <<"\n"; 
		cout << y <<"\n";
		switch (x) //Converts letter value to equivalent number, i.e. A or a = 1
		{
		case ('A'):
			x = 1;
			break;
		case ('a'):
			x = 1;
			break;
		case ('B'):
			x = 2;
			break;
		case ('b'):
			x = 2;
			break;
		case ('C'):
			x = 3;
			break;
		case ('c'):
			x = 3;
			break;
		case ('D'):
			x = 4;
			break;
		case ('d'):
			x = 4;
			break;
		case ('E'):
			x = 5;
			break;
		case ('e'):
			x = 5;
			break;
		case ('F'):
			x = 6;
			break;
		case ('f'):
			x = 6;
			break;
		case ('G'):
			x = 7;
			break;
		case ('g'):
			x = 7;
			break;
		case ('H'):
			x = 8;
			break;
		case ('h'):
			x = 8;
			break;
		default:
			cout << "Incorrect position. Retype using format given.\n"; //If user does not type a letter from A-H, ask again
			goto retry;
			break;
		}
		if (y > 7 || y < 1) //If user does not give a number from 1-7, ask again
		{
			cout << "Incorrect position. Retype using format given.\n"; 
			goto retry;
		}
		playerGrid[int(x)][y] = 1; //Set the chosen coordinates to 1, meaning a ship is there. int(x) makes sure it isn't a symbol
	}
}


I can explain stuff if you want. Thanks.
Thanks, but just for future reference and curiosity, can you tell me why the program is giving me these weird values? I've looked at the link and it provides a different method but doesn't tell me what's wrong.
Sorry, I didn't actually read the code very carefully because I had just responded to the other thread.

There are two problems:

1) int('7') doesn't transform the letter '7' into the number 7, it just gives you whatever the code for the letter 7 is (in ASCII it is the number 55). I addressed that on lines 26 and 27 of the example I gave you.

2) The indices into an array are always 0 to N-1, where N is the number of items in the array. You are treating it as 1 to N, and also the int(x) thing is giving you an index that is too large, so you are not reading and writing the spots you think you are. You are actually lucky your program didn't throw an access violation.

Those two things are causing you grief.

One other thing you should watch out for is your mathematical expressions. For example, on line 102 you want to make sure y is in [1,7] inclusive, but you use the exclusive comparison operators, so you are actually checking to see that y is in [2,6] inclusive == (1,7) exclusive.

[edit] One last thing, you generally shouldn't encode ASCII values directly. Use the character literal instead. So line 39 would read y = y - '0'; This guarantees that the correct calculation happens, even on old dinosaurs using EBCDIC. :-@

Hope this helps.

:-)
Last edited on
Topic archived. No new replies allowed.