Thread: strange error...

  1. #16
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    the code that checks for the shot at the beginning of my else if statement is...check_shot..

    it looks like this..

    bool ship::check_space(char dir, int col, int row, ocean field)
    {
    if(dir == 'v') // place vertical
    {
    for(int i(0); i<length; i++)
    if(field.get_cell(row+i, col) != '~')
    return false;
    }
    else // place horizontal
    {
    for(int i(0); i<length; i++)
    if(field.get_cell(row, col+i) != '~')
    return false;
    }
    return true;
    }

    this code was provided for us by the teacher...but i thought i'd put it on here for reference for you

  2. #17
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>i run through it and it says index is 61 somehow
    Then follow the code path in your debugger, and determine what goes to make up 61. From that, you should be able to determine where it's gone wrong.

    AND USE CODE TAGS !
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #18
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    its hard to tell what it does w/ the debugger...i don't even know how i could get a 61...

    BBNERD AKA RETARD

  4. #19
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by BODYBUILDNERD
    its hard to tell what it does w/ the debugger...i don't even know how i could get a 61...

    BBNERD AKA RETARD
    Keep trying mate. Just step it through as best you can, breaking at points in the code where you think key variables will get altered.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #20
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    i can't figure it out..

    this hasn't been my day...i can't program,

    i find out my friggen gym that i work out at is closing...

    and finals are next week...

    AHH

    BBNERD

  6. #21
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Okay, look at this:

    index = (...) - 65

    (61) = (...) - 65

    (126) = int(field.get_cell(row-1, col-1))

    int(field.get_cell(row-1, col-1)) = '~'

    field.get_cell() is returning the character '~'! Is that one of the ships?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #22
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    nope not a ship...but i dunno where i coulda went wrong..hmm

  8. #23
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If you don't know what a ~ character represents in your cell array, then where did it come from? Did get_cell() work with the correct numbers or did that go outside of the array bounds too.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #24
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    the ~ represents an empty space, i'm just wondering how on the field, it shows me for debugging purposes it says there's a letter there, but then when i call it it says there isn't...

    ahh

  10. #25
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Post your whole program here; I usually don't like that, but in this case it seems to be the easiest way to go But make sure that you use code tags!
    i.e. [ c o d e ] /*stuff here*/ [ / c o d e ]
    without the spaces
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #26
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    ocean.h file

    Code:
    #ifndef OCEAN_H
    #define OCEAN_H
    #include <iostream>
    using namespace std;
    
    class ocean
    {
    private:
    	
    	//private variables
    	char grid[10][10];
    	int ship_area;
    	int num_hits;
    
    public:
    
    	//constructors	
    	ocean();
    
    	//accessor functions
    	int get_area() {return ship_area;}
    	int get_hits() {return num_hits;}
    	char get_cell(int row, int col) {return grid[row][col];}
    
    	//public member functions
    	void change_grid(int row, int col, char symbol){grid [row][col] = symbol;}
    	void calc_ship_area(int crus, int sub, int bat);
    	void add_hit() {num_hits ++;}
    	bool check_shot(int row, int col);
    	void print_grid(ostream & fout);
    };
    
    #endif
    ship.h file

    Code:
    #ifndef SHIP_H
    #define SHIP_H
    #include "ocean.h"
    #include <iostream>
    using namespace std;
    
    struct cell
    {
    	int row;
    	int col;
    };
    
    
    class ship
    {
    private:
    	
    	//private variables
    	int ship_id;
    	int length;
    	cell place_arry[4];
    	char status;
    	char placement;
    	int num_hits;
    
    	//private helper functions
    	void change_status();
    	void sink_ship(ocean & field);
    	void report_sunk();
    	void place_ship(ocean & field);
    	bool check_space(char dir, int col, int row, ocean field);
    
    public:
    
    	//constructors
    	ship();
    	ship(int id, int len, ocean & field);
    
    	//accessor functions
    	int get_length() {return length;}
    	int get_status() {return status;}
    	int get_id() {return ship_id;}
    
    	//public member functions
    	void inc_hits(ocean & field);
    	
    };
    
    #endif;
    ocean.cpp file
    Code:
    #include <iostream>
    #include<windows.h>
    #include "ocean.h"
    using namespace std;
    
    ocean::ocean()
    {
    	for (int i = 0; i < 10; i++)
    	{
    			for (int j = 0; j < 10; j++)
    			{
    				grid[i][j] = '~';
    				
    			}			
    	}
    		
    	ship_area = 0;
    	num_hits = 0;
    }
    
    
    void ocean::calc_ship_area(int crus, int sub, int bat)
    {
    	if (crus + sub + bat > 0 && crus + sub + bat <= 10)
    
    		ship_area = (2 * crus) + (3 * sub) + (4 * bat);
    }
    
    bool ocean::check_shot(int row, int col)
    {
    	if (grid[row][col] == '~')
    		return true;
    	else
    		return false;
    }
    void ocean::print_grid(ostream & fout)
    {
    	cout << "\t\t   1  2  3  4  5  6  7  8  9  10\n";
    	for (int l = 0; l < 10; l++)
    	{
    		if (l != 9)
    
    			cout << "\t\t" << l + 1 << "  ";
    
    		else
    
    			cout << "\t\t" << l + 1 << " ";
    
    
    		for (int i = 0; i < 10; i ++)
    		{			
    			cout <<  grid[l][i] << "  ";
    
    
    		}
    		cout << endl;
    	}
    
    	cout << endl;
    	
    }
    ship.cpp file
    Code:
    #include <iostream>
    #include "ship.h"
    #include <windows.h>
    using namespace std;
    
    ship::ship()
    {
    	ship_id = -1;
    	
    	for(int i = 0; i < 4; i++)
    		for(int k=0; k<2;k++)
    	{
    		place_arry[i].row = -1;
    		place_arry[i].col = -1;
    	}
    
    	status = ' ';
    	placement = ' ';
    	length = 0;
    	num_hits = 0;
    }
    
    ship::ship(int id, int len, ocean & field)
    {
    	ship_id= id;
    	length = len;
    	place_ship(field);
    	status = 'c';
    	num_hits = 0;
    }
    
    void ship::inc_hits(ocean& play)
    {
    
    	num_hits++;
    	change_status();
    
    	if (status == 's')
    
    		sink_ship(play);
    }
    
    void ship::change_status()
    {
    	if (status == 'c')
    	{
    		status = 'i';
    	}
    	else if (num_hits == length && status == 'i')
    	{
    		status = 's';//sunk
    	}
    }
    
    void ship::sink_ship(ocean & play)
    {
    	report_sunk();
    	for (int l = 0; l < length; l++)
    		play.change_grid(place_arry[l].row, place_arry[l].col, 'S');
    }
    
    void ship::report_sunk()
    {
    	cout << "        *****    ****   ****   ****     ****   ****   ****      **** **** \n";
    	cout << "       *     *   *  *   *  *   *   *    *  *   *  *  *  *       *  * *  * \n";
    	cout << "      *  ***  *  *  *   *  *   *     *  *  *   *  * *  *        *  * *  * \n";
    	cout << "       *  *  **  *  *   *  *   *  *   * *  *   *  *  *          *  * *  * \n";
    	cout << "        *  *     *  *   *  *   *  * *   *  *   *    *           *  * *  * \n";
        cout << "          *  *   *  *   *  *   *  *  *     *   *  *   *         *  * *  * \n";
    	cout << "       **  *  *  *  *   *  *   *  *   *    *   *  * *   *       **** **** \n";
    	cout << "      *  * *  *  *  *****  *   *  *    *   *   *  *   *   *     **** **** \n";
    	cout << "       *  *   *   *       *    *  *     *  *   *  *     *   *   *  * *  * \n";
    	cout << "         *****     *******     ****      ***   ****      *****  **** **** \n";
    	cout << "\a\a\a\a\a";
    	Sleep(1000);
    
    }
    
    
    void ship::place_ship(ocean& field)
    {
    	int i;
    	int direction; // 0 = vertical, 1 = horizontal
    	int start_col; // column to start ship placement in
    	int start_row; // row to start ship placement in
    	char placed;
    
    	direction = rand()%2;
    	if(direction == 0) // place vertically
    	{
    		placed = 'n';
    		placement = 'v';
    		do
    		{
    			start_col = rand()%10; // place in any column
    			start_row = rand()%(11-length); // start in top 7 rows
    			// check available space
    			if(check_space('v', start_col, start_row, field))
    			{
    				for(i=0; i<length; i++)
    				{
    					field.change_grid(start_row+i, start_col, ship_id+65);
    					place_arry[i].row = start_row+i	;
    					place_arry[i].col = start_col;
    				}
    				placed = 'y';
    			}
    		}while(placed != 'y');
    	}
    	else // place horizontally
    	{
    		placed = 'n';
    		placement = 'h';
    		do
    		{
    			start_col = rand()%(11-length); // start in first 7 columns
    			start_row = rand()%10; // place in any row
    			// check available space
    			if(check_space('h', start_col, start_row, field))
    			{
    				for(i=0; i<length; i++)
    				{
    					field.change_grid(start_row, start_col+i, ship_id+65);
    					place_arry[i].row = start_row	;
    					place_arry[i].col = start_col+i;
    				}
    
    				placed = 'y';
    			}
    		}while(placed != 'y');
    	}
    }
    
    bool ship::check_space(char dir, int col, int row, ocean field)
    {
    	if(dir == 'v') // place vertical
    	{
    		for(int i(0); i<length; i++)
    			if(field.get_cell(row+i, col) != '~')
    				return false;
    	}
    	else // place horizontal
    	{
    		for(int i(0); i<length; i++)
    			if(field.get_cell(row, col+i) != '~')
    				return false;
    	}
    	return true;
    }
    and finally the main..battleship.cpp

    Code:
    #include<iostream>
    #include<windows.h>
    #include <ctime>
    #include <fstream>
    #include "ocean.h"
    #include "ship.h"
    using namespace std;
    
    void howmany(ship ship_ary[10], int & numships, ocean & field);
    void graphic(ostream & fout, char shot);
    void pick(int & row, int & col);
    
    int main()
    {	
    	ship ship_arry[10];
    	int numships;
    	ocean field, play;
    	ofstream fout;
    	int guesses;
    	char newgame = 'y';
    	int index;
    	char gameover;
    	int row = -1, col = -1;
    	fout.open("a:battle.txt");
        
    	srand ((unsigned) time((time_t *)NULL));
    
    	while (newgame == 'y')
    	{	
    		play = ocean();
    		field = ocean();
    		
    		guesses = 0;
    		gameover = 'n';
    
    		howmany(ship_arry, numships, field);
    		field.print_grid(cout);
    		cout << "\tThis is the actual ship placement. It is provided for debugging only.\n";
    		cout << "\t";
    		system("pause");
    		system("cls");
    		pick(row, col);
    
    		while (row != -1 || gameover == 'n')
    		{	
    			if (play.get_cell(row-1, col-1) == 'H' || play.get_cell(row-1, col-1) == 'S')
    			{
    				cout << "\tI'm sorry, you've already tried at this row and column\n";
    				cout << "\tUnfortunately this will be counted as a miss on the counter\n";
    				system("pause");
    				system("cls");
    			}
    			else if (play.get_cell(row-1, col-1) == 'M')
    			{
    				cout << "You already guessed this! This counts as a miss\n\n";
    				system("pause");
    				system("cls");
    			}
    			else if (field.check_shot(row-1, col-1))
    			{
    				graphic(cout, 'H');
    				index = int(field.get_cell(row-1, col-1)) - 65;
    				field.change_grid(row-1, col-1, '*');
    				play.change_grid(row-1, col-1, 'H');
    				ship_arry[index].inc_hits(play);
    				system ("cls");
    				play.print_grid(cout);
    				field.add_hit();
    				if (field.get_hits() == field.get_area())
    				{
    					gameover = 'y';
    				}
    			}
    
    			else
    			{
    				graphic(cout, 'M');
    				field.change_grid(row-1, col-1, '-');
    				play.change_grid(row-1,col-1, 'M');
    				system("cls");
    				play.print_grid(cout);
    			}
    				guesses++;
    
    				if (gameover == 'n')
    					pick(row, col);			
    		}
    
    		cout << "\n\n\tYou took " << guesses << " guesses to clear the ocean!\n\n";
    		cout << "\tYour score is " << guesses - field.get_area() << "!!\n\n";
    
    		cout << "\tDo you want to play another game? \n\n\n";
    		cout << "\tSelect (y) to start a new game, (n) to quit: ";
    		cin >> newgame;
    		system ("cls");			
    
    	}
    
    	cout << "END OF BATTLESHIP 1.0!";
    	Sleep(2000);
    	fout.close();
    
    	return 0;
    }	
    
    void howmany(ship ship_arry[10], int & numships, ocean& field)
    {	
    	int temp2, temp3, temp4;
    	char cont;
    	
    	do
    	{
    	cout << "\tWELCOME TO JOSH'S GENERIC BATTSHIP 1.0!\n\n\n";
    	cout << "\tYou can pick up to 10 ships (battleships, cruisers, and submarines)\n";
    	cout << "\tto be placed on the board by the computer.\n";
    
    	cout <<"\n\n\t Please enter the number of: \n\n\n";
    	cout <<" \t\tBATTLESHIPS: ";
    	cin >> temp2;
    	cout <<"\n\n\t\tCRUISERS: ";
    	cin >> temp3;
    	cout <<"\n\n\t\tSUBMARINES: ";
    	cin >> temp4;
    	cout << "\n\n";
    
    	if (temp2 + temp3 + temp4 <=10 && temp2 + temp3 + temp4 > 0)
    	{
    		cont = 'n';
    	} 
    	else if (temp2 + temp3 + temp4 <= 0)
    	{
    		cout << "You need to pick at least one ship to be placed!\n\n";
    		system("pause");
    		temp2 = 0;
    		temp3 = 0;
    		temp4 = 0;
    		cont = 'y';
    	}
    
    	else 
    	{
    		cout << "The number of ships you selected exceeds 10!\n\n";
    		system("pause");
    		temp2 = 0;
    		temp3 = 0;
    		temp4 = 0;
    		cont = 'y';
    	}
    
    	system ("cls");
    	}while(cont == 'y');
    
    	field.calc_ship_area(temp2, temp3, temp4);
    
    	for (int i = 0; i < temp2; i++)
    		ship_arry[i] = ship(i, 2, field);
    	for(; i < temp2 + temp3; i++)
    		ship_arry[i] = ship(i, 3, field);
    	for(; i < temp2 + temp3 + temp4; i++)
    		ship_arry[i] = ship(i, 4, field);
    
    	numships = temp2 + temp3 + temp4;
        	
    }
    
    void graphic(ostream & fout, char shot)
    {
    	if (shot == 'H')
    	{
    		fout << "       ***********               *                *         *       ****\n";
    		fout << "      *  * * * *  *             * *              * *       * *      *  *\n";
    		fout << "      *  *      *  *           * * *            *   *     *   *     *  *\n";
    		fout << "      *  *      *  *          * * * *          *  *  *   *  *  *    *  *\n";
    		fout << "      *  * * * *  *          * *   * *         * *  *  *  *  * *    *  *\n";
    		fout << "      *           *         * *     * *        * *   *   *   * *    *  *\n";
    		fout << "      *  * * * *   *        * * * * * *        * *     *     * *    *  *\n";
    		fout << "      *  *      *  *        * * * * * *        * *           * *    ****\n";
    		fout << "      *  *      *  *        * *     * *        * *           * *    ****\n";
    		fout << "      *  * * * *  *         * *     * *        * *           * *    *  *\n";
    		fout << "       ***********          ***     ***        ***           ***    ****\n";
            fout << "\a\a\a\a\a";
    		Sleep(1000);
    	}
    
    	else 
    	{
    		fout << "         *        *        ********      *****         *****          \n";
    		fout << "        * *      * *       *      *     *     *       *     *           \n";
    		fout << "       *   *    *   *      ***  ***    *  ***  *     *  ***  *        \n";
    		fout << "      *     *  *     *       *  *       *  *  **      *  *  **           \n";
    		fout << "      *  *   *    *  *       *  *        *  *          *  *        \n";
    		fout << "      *  * *    * *  *       *  *          *  *          *  *        \n";
    		fout << "      *  *   **   *  *       *  *        **  * *      **  *  *    \n";
    		fout << "      *  *        *  *     ***  ***     *  * *  *     *  * *  *    \n";
    		fout << "      *  *        *  *     *      *      *  *   *      *  *   *    \n";
    		fout << "      ****        ****     ********        *****         ***** ";
     		fout << "\a\a\a\a\a";
    		Sleep(1000);
    	}
    }
    
    void pick(int & row, int & col)
    {
    	char cont='n';
    
    	do
    	{
    	cout << "\tHit -1 to end this game.\n\n\n";
    	cout << "\trow: ";
    	cin >> row;
    	cout << "\n\n\tcolumn: ";
    	cin >> col;
    
    	if (row > 10 || row < 0 || col >10 || col < 0)
    	{
    		cont = 'n';
    		cout << "\tThe row or column you picked exceeded 10 or is less than 0\n";
    		cout <<"\n";
    		cout <<"\t";
    		system("pause");
    		system("cls");
    	}
    	else
    		cont = 'y';
    		system("cls");
    
    	}while(cont == 'n');
    
    	
    }

    there it is..LOL HELP
    Last edited by BODYBUILDNERD; 12-04-2002 at 09:17 PM.

  12. #27
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    ahh, code tags use square brackets, not angled ones! [, not <! It's not too late, click on "edit post" and fix it!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #28
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    I FIXED IT..woohoo..got HTML on the brain too

  14. #29
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    when i shoot a missle, it finds a spot on the field that isn't where its located when i look at the field itself..thats weird...

    AHH again and again

  15. #30
    In Battleship.cpp - main
    Code:
    srand ((unsigned) time((time_t *)NULL));
    
    	while (newgame == 'y')
    	{	
              play = Ocean();//CHECK HERE
    		field = Ocean();//CHECK HERE
    		
    		guesses = 0;
    		gameover = 'n';
    Also... in main
    Code:
    field.calc_ship_area(temp2, temp3, temp4);
    
    	for (int i = 0; i < temp2; i++)
    	
    	ship_arry[i] = Ship(i, 2, field);//Check here
    	for(; i < temp2 + temp3; i++)
    		ship_arry[i] = Ship(i, 3, field);//HERE
    	for(; i < temp2 + temp3 + temp4; i++)
    		ship_arry[i] = Ship(i, 4, field);//Check here
    
    	numships = temp2 + temp3 + temp4;
        	
    }
    
    void graphic(ostream & fout, char shot)
    Everything I highlighted is something that appears to not make sense. You also have some logic errors b/c the program would not let me choose an area to hit, it just kept saying "press any button to continue" in a LOOP.

    **PS I changed your code just for my sake to Capitilize the Ocean and Ship objects, so just uncap them for your changes I made several other changes to your code but tell me if you want me to post the whole bit**
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM