error C2065: 'phone_number' : undeclared identifier |
"undeclared identifier" means you are using a name (in this case, 'phone_number') and the compiler does not know what that is. As you already figured out... 'phone_number' was a mistake, and you just wanted 'phone', since that is the name of your string.
error C2660: 'std::basic_string<_Elem,_Traits,_Ax>::length' : function does not take 2 arguments |
std::basic_string<blahblah> is really just "string". The reason for the long name is how the string class is implemented.
So this error is telling you that string::length does not take 2 arguments.
The error is coming from here:
phone.length(3,3)
Remember that length just returns the length of the string. It doesn't take any parameters.
If you are intending to extract a portion of the string, you want substr.
You didn't post the error for this next one:
phone.erase(6,4)
Erase does not return a string.
It also does not take integer parameters. It takes iterators.
begin() will get you an iterator to the first element in the string:
1 2
|
phone.erase( phone.begin() ); // will erase the first element in the string
phone = phone.substr(1); // will have the same effect
|
I'm also not sure you are understanding the assignment correctly. The goal is not to print the number with parenthesis and dashes. The user has
input a number with parenthesis and dashes and your job is to
remove them.
This basically means you should be iterating over the 'phone' string and removing any character that is not a digit.
This can be done by creating a second string and building it one character at a time. Or it can be done by iterating over the string directly and removing characters with erase. The former is probably the simpler approach for a beginner (erase is a little confusing, as there are some nuances that are not immediately clear)