+ 4
Which is best data type to store user name in C++? string or character(char)...
10 Answers
+ 7
use Strings
+ 6
I think is better string
+ 6
You can use a char array to store your name, but you either need to limit the length of the array or implement the same code that is provided by string. Making a fixed array length will use more storage because all names have to account for whatever maximum you pick. Strings only use the storage needed for each name as it is allocated once the size of the name is known.
@ReimarPB you can read individual characters to put into the char array.
+ 4
We have char array which can store multiple characters... @ReimarPB
+ 3
String is better. Char can only hold one character.
+ 3
No why user have to put a space between characters... did you ever work with char array especially to store names etc... one thing more mostly passowrds, emails are store in char so what about that... @ReimarPB
+ 2
When using any modern c++ compiler you may as well use string. It's easier and safer with no real down sidesâ.
The only reasonsâ I would use a chat array are:
If you know the size, and it will not change, eg to store a checksum.
If want the code to be compatible with c. ie a function can be used in c program and c++ program and you only want to write it once.
Lastly if you are reading and writing data to and from files using fixed lengths is easier.
You have to very careful doing this! There are much better ways of doing this. So this point is only half valid, probably would only use for a personal project.
+ 1
@Zohaib Tariq Yeah, but thats not necessary unless you want to store every character seperately. Also, if you use user input, the user will have to put a space between all the characters.
+ 1
std::string and std::wstring, included in standard library