Sorry if this is a bit of a silly question.
Here is my code:
#include<iostream>
using namespace std;
int main()
{
int columns, rows;
char **map;
cin>>columns;
cin>>rows;
/*creats array of pointers rows tall*/
map = new char*[rows];
/*creats array of chars columns tall*/
for(int i=0; i<rows; i++)
map[i] = new char[columns];
//populate map with input
map[0][0] = cin.get();
for(int j=0; j<rows; j++)
for(int i=0; i<columns; i++)
{
if (cin.peek() == '\n')
cin.ignore(256, '\n');
else
map[j][i] = cin.get();
}
//DISPLAY
cout<<endl;
for(int j=0; j<rows; j++)
{
for(int i=0; i<columns; i++)
{
cout<<map[j][i];
}
}
return 0;
}
The user will input something like:
7 4
#######
#S# #
# #E#
#######
and I would like to output it. However mine comes out like:
#######
#S#
## #
E#####
Any thoughts?
First for loop:
And add the new line to output:
Always make sure you get the trailing enter out of the input stream before reading it again.