0

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.

8
  • You need to understand what "want to print" entails. Commented Aug 1, 2013 at 22:52
  • 1
    Strings are null-terminated... Commented Aug 1, 2013 at 22:54
  • Welcome to Stack Overflow. Please read the About page soon. Please show how you are printing your arrays. Unless you've done something you've not shown, printing them as if they are strings will show the sort of behaviour you're seeing. Since you don't show any code changing 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 in n? Commented Aug 1, 2013 at 23:01
  • @KerrekSB: That code is also valid C++. Accusing the OP of lying is a bit much. Commented Aug 1, 2013 at 23:20
  • @KeithThompson: Without knowing whether n is a constant expression, I'm not sure it's valid C++... Commented Aug 1, 2013 at 23:22

3 Answers 3

2

I can't be sure without seeing exactly how you are printing, but if you're just printing via something like printf("%s\n", lines[i]);, then your problem is because you forgot to null-terminate your strings.

Make sure you do something like

for (int i = 0; i < 4; i++) {
    lines[i][n] = '\0';
}
Sign up to request clarification or add additional context in comments.

4 Comments

Pardon, I guess if you're using C++ then std::cout << lines[i];
This is probably right, since the OP has the conspicuous array size n + 1 in her code...
That, and the output that the OP is possible if lines[0][n] and lines[1][n] are not null, but lines[3][n] and lines[4][n] are.
Where can I check this problem as answered? (I already checked green tick.) Also it was that what I don't know. Thank you.
0

Works fine for me:

#include <stdio.h>

for(int i = 0; i != 4; ++i)
{
    fwrite(lines[i], 1, n, stdout);
    fputc('\n', stdout);
}

Or in C++:

#include <iostream>
#include <string>

for(int i = 0; i != 4; ++i)
{
    std::cout << std::string(lines[i], n) << '\n';
}

2 Comments

While that might be true, what if the OP's problem is that he's not printing the same way? This wouldn't help the OP understand why he sees the behavior he does.
@DennisMeng: By all means post an answer that explains the OP's problem :-)
0
aceg'aceg■bdfh

aceg■bdfh

bdfh

bdfh

See you output then you can find :

line[0] in fact print line[0] & line [1] & line[2].
line[1] in fact print line[1] & line [2]. 

That means you "string" didn't have the right stop flag.

use line[i][n] = '\0' Or char[i][4] = '\0' if you only store 4 char.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.