+ 2
What data type would you use to store a telephone number? And Why
I mean i do know i could use a string, but is there another way?
3 Answers
+ 10
David Holmes Ng'andu ; you should use a string. because phone numbers can also contain spaces, leading zeros or characters like '+'. it is the same issue with postal codes. so everything that stores 'numbers' that are not meant to calculate with should be in string format.
+ 3
David Holmes Ng'andu I'll also use a string like Lothar
+ 1
In c and not c++, a null-terminated character array would be good. The string class doesn't exist in c.
In c++, an instance of the string class would be good.
There isn't a better way because phone numbers can have many formats. A phone number can be better understood if they include a +, hyphens, curved brackets, and spaces entered by the user. Consider the wide variety of phone number formats from including country codes, area codes, local codes, extensions... If you want a more deeply structured storage, you'll either need to heavily restrict the supported phone number formats or you'll need to make a data structure as complex as the wide variety of phone number formats that exist.
These are the same problems you'd have for strictly validating phone numbers. Requiring at least 7 digits is generally reasonable if you don't want to support emergency phone numbers like 999 or 911 but rules around hyphens, brackets... get very complex.