Like Tree5Likes
  • 1 Post By anduril462
  • 1 Post By CommonTater
  • 1 Post By anduril462
  • 2 Post By time4f5

error c2664???

This is a discussion on error c2664??? within the C Programming forums, part of the General Programming Boards category; Hi expert programmers I have this code that is giving me error c 2664 on manually place ship. can you ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Location
    Baltimore Md. USA
    Posts
    58

    error c2664???

    Hi expert programmers I have this code that is giving me error c 2664 on manually place ship. can you check it out please, I would greatly appreciate it.
    Code:
    /**/  
    #include <stdio.h>
      #include <stdlib.h>
    #include <time.h>
     
      //function declarations
           int welcome_screen(void);
    	  void initialize_game_board (char game_board[10][10], int num_rows, int num_cols);
    	  void print_game_board (char game_board[10][10], int num_rows, int num_cols);
           int check_shot(char target, int target_row, int target_col);
           int is_winner(char game_board[10][10]);
          void manually_place_ships_on_board(char *game_boardptr, char ship);
    	  void randomly_place_ships_on_board(char *game_boardptr, char ship, int dir);
    	  void generate_start_pt (int len_ship, int *row_coord, int *col_coord, int dir);
          void generate_direction (int *dir);
    	   int print_menu (void);
    	  void get_target (int *target_row, int *target_col);
    	  void rand_target (int *target_row, int *target_col);
          void update_board(int hit_or_miss, char *target);
           int search_ship(char game_board[10][10], int num_rows, int num_cols, char ship);
    
        /*+++++++++++++++++++++++++++++main++++++++++++++++++++*/     
    	   int main (void)//main
     
          {//main
    		//local declaration and assignments
          char game_board_1[][10] = {{'~', '~'}, {'~'}};
          char game_board_2[][10] = {{'~', '~'}, {'~'}};
          char game_board_target[10][10] = {{'~', '~'}, {'~'}};
          int direction = 0, row_coord = 0, col_coord = 0, choice = 0, turn = 0, hit_or_miss = 0, target_row = 0, target_col = 0;
    
          int number_of_ships_1 = 0, number_of_ships_2 = 0;
     
          srand ((unsigned int)time(NULL));//random 
    
          choice = welcome_screen();
     
          if (choice == 1)
          {//if choice
          initialize_game_board(game_board_1, 10, 10);//these are function calls for if statement
    	  initialize_game_board(game_board_2, 10, 10);
          initialize_game_board(game_board_target, 10, 10);
    	        choice = print_menu();
    
               if (choice == 1)
    			         {//if
         print_game_board(game_board_1, 10, 10);
    	  
    	  system("pause");//in order to update board
    	  system("cls");
    
          manually_place_ships_on_board(*game_boardptr, 'c');
    	  print_game_board(game_board_1, 10, 10);
    
          system("pause");
    	  system("cls");
    	  
    	  manually_place_ships_on_board(game_board_1, 'b');
    	  print_game_board(game_board_1, 10, 10);
    
          system("pause");
    	  system("cls");
    
          manually_place_ships_on_board(game_board_1, 'r');
    	  print_game_board(game_board_1, 10, 10);
    	        
          system("pause");
    	  system("cls");
    	  
    	  manually_place_ships_on_board(game_board_1, 's');.
          print_game_board(game_board_1, 10, 10);
    
          system("pause");
          system("cls");
    
          manually_place_ships_on_board(game_board_1, 'd');
         }//if choice 1
      
          else if (choice == 2)
          {
          generate_direction(&direction);//if play is computer, ie randomly placed fleet
          randomly_place_ships_on_board(game_board_1, 'c', direction);
          generate_direction(&direction);
          randomly_place_ships_on_board(game_board_1, 'b', direction);
          generate_direction(&direction);
           randomly_place_ships_on_board(game_board_1, 'r', direction);
    	   generate_direction(&direction);
    	   randomly_place_ships_on_board(game_board_1, 's', direction);
           generate_direction(&direction);
    	   randomly_place_ships_on_board(game_board_1, 'd', direction);
    	   }
    
          generate_direction(&direction);
          randomly_place_ships_on_board(game_board_2, 'c', direction);
           generate_direction(&direction);
    	   randomly_place_ships_on_board(game_board_2, 'b', direction);
           generate_direction(&direction);
           randomly_place_ships_on_board(game_board_2, 'r', direction);
           generate_direction(&direction);
    	   randomly_place_ships_on_board(game_board_2, 's', direction);
           generate_direction(&direction);
           randomly_place_ships_on_board(game_board_2, 'd', direction);
    
          print_game_board(game_board_1, 10, 10);
    
          printf("Target Board\n");
    	  print_game_board(game_board_target, 10, 10);
    
          number_of_ships_1 = 5;
    	  number_of_ships_2 = 5;
    	    if (turn == 1)
    		{
    		 get_target(&target_row, &target_col);
    		 hit_or_miss = check_shot(game_board_2, target_row, target_col);
    		 update_board(hit_or_miss, game_board_2[target_row][target_col]);
    		 update_board(hit_or_miss, game_board_target [target_row][target_col]);
    		 printf("Target Board\n");
    
    		print_game_board(game_board_target, 10, 10);
    
    		system("pause");
    		system("cls");
    	  }
    
          else
         {
          rand_target(&target_row, &target_col);
          hit_or_miss = check_shot(game_board_1, target_row, target_col);
          update_board(hit_or_miss, game_board_1[target_row][target_col]);
          printf("Player 1 Board\n");
          print_game_board(game_board_1, 10, 10);
          }
    
        turn = (turn + 1) % 2 + 1;
        while (number_of_ships_2 != 0 || number_of_ships_2 != 0)
          {
          if (turn == 1)
          {
         get_target(&target_row, &target_col);
          hit_or_miss = check_shot(game_board_2, target_row, target_col);
          update_board(hit_or_miss, game_board_2[target_row][target_col]);
          number_of_ships_2 = is_winner(game_board_2);
          printf("Target Board\n");
          print_game_board(game_board_target, 10, 10);
          system("pause");
          system("cls");
          }
    
          else
          {
          rand_target(&target_row, &target_col);
          hit_or_miss = check_shot(game_board_1, target_row, target_col);
          update_board(hit_or_miss, game_board_1[target_row][target_col]);
         number_of_ships_1 = is_winner(game_board_1);
          printf("Player 1 Board\n");
          print_game_board(game_board_1, 10, 10);
          }
          }
    
         if (number_of_ships_1 == 0)
          {
          printf("Sorry you lost. Better luck next time.\n");
          }
    
          else if (number_of_ships_2 == 0)
          { printf("Congratulations! You won!\n");
    	       }
    	       }
    		return 0;
    		 }
        /*++++++++++++++++++++++++++++++++++welcomeScreen+++++++++++++++++*/
          int welcome_screen(void)  /* prints out the menu */
          {
          int choice = 0;
    
          printf("****************Welcome to ***************\n");
    	  printf("***************BattleFleet****************\n");
    	  printf("*****************************************\n");
    	  printf("1. To start playing please press one (1).\n2. Or to quit please press two (2).\n");
    	  /* sets the value of choice to the user's choice. */
    
          scanf("%d", &choice);
    	  return choice;
    	       }
    	  /*++++++++++++++++++++initialize game board+++++++++++++++++++++++++*/
    	  void initialize_game_board (char game_board[][10], int num_rows, int num_cols)
    		  {
    			  int row_index = 0, col_index = 0;
    	    for (row_index = 0; row_index < num_rows; row_index++)
    			{
    				for (col_index = 0; col_index < num_cols ; col_index++)
    				{
    					game_board[row_index][col_index] = '~';
    					   }
    				     }
    		 }
    	  /*++++++++++++++++++++++++++++++printGameBoard++++++++++++++++++++++*/
    	  void print_game_board (char game_board[10][10], int num_rows, int num_cols)
    		        {
    					      int row_index = 0, col_index = 0;
          printf(" ");
          for (col_index = 0; col_index < num_cols; col_index++)
          {
          printf(" %d", col_index);
          }
    	        putchar ('\n');
    
          for (row_index = 0; row_index < num_rows; row_index++)
          {
          printf("%d ", row_index);
    	      for (col_index = 0; col_index < num_cols ; col_index++)
    			  {
    				  printf("%c ", game_board[row_index][col_index]);
    				  }
       putchar ('\n');
             }
    	        }
    	  /*+++++++++++++++++++++++++++++checkShot++++++++++++++++++++++++++*/
          int check_shot(char game_board[10][10], int target_row, int target_col)
    
          {
    
          if (game_board[target_row][target_col] == '~')
          {
    		 printf("Miss...");
    			return 0;
         }
    
          else
         {
          printf("Hit!");
          return 1;
          }
          }
    	  /*++++++++++++++++++++++++++++search Ship++++++++++++++++++++++++++*/
      int search_ship(char game_board[10][10], int num_rows, int num_cols, char ship)
          {
          int row = 0, col = 0, status = 0;
          for (row = 0; row < num_rows; row++)
          {
          for (col = 0; col < num_cols; col++)
          {
          if (game_board[row][col] == ship)
          status = 1;
          break;
          }
          }
    
          return status;
           }
      /*++++++++++++++++++++++++++++Update board+++++++++++++++++++++++++++*/
     void update_board(int hit_or_miss, char *target)
    	       {
    
          if (hit_or_miss == 1)
    		  {
    			  *target = '*';
    			  }
     
          else
    		  {*target = 'm';
    	        }
    	        }
     /*++++++++++++++++++++++++++++++++print Menu++++++++++++++++++++*/
          int print_menu (void)/* prints out the menu */
    	 {
           int choice = 0;
          printf("*************************\n");
    
          printf("********* Menu **********\n");
    
          printf("*************************\n");
    
          printf("1. Player vs Player press one (1).\n2. Or Computer vs Player press two (2).\n");
    
               /* sets the value of choice to the user's choice. */
    	   scanf("%d", &choice);
    	        return choice;
    			}
    	  /*+++++++++++++++++++++++++++++++++manually place ships++++++++++++++++++++++++++++*/
          void manually_place_ships_on_board(char *game_boardptr,char ship) /*int row_index1, int col_index1, int row_index2, int col_index2, int row_index3, int col_index3, int row_index4, int col_index4, int row_index5, int col_index5)*/
    
          {
    
          int size = 0, row_index = 0, col_index = 0, count = 0;
    	    if (ship == 'c')
    			{size = 5;
    		printf("Please select where to place the Cruiser by entering five sets of coordinates.\n");
    		   }
           else if (ship == 'b')
    		   {size = 4;
          printf("Please select where to place the Battleship by entering four sets of coordinates.\n");
    	        }
    
          else if (ship == 'r' || ship == 's')
    		  {size = 3;
          if (ship == 'r')
          {
          printf("Please select where to place the Cruiser by entering three sets of coordinates.\n");
          }
          else
          { printf("Please select where to place the Submarine by entering three sets of coordinates.\n");
          }
          }
          else
          {  size = 2;
          printf("Please select where to place destroyer by entering two sets of coordinates.\n");
          }
       while (count < size)
        {
          printf("Enter a set of coordinates: \n");
          printf("Row: ");
          scanf("%d", &row_index);
    
          printf("Column: ");
          scanf("%d", &col_index);
    
         if (game_board[row_index][col_index] == '~')
         {
         game_board[row_index][col_index] = ship;
         count++;
          }
          else
          {
          printf("Error illegal ship placement. Please enter another set of coordinates.");
    
          }
          }
          }
    	  /*+++++++++++++++++++++++++++generate direction++++++++++++++++*/
          void generate_direction (int *dir)
          {
          *dir = rand () % 2;
          }
       void generate_start_pt (int len_ship, int *row_coord, int *col_coord, int dir)
          {
         switch (dir)
          {
          case 0:
         *row_coord = rand () % 10;
          *col_coord = rand () % (10 - len_ship + 1);
          break;
    
          case 1:
          *row_coord = rand () % (10 - len_ship + 1);
          *col_coord = rand () % 10;
          break;
          }
          }
       /*++++++++++++++++++++++++++++++randomly placed ships/ computer ++++++++++++++++*/
        void randomly_place_ships_on_board(char game_board[10][10], char ship, int dir)
          {
          int row_coord = 0, col_coord = 0, size = 0, count = 0;
           
         if (ship == 'c')
          {size = 5;
          }
          else if (ship == 'b')
          {size = 4;
          }
          else if (ship == 'r' || ship == 's')
          {size = 3;
          }
          else
          {
          size = 2;
          }
           /*+++++++++++++++++++++++++++++generate start point+++++++++++++*/
         generate_start_pt(size, &row_coord, &col_coord, dir);
          while (count < size)
          {
          if (game_board[row_coord][col_coord] == '~')
          {
          game_board[row_coord][col_coord] = ship;
          if (dir == 0)
          {
       col_coord++;
          }
    
          else
          {
          row_coord++;
          }
         count++;
          }
         }
          }
    	/*++++++++++++++++++++++++++++getTargetManuallly+++++++++++++++++++++++++++*/
          void get_target (int *target_row, int *target_col)
         {
    
          printf("Please enter a target coordinate: \n");
          printf("Row: ");
          scanf("%d", target_row);
          printf("Column: ");
         scanf("%d", target_col);
     }/*++++++++++++++++++++++++++randomTarget/computer++++++++++++++++++++++*/
          void rand_target (int *target_row, int *target_col)
          {
          *target_row = rand() % 9 + 1;
          *target_col = rand() % 9 + 1;
    }
    	  /*++++++++++++++++++++++++++++++winner+++++++++++++++++++++++++*/
          int is_winner (char game_board[10][10])
    
          {
          int number_of_ships = 0;
          number_of_ships += search_ship(game_board, 10, 10, 'c');
          number_of_ships += search_ship(game_board, 10, 10, 'b');
          number_of_ships += search_ship(game_board, 10, 10, 'r');
          number_of_ships += search_ship(game_board, 10, 10, 's');
          number_of_ships += search_ship(game_board, 10, 10, 'd');
           
    	  return number_of_ships;
           }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Location
    Baltimore Md. USA
    Posts
    58
    here is the compile error
    (52): error C2664: 'manually_place_ships_on_board' : cannot convert parameter 1 from 'char [2][10]' to 'char *'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    It's probably down to the lack of consistency you have when passing around the game board.

    void initialize_game_board (char game_board[10][10], int num_rows, int num_cols);
    vs.
    void manually_place_ships_on_board(char *game_boardptr, char ship);

    But your code is too badly indented to read carefully.
    SourceForge.net: Indentation - cpwiki
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Location
    Baltimore Md. USA
    Posts
    58
    sorry about that. I had copied it to a word doc and it justified everything to the margin, because it is so long, I didn't take the time to move it all around again.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,512
    Why does your code bear such a strong resemblance to this code: Battleship help - C.

    Maybe you don't know what the problem is because you didn't write it?
    Salem likes this.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Location
    Baltimore Md. USA
    Posts
    58
    Correct. I found this and thought it would be pretty cool to get it to work. It seems like it will be a fun game. It isnt for a class or anything

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by time4f5 View Post
    Correct. I found this and thought it would be pretty cool to get it to work.
    It would be WAY cool if you wrote it yourself.
    This way it's just a headache that aspirin won't cure.
    Salem likes this.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Location
    Baltimore Md. USA
    Posts
    58
    I'm not seeing why this is a big deal. I found some code that someone asked for help with. I am trying to get it to work.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,512
    The original code is from over a year ago. I highly doubt that guy cares about getting his battleship program fixed at this point. If you just want a battle ship program, download a free version of the game, there are far better ones than this out there, and open source to boot. If you want to feel good about your creation, it helps if you create it. If you wanted to learn from this, you found a piece of code that is mediocre at best, so you will not learn much, and probably pick up some bad habits along the way.

    And, while I'd like to believe this is all for fun, there is a statistically significant chance that you have this for a homework assignment and are lying to get us to help you cheat your way through class. It's even more likely since your recent posts (http://cboard.cprogramming.com/c-pro...er-result.html, homework (reading and writing a file)) suggest you're in a programming class, for which a battleship program would be an appropriate assignment.
    Salem likes this.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Location
    Baltimore Md. USA
    Posts
    58
    Classes have already ended and because I'm not employed as a programmer I thought i should keep it fresh by looking for a problem to fix. This one looked fun. Statistics are wrong at times.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Location
    Baltimore Md. USA
    Posts
    58
    I can't figure this code out but I'm now inspired to try one on my own.
    Salem and anduril462 like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error: C2664 : sprintf_s() cannot convert
    By klipseracer in forum C++ Programming
    Replies: 14
    Last Post: 03-25-2008, 02:32 AM
  2. error C2664
    By carolsue2 in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2006, 10:17 AM
  3. Need Help on C2664 error
    By jamez05 in forum C++ Programming
    Replies: 8
    Last Post: 10-28-2005, 12:40 PM
  4. Error C2664
    By carisma36 in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2004, 02:22 AM
  5. C2664 Compile Error
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-18-2002, 12:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21