Anyone know why this darn phone # program wont compile??

I'm tryin to get this phone number format program to work like so: (xxx)xxx-xxxx but for some reason its not compiling the way i want it to..any suggestions..??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
using namespace std;

int main()
{
   // declarations
   string phone       = "";
   string currentChar = "";
   int numChars       = 0;
   int subscript      = 0;

   // statements
   //get phone number
   cout << "Enter a phone number "
        << "in this format (xxx) xxx-xxxx: ";
   getline(cin, phone);

   //determine number of characters
   numChars = phone.length();

   //remove parentheses, hyphens, and spaces
   while (subscript < numChars)
   {
      // STUDENT CODE BEGINS


      cout << "(" << phone_number.substr(0,3) << ")"
     << phone.length(3,3) << "-" << phone.length(6,4) << endl;





      // STUDENT CODE ENDS
   }  //end while

   //display phone number
   cout << "Phone number without "
        << "parentheses, hyphens, or spaces: "
        << phone << endl;

   cin.get();
   return 0;
}  //end of main function 
When a compile fails... the compiler spits out these handy dandy error messages.

Not only do they tell you exactly what the problem is, but also tell you what line it's on.

How about copy/pasting those error messages in here so we can explain to you what they mean?
In any case, it seems like you're supposed to actually erase the characters from the string, perhaps using std::string::erase:
http://www.cplusplus.com/reference/string/string/erase/
wouldnt it be an insert though since im trying to add the () in the area code and the hyphen to seperate the digits..?
1
2
3
4
5
6
7
8
	5	IntelliSense: too many arguments in function call	c:\users\rico\desktop\cis276\class projects\ex14_10phonenumber.cpp	38	35	2
	6	IntelliSense: too many arguments in function call	c:\users\rico\desktop\cis276\class projects\ex14_10phonenumber.cpp	39	22	2
	7	IntelliSense: too many arguments in function call	c:\users\rico\desktop\cis276\class projects\ex14_10phonenumber.cpp	39	50	2
Error	1	error C2065: 'phone_number' : undeclared identifier	c:\users\rico\desktop\cis276\class projects\ex14_10phonenumber.cpp	40	1	2
Error	2	error C2660: 'std::basic_string<_Elem,_Traits,_Ax>::length' : function does not take 2 arguments	c:\users\rico\desktop\cis276\class projects\ex14_10phonenumber.cpp	43	1	2
Error	3	error C2660: 'std::basic_string<_Elem,_Traits,_Ax>::length' : function does not take 2 arguments	c:\users\rico\desktop\cis276\class projects\ex14_10phonenumber.cpp	44	1	2
Error	4	error C2660: 'std::basic_string<_Elem,_Traits,_Ax>::length' : function does not take 2 arguments	c:\users\rico\desktop\cis276\class projects\ex14_10phonenumber.cpp	44	1	2

those are my errors but even those dont make a whole lot of sense at times..
Im down to 4 errors now with this code...someone throw me a bone here...
1
2
3
 cout << "(" << phone.substr(0,3) << ")"
     << phone.length(3,3) << "-" << phone.length(6,4) << endl;
cout << "(" << phone.substr(0,3) << ")"
<< phone.length(3,3) << "-" << phone.erase(6,4) << endl;

im down to 3 errors now when i changed it to this..anything..?
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)
cout << "(" << phone.substr(0,3) << ")"
<< phone.substr(3,3) << "-" << phone.erase(6,4) << endl;

this is the last thing I changed it too and still was ending up with just 2 errors left now
but there still that dumb c2660 error, I read what you posted Disch but I still thing I'm missing the concept of what I have to do to fix this little altercation
wow im down to 1 error now after this correction:

cout << "(" << phone.erase(0,3) << ")"
<< phone.erase(3,3) << "-" << phone.erase(6,4) << endl;

and there saying its a LINK error1168...whatever that is..??
@CRooky. Your code compiles successfully, with the change you mentioned above, by g++ 4.7.0 compiler. But the while loop needs to be fixed. Currently it is an infinite loop.
thanks for the input kannanmj but I have no idea how to fix that..?? any ideas..??
Assuming the input is always given in the correct format (xxx) xxx-xxxx the following code is sufficient to get the expected output:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

int main()
{
   std::string phone;
   std::cout << "Enter a phone number in this format (xxx) xxx-xxxx: ";
   getline(std::cin, phone);
   std::cout << "Phone number without \"(\", \"-\" and blank character: "
     << phone.substr(1,3) << phone.substr(6,3) << phone.substr(10,4) << std::endl;
   return 0;
}

Topic archived. No new replies allowed.