Help Please!

hi everybody. Todays question from me is how do you use arrays to get a string ?
i now about arrays and how to use them, but what about using them with strings.
The simplistic answer is that a 'string' is a dynamic array of chars. Or do you want to get an array of strings?
well i want to get an array of char's which is easy but i want to display them as a string so an array of my name {c, h, r, i, s, t, o, p, h, e, r} them output that as christopher.
Computergeek01: Why are strings dynamic?

PracticingProgrammer: {'c', 'h', 'r', 'i', 's', 't', 'o', 'p', 'h', 'e', 'r', 0}, or just "christopher".
@ Veltas: Because they are meant to be able to change their length during runtime with minimal effort. It's a loose definition but one that I stick with.

@ OP: Maybe something like this would work for you? http://www.cplusplus.com/reference/string/string/assign/
Last edited on
well basically in a program that may ask you to spell your name but it stores it in a char variable that is an array so, char name[11]; and then i would want to output that name back to you like cout << name[11]; but that would display individual characters so how would you display the whole array without writing all of the parts of the array.
sorry for being a bit un-clear
@ OP: What's wrong with this: std::cout << name;?

EDIT: To clarify:
1
2
3
    char name[] = {'c', 'h', 'r', 'i', 's', 't', 'o', 'p', 'h', 'e', 'r', 0};

    std::cout << name;
Last edited on
Thank you all. you have all answered my question! :)
He said cout, don't tell him std::cout without explaining it; he's new. He'll likely think it's a different thing.

If you want to stick with Dynamic, that's fine, but don't you think you should avoid giving newbies the wrong information?
@Veltas Computergeek01 wasn't implying to use 'std::cout' instead of 'cout'. He was saying to use 'name' instead of 'name[11]'
Maybe I should be more careful with what I type to avoid initial confusion, but in this instance nothing that he may have picked up from me will be harmful. He has a LONG way to go and trying to explain namespaces to the OP at this point WOULD be harmful to their development.
@GodPyro, I know, but the newbie doesn't; perhaps you can see my concern.

@Computergeek01 I didn't suggest to explain namespaces, I just suggested that maybe leaving std:: out would be safer since he mentioned cout before and so he's probably adding using namespace std; to the top of all his files right now. Saying std::cout rather than cout might make a newbie think you're using something different.
Topic archived. No new replies allowed.