+ 4

Which is best data type to store user name in C++? string or character(char)...

20th Sep 2017, 7:21 PM
Zohaib 👑
Zohaib 👑 - avatar
10 Answers
+ 7
use Strings
20th Sep 2017, 8:17 PM
David Akhihiero
David Akhihiero - avatar
20th Sep 2017, 10:56 PM
Hatsy Rei
Hatsy Rei - avatar
+ 6
I think is better string
20th Sep 2017, 7:23 PM
Daniel
Daniel - avatar
+ 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.
20th Sep 2017, 8:29 PM
John Wells
John Wells - avatar
+ 4
We have char array which can store multiple characters... @ReimarPB
20th Sep 2017, 7:34 PM
Zohaib 👑
Zohaib 👑 - avatar
+ 3
String is better. Char can only hold one character.
20th Sep 2017, 7:29 PM
ReimarPB
ReimarPB - avatar
+ 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
20th Sep 2017, 7:44 PM
Zohaib 👑
Zohaib 👑 - avatar
+ 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.
21st Sep 2017, 1:29 AM
Jared Bird
Jared Bird - avatar
+ 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.
20th Sep 2017, 7:37 PM
ReimarPB
ReimarPB - avatar
+ 1
std::string and std::wstring, included in standard library
28th Mar 2018, 8:26 AM
acedj