Hi All,

I have a simple question, I hope.

Here's the overview of the problem:
"There is a board game with 9 squares on it (three rows of 3 squares). Each square has a coin on it. The coins can be either heads or tails side up. You can only flip the coins that are heads, but when you do flip a head, the immediate coins to the left, right, above and below this coin also flip. However, if the coin being flipped is on the edge of the board, then you do not wrap around. For example, if the coin is in the center of the board, then there are 4 other coins that flip with it. If the coin is in the top right corner, then only two other coins flip with it."

There is an input file with 10 different "boards" in it. Represented as follows:
T T H
H T H
H H H
My Problem is how to load this file into my 2D array and let the compiler know if its a head or a tail? I was thinking of representing H with a 1 and T with a 0, but don't have an idea on how to go about it. I'm pretty sure I have an idea on what to do on the rest of the problem, but this starting step is giving me some trouble.

If you have any ideas on how to go about it please advise me on it. I'm not asking for you guys to write the whole program for me :), just this starting step. How to load an input file with ten boards of similar design represented above and how to let compiler know if its a head or a tail. Of course tips on other parts of the program will be appreciated :)

Thanks for your time,

bratok

Here's a though,
will using getline() function work? Create some sort of loop that checks which character is in place1, place2 or place 3 on each line and then assigning 1 or a 0 depending on if its a H or a T? Or am i making this way harder than it should be?

>>Or am i making this way harder than it should be?

Seems like a totally reasonable approach. But if you read one string at a time, the default behavior is to read a word at a time. Read each word, check for being "T" or "H" or other (invalid input), and fill your data-structure with the corresponding 0 and 1. Stop when your data structure is full or when you run out of input.

To read all words, you can do:

#include <fstream>
#include <string>

using namespace std;

int main() {
  ifstream file_in("my_data.txt");
  string s;
  while(file_in >> s) {
    cout << "Got word: '" << s << "'" << endl;
  };
  return 0;
};

The above program should print all the words contained in the input file.

I think that's a good starting point.

Thanks for the reply! Before I proceed with this approach, i have a quick question. I read in from file and just using the while loop like you have suggested, and simply output it into another file. Opening the output file I noticed that the letters are just printed out in one column
T
T
H
etc.
Here's the question. Is that how compiler read in data? IF it does, will it affect the way my boards will be read in? As they should be a grid of 3x3. Or is this just purely the way its written in the output file?(In which case it doesn't matter much)
I'm leaning towards the latter, but just wanted to make sure before I proceed.
BTW I really appreciate any input, I'm not very C++ savvy and it takes me a while to pick up even some simple concepts.

Thanks,

Edited 5 Years Ago by bratok: n/a

The input works by reading words by ignoring any "space" character. Here, a "space" character means anything from an actual space, to a tab, or a new-line.

The reason why it outputs in one column is because there is a "endl" (meaning "end-line") printed after each word, that has the effect of putting each word on a separate line.

So, your intuition is correct. The input ignores the column-row structure and just reads one word after another (left-to-right, top-to-bottom). And the only reason for the one-column output, is the way it is outputted.

For instance you could re-output the same structure with this code:

#include <fstream>
#include <string>
#include <iostream>

using namespace std;

int main() {
  ifstream file_in("my_data.txt");
  string s;
  int i = 0; //count the number of words read.
  while(file_in >> s) {
    cout << s << " ";
    if( (++i) % 3 == 0 )  //make new line at every 3 word.
      cout << endl;
  };
  return 0;
};

Thanks again! Actually I just played around with getline() function and was able to figure this out a few minutes ago :). For the sake of testing this out I just opened the file and basically copied its contents into the output file and the program printed out nice copies of the initial boards that were in the input file :)

ifstream fin("input3.txt");
	ofstream fout("output3.txt");

	string s;
	while(!fin.eof())
	{
		getline(fin,s);
		fout << s << endl;
	};

Thanks for the help! I'll most likely check in again in a little while with more questions :D

Edited 5 Years Ago by bratok: n/a

This article has been dead for over six months. Start a new discussion instead.