-1

I am creating a battleship game using c++, we have to load text files which contain the user's guesses & another file which contains the boat locations. I am approaching this by loading the data from the files into a 2D array. The goal is to be able to compare the two 2D arrays in order to determine if the user has won the game. I am struggling with loading the data into the array. This is my code for inputting the Users guess file.

#include "stdafx.h"
#include "openconfigfile.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void openconfigfile()
{
    int row = 12;

    //Open the file in the location specified by user  
    ifstream file("C: Project Files\\Proj01_files\\in1.txt");

    //Check that the file was opened & loaded into array 
    if (file.is_open())
    {
        cout << "User input file has been loaded." << endl;
        string inputarray[12][2];

        while (file.good()) {
            for (int col = 0; col < 2; col++) {
                file >> inputarray[12][2];
            }
            row++;
        }

    }
    else cout << "Unable to open user input file.Please check your file path is correct.";
    file.close();

}

I know there is something wrong with the while loop since every time I run the project it tells me that my project file has stopped working. I am not sure how to fix it. Any help would be appreciated.

123556666
  • 7
  • 2

1 Answers1

0

First of all, what is the variable row doing?

 int row = 12;

Why is it initialized to 12?

Making assumptions on your code, it should be

int row = 0;

In the while loop, you can change the condition to eof

while (file.good())

This should be

while (!file.eof())

You are using the for loop but feeding the data in the same index of the array because of this LOC

file >> inputarray[12][2];

Again making assumptions on your code,

file >> inputarray[row][col];

This makes your function as following.

int row = 0;

//Open the file in the location specified by user  
ifstream file("C: Project Files\\Proj01_files\\in1.txt");

//Check that the file was opened & loaded into array 
if (file.is_open())
{
    cout << "User input file has been loaded." << endl;
    string inputarray[12][2];

    while (!file.eof()) {//EOF is end of file, if the file end hasn't reached, keep looping
        for (int col = 0; col < 2; col++) {
            file >> inputarray[row][col];
        }
        row++;
    }

}
else cout << "Unable to open user input file.Please check your file path is correct.";
file.close();

As the comment of @Someprogrammerdude states, you should really go back a few steps and learn more about arrays.

But still it's a great try to learn programming and filing. Hope i helped!

Farhan Qasim
  • 990
  • 5
  • 18
  • Please note that if the row exceeds 12, it would give an exception because your 2D array is basically a 12 by 2 array. So you can add an addition check in your while loop 'while(!file.eof()&&row<12)' If you expect your file to contain more than 12 rows. – Farhan Qasim Feb 01 '18 at 15:09