+ 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?

2nd Mar 2021, 2:54 PM
David Holmes Ng'andu
David Holmes Ng'andu - avatar
3 ответов
+ 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.
2nd Mar 2021, 3:01 PM
Lothar
Lothar - avatar
+ 3
David Holmes Ng'andu I'll also use a string like Lothar
2nd Mar 2021, 3:04 PM
K.S.S. Karunarathne
K.S.S. Karunarathne - avatar
+ 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.
15th May 2021, 12:46 PM
Josh Greig
Josh Greig - avatar