1

I want the user to input just one character, say 'Y', then I want Y to be stored in each element of an array (ie: array[9]) so that when I print it would be like 'YYYYYYYYY', or when it's a 2d array (ie: array[2][2]) it would look like:

YYY
YYY
YYY   
1
  • 1
    Storing and printing are two different problems; so which one are you having problem with and what did you try to solve it? Commented Feb 23, 2013 at 9:20

2 Answers 2

1

If you need to print each entered character same number of times, why to store all? it will waste memory, just store one character and it print any number of times needed, so a single array will be enough,i guess. But this solution is what i got from your question, if there are some constraints please do share or better share code...

Sign up to request clarification or add additional context in comments.

Comments

0

You can use memset() from <cstring>, or std::fill() from <algorithm>. So for a char array[9];, you can do:

memset(array, 'Y', 9);

or:

std::fill(array, array + 9, 'Y');

For a 2D array, you'd set each row individually.

Watch out when using memset(), because the length is given in bytes, not in elements. If you had an int array, for example, memset(array, 'Y', 9) wouldn't work. std::fill() does not have that problem.

1 Comment

I don't think this: memset(array, 'Y', 9 * sizeof(int)); is going to do what you may want. The byte value for an ASCII 'Y' is 0x59, for a system with 32-bit integers your int values will become 0x59595959, or 1499027801. You'd definitely want to use std::fill() as the correct alternative.

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.