I am having trouble running this code properly, I believe it has something to do with the location of my board.dat file, but am unsure how to exactly fix it. Upon running the code, I can not open file error. Attached is an image of what my board.dat text file looks like that is located on my desktop. #include #include #include using namespace std; // function prototypes void Fire(char board[][25]); void FleetSunk(char board[][25], int&); int main() { // declaring board char board[25][25]; ifstream infile; // opening the input file infile.open("board.dat"); if (!infile) { cout << "Can not open file." << endl; } // taking input from file for (int i = 0; i < 25; i++) { for (int j = 0; j < 25; j++) { char bS; infile >> bS; board[i][j] = bS; } } int fS = 0; // calling functions do { Fire(board); FleetSunk(board, fS); } while (fS == 0); system("PAUSE"); } void Fire(char board[25][25]) { int row = 0, row1 = 0; int col = 0, col1 = 0; // taking values from user for row and column cout << "Enter the Row and Column that you would like to try and hit :"; cin >> row1; cin >> col1; row = row1 - 1; col = col1 - 1; // printing msg switch (board[row][col]) { case '#': if (board[row - 1][col] == 'H') { cout << "HIT AGAIN" << endl; board[row][col] = 'H'; } else if (board[row + 1][col] == 'H') { cout << "HIT AGAIN" << endl; board[row][col] = 'H'; } else if (board[row][col - 1] == 'H') { cout << "HIT AGAIN" << endl; board[row][col] = 'H'; } else if (board[row][col + 1] == 'H') { cout << "HIT AGAIN" << endl; board[row][col] = 'H'; } else { cout << "HIT" << endl; board[row][col] = 'H'; } break; case '~': cout << "MISS" << endl; break; case 'H': cout << "You already destroyed these coordinates." << endl; break; } } void FleetSunk(char board[25][25], int& fS) { // checking if all ships have sunk for (int i = 0; i < 25; i++) { for (int j = 0; j < 25; j++) { if (board[i][j] == '#') { fS = 0; return; } } } cout << "The Fleet has been destroyed!" << endl; fS = 1; }

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter10: Object-oriented Programming
Section: Chapter Questions
Problem 1FTB
icon
Related questions
Question

I am having trouble running this code properly, I believe it has something to do with the location of my board.dat file, but am unsure how to exactly fix it. Upon running the code, I can not open file error. Attached is an image of what my board.dat text file looks like that is located on my desktop.

#include<iostream>

#include<fstream>

#include<string.h>

using namespace std;

// function prototypes

void Fire(char board[][25]);

void FleetSunk(char board[][25], int&);

 

int main()

{

// declaring board

char board[25][25];

ifstream infile;

// opening the input file

infile.open("board.dat");

 

if (!infile) {

cout << "Can not open file." << endl;

}

 

// taking input from file

for (int i = 0; i < 25; i++) {

for (int j = 0; j < 25; j++) {

char bS;

infile >> bS;

board[i][j] = bS;

}

}

int fS = 0;

// calling functions

do {

 

 

Fire(board);

FleetSunk(board, fS);

 

}

while (fS == 0);

 

 

system("PAUSE");

}

 

 

void Fire(char board[25][25])

{

 

int row = 0, row1 = 0;

int col = 0, col1 = 0;

 

// taking values from user for row and column

cout << "Enter the Row and Column that you would like to try and hit :";

cin >> row1;

cin >> col1;

 

row = row1 - 1;

col = col1 - 1;

// printing msg

switch (board[row][col]) {

case '#':

if (board[row - 1][col] == 'H') {

cout << "HIT AGAIN" << endl;

board[row][col] = 'H';

}

else if (board[row + 1][col] == 'H') {

cout << "HIT AGAIN" << endl;

board[row][col] = 'H';

}

else if (board[row][col - 1] == 'H') {

cout << "HIT AGAIN" << endl;

board[row][col] = 'H';

}

else if (board[row][col + 1] == 'H') {

cout << "HIT AGAIN" << endl;

board[row][col] = 'H';

}

else {

cout << "HIT" << endl;

board[row][col] = 'H';

}

break;

case '~':

cout << "MISS" << endl;

break;

case 'H':

cout << "You already destroyed these coordinates." << endl;

break;

 

}

 

}

 

void FleetSunk(char board[25][25], int& fS) {

// checking if all ships have sunk

for (int i = 0; i < 25; i++) {

for (int j = 0; j < 25; j++) {

if (board[i][j] == '#') {

fS = 0;

return;

}

}

}

cout << "The Fleet has been destroyed!" << endl;

fS = 1;

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 9 images

Blurred answer
Knowledge Booster
Graphical User Interface
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:
9780357392676
Author:
FREUND, Steven
Publisher:
CENGAGE L