When I try to use 2d array to print a greeting card, the output of the columns are not I want(it shifted).
Can someone teach me how to write the correct code to have the correct output?
Thank you so much.
I have modified the codes as below. I don't know why there will be a extra x inside the box. I want the box fixed when I type something inside the box. But now the last column of x will be shifted if something in the box.
#include <iostream>
#include<string>
using namespace std;
void main(){
string pattern[70][30];
for (int i = 0; i < 16; i++){
for (int j = 0; j < 71; j++){
//col
if ((i>0 && i < 15) && (j == 0 || j == 69)){
pattern[i][j] = "x";
cout << pattern[i][j];
}
//row
if ((i == 0 || i == 15) && j < 70){
pattern[i][j] = "x";
cout << pattern[i][j];
}
else
cout << " ";
if (j == 5)
{
if (i == 1)
pattern[i][j] = "Hello";
cout << pattern[i][j];
}
}cout << endl;
}
}
OK so what you have to do is modify the lines in which you have characters other than blank spaces.
You currently have one pattern for a normal row and a second pattern for a normal column.
So you need a new pattern for a row with text made up of
x + a spaces + text + b spaces + x
This means you will have a small calculation. You know the overall width of the line. The reast follows.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
x Hello x
x x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
When I try to modify the provided code, it said "error C2109: subscript requires array or pointer type" and I add these pointers. But it still not work and keep showing the same error message. How can I modify it :( ?
int *pointerR;
int *pointerC;
void drawPattern(char[X][Y], int r, int c, char){
pointerR = &r;
pointerC = &c;
}
....
void drawPattern(char aPattern,int , int , char aChar)
{
int r = *pointerR;
int c = *pointerC;
aPattern[r][c] = aChar;
}