In C++, I'm trying to create string array. And also I want to access chars and change them like this:
#include<iostream>
using namespace std;
int main(){
int n;
cin >> n;
char lines[4][n+1];
int color = 0;
for(int a = 0; a < n; a++){
for(int i = 0; i < 4; i++){
lines[i][a] = (char) (color % 25 + 97);
if(i == 1 || i == 3){
color++;
}
}
}
cout << lines[0] << endl << lines[1] << endl << lines[2] << endl << lines[3];
return 0;
}
When i want it to print all of "lines[i]" there appears some unknown characters.
Expected:
aceg
aceg
bdfh
bdfh
Output:(https://i.sstatic.net/SR4Y4.png)
aceg'aceg■bdfh
aceg■bdfh
bdfh
bdfh
Do you know the reason? (I checked my whole code many times, it causes something i don't know about array of char arrays I thought)
Are there any other ways to do this? (If possible without libraries like its in C)
Edit
I added all of my code to this message.
color, you should be getting the same value ('a') in every cell. When you omit key parts of the code, people become frustrated — it is hard to help you when code is missing. Please read up on how to create an SSCCE (Short, Self-Contained, Correct Example). What's the value inn?nis a constant expression, I'm not sure it's valid C++...