Hello frza45,
In everyone's attempt to find the most advanced answer to your problem I believe that they have missed the most fundamental part.
Each numerical variable type has a finite number it can store.
An example is an "int" which is generally four bytes can store a number between (
-2,147,483,648 to
2,147,483,647) and the unsigned version is (
0 to 4,294,967,295).
Compared to a phone number of (9995551234) or (
9,995,551,234). You can see that the phone number is larger than an "int" can store.
Most often an "unsigned long long" is used to store a phone number. This can store a number up to (18,446,744,073,709,551,615) which is large enough to store a phone number as a numeric variable.
For what you want to do it would be simpler to store the phone number as a string to begin with. Then you can manipulate it any way you like.
These are some links I found that will in some way help you understand the size limits of different variable types. Not exactly what I was looking for, but useful:
https://www.geeksforgeeks.org/c-data-types/
http://www.cplusplus.com/reference/limits/numeric_limits/
http://www.cplusplus.com/doc/tutorial/variables/
All information is good some just not easy to understand at first.
To answer your question. Start with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
// in the class.
unsigned long long phoneNum;
// Or as KishoreG has shown.
unsigned long long m_phoneNum;
// Change your "get" and "set" functions to return an "unsigned long long" and set the same.
// In "main".
unsigned long long userPhone;
std::string sUserPhone; // <--- Added.
// On line 54 change the "getline" to:
std::cin >> userPhone; // <--- Changed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
// Once the phone number is in a numeric variable you can use:
sUserPhone = std::to_string(userPhone);
// Or
sUserPhone = std::to_string(info.getPhone());
| |
When mixing the unformatted input of "getline" and formatted input of "stdcin >> userPhone;". The formatted input will leave the new line in the input buffer. The "std::cin.ignore()" will clear the input buffer before the next use of "getline". Otherwise the next "getline" will take the new line from the input buffer and move on never giving you the chance to enter anything. Two things about "std::getline". As mentioned earlier "getline" only works with a "std::string" as the second parameter and "getline" will eat the new line and discard it leaving an empty input buffer.
The "ignore" statement shown is the more portable version that any operating system and set of header files can use as some setups are different in the sizes. If it is possible, follow the last formatted input with the "ignore" function.
As the bits of code show there are two ways to use "std::to_string" to convert the numeric variable into a string. Exactly where to use it will depend on what you want to do where. The variable "sUserPhone" was a quick way to use the same basic variable name where the "s" reminds me that it is defined as a string. You are free to choose another name if you like.
For
KishoreG's code there are many parts that are worth taking note of.
The way he/she, sorry not sure which, defined the "classs" is good. I like the use of the overloaded ctor and breaking up the class definition and function definitions into separate parts. This is a good practice to get use to because some day you will be putting the "class" definition in a header file and the functions in a ".cpp" file.
I have read several tutorials that suggest using the "m_" as a prefix to the "private" variable names. When using the overloaded ctor or a set function the parameter can use
std::string name
and inside the function you would use
m_name = name;
. Just a concept to keep in mind.
In "main" a few blank lines would be helpful, but answer all the questions then create an object of the class using the overloaded ctor. This has potential when you put most of "main" in a loop.
A suggestion. You may find this of more use than what you have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
int main()
{
Personal info;
string userName, userAddress;
int userAge;
unsigned long long userPhone;
cout << "\n Enter your name (First last): "; // Or "\n Enter your name (Last, First): ".
getline(cin, userName);
cout << "\n Enter your Address (Number street, City, State, Zipcode): ";
getline(cin, userAddress);
cout << "\n Enter your age: ";
cin >> userAge;
cout << "\n Enter your Phone # (2025551234): ";
cin >> userPhone;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
| |
Combining more than one variable on a single line is acceptable, but you can define each variable on its own line if you want. Different types require their own line.
Beginning each "cout" with the "\n ", and this may be just a personal thing with me, I find that one line off the top edge and one character in makes it easier to read.
I find that the parts in ()s help the user to know what to enter or how you want the information entered.
For the name (Last, First) can be useful in the future if you need to break up the full name into two parts. The comma can be used as a separator should either name have a space in it.
I have also found that ending the prompt with ": " and no "endl" will put the input on the same line. Most of the time this works unless the prompt is long and the input is long. Then you might want to break it into two lines.
After that a few blank lines makes the code easier to read and that is an important point both for you and others.
Hope that helps,
Andy