Hello,
I'm trying to copy one 2d array in another. However, in the one that I'm copying into, I want to have spaces in between each letter and a space in between each row. Here is what I have so far, but it only prints out the first letter of each row:
Code:
#include <iostream>
#include <map>
#include <cmath>
using namespace std;
int main() {
// potential letters
char letters[] = {'a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char city[26][26] = {0}; // 2d array to hold input
string line; // for reading in strings
int row = 0; // keep track of number of rows
map<char,int> values; // map letters to number values
// initialize map
for (int i = 0; i < 26; i++)
values[letters[i]] = i;
// put input into 2d array
while ( getline(cin,line) ) {
for (int i = 0; i < line.length(); i++)
city[row][i] = line[i];
row++;
}
// 2d array to hold map of city
int row2 = (2*row)-1;
char cityMap[5][12] = {0};
for (int i = 0,l=0; l < row; i+=2,l++) {
for (int j = 0,k=0; j < 12; j+=2,k++) {
if (city[l][k] == 0) break;
cityMap[i][j] = city[l][k];
}
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 12; j++) {
if (cityMap[i][j] == 0) break;
cout << cityMap[i][j];
}
cout << endl;
}
}
I know that loop to fill up the cityMap array is ugly and that's where I'm doing something wrong, I think.
Here's some input and what the output should be:
a c a b a b
b c d e f g
d c b a b b
Currently, I am getting the following for output:
If someone could show me how to alter my code to make that output happen, that would be greatly appreciated.