Making a Game for a school project and Im using a 2D array to make the gameboard. Well in the begginning of the game the user has to input what the current game board looks like. I have it so the user is entering data and the data is being entered into the array,but the newline character at the end of each input is missing it up,here is the portion of code Im working with atm.

char board;
      int a = 6,i = 6;
      cout << " Please enter what the board looks like: "<<endl;
      cin.get(board);

      while(cin)
      {
          cout<<"board is ="<<board<<"and going into position"<<a<<i<<endl;
          /*
          if(board == ' ')
          gameBoard[a][i] = ' ';
          else
          gameBoard[a][i] = board;
          */
          if(i>0)
          {
           i--;
          }
          else
          {
           a--;
           i = 6;
          }
          cin.get(board);
      }

Recommended Answers

All 6 Replies

Hi Hyiero

The problem is that cin.get(board) does not remove the 'newline' from the input stream.
Look at the sticky thread called "How do I flush the input stream?"

That should solve your problem

Ok I read the sticky and it helped somewhat,I used his ignore_line function but the problem is that it gathers the entire line as the char,instead of just one character at a time so I have the program trying to enter, ..sss.. into one postion into the array where is should enter . move to the next postion then enter . again
here is what my code looks like now

ignore_line(cin);
          cout<<"board is ="<<board<<"and going into position"<<a<<i<<endl;
          /*
          if(board == ' ')
          gameBoard[a][i] = ' ';
          else
          gameBoard[a][i] = board;
          */
          if(i>0)
          {
           i--;
          }
          else
          {
           a--;
           i = 6;
          }
        //  cin.clear();
          cin.get(board);

I question the wisdom of using while(cin) as a sentinel representing "finished entering data". That might work just fine for a file, but this isn't a file.

An example of how things should end up in the array after everything is done, given certain user input, might be in order, as well as the 2-D array dimensions.

Ok Here is an example of how the input should look and then what should be printed afterwards
and Dimensions:
char gameBoard[8][8];


INPUT:

F.F  
  ...  
....S..
SSSS.SS
SSSSSSS
  SSS  
  SSS

OUTPUT:

7   F.F
6   ...
5 ....S..
4 SSSS.SS
3 SSSSSSS
2   SSS
1   SSS
  abcdefg

Wat's the "abcdefg" represent? Is that a part of the array itself? Why an 8 x 8 array for a 7 x 7 grid? Array indexes start at 0, not 1. Are you not using the 0 index, just 1 - 7? I'm not clear on what you are doing. Be that as it may, if the '\n' isn't to be stored and doesn't signify anything that you need to do, just read it in and ignore it.

if(board == '\n')
{
    continue; /* or however you want to ignore it */
}

Yea solved my on problem,it is an 8x8 array because the abcdefg as well at the 1-7 on the sides are manually entered into the array the user only inputs what the board would look like if anyone wants me to post the solution to my problem just let me know,but did similar to how everyone was saying except i used getline and stringsream

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.