Need help with Phone Number Program

Hi! I'm taking an intro to C++ class. My professor has assigned a problem where you have to take a phone number with words in it, and convert it to just digits. I have no idea where to even start with this. I'm not asking for a full, complete program from any one, but a little help to get me started would be appreciated.

The problem is stated thusly: "You want to build a translator that returns just digits. Your program should be able to handle a list of numbers from a file. It should be able to handle a line with any combination of numbers and upper or lower case letters and hyphens. If you get more than what a phone number requires, ignore the remainder. There may or may not be a leading '1'.

"Your program should be efficient and use a switch statement in a logical manner. Do not use arrays[we haven't even covered arrays yet]. Do not use string indexing (i.e. Str1[2]) [again, I don't know what this is]. Note that the output should have both the inputted data and the phone number."

An example of some output he gave is:
"1-888-DOG-BITE -> 1-888-364-2483
FAST-CASH-NOW -> 327-827-4669"

I was thinking of just using a switch to translate each character separately , but I don't get how I would output the original number with the letters first, like above, and then translate it and output. Any insight on this would be very much appreciated!
Last edited on
hi, I also just started to learn cpp, I would suggest the following, but it really depends on the input, as you need to parse it. In case, that every phone number is leaded by a beginning line or a space:
i.e.
Dog Bite 1-88-DOG-BITE
FAST-CASH-NOW
You can easily use RegEx, by matching beginning of line or space until end of line or the next space.
For replacing a A, B, C with 2 etc. you also can use RegEx.

So, for the procedure I would suggest to parse line by line with RegEx, identify the phone number and replace the letters.

That's how I would solve that problem. Hope that helps.
Not quite sure how to use regex \:
looking up examples and tutorials as we speek. Thanks for the input! Not sure if my professor will like me using it (he doesn't like us using stuff we haven't covered in class), but it seems like a useful thing to know.
As far as I know RegEx is the most common and fastest way to process text - so indeed, it is useful to know.
If your professor doesn't want you to use uncovered stuff, I have to ask: What topics did your class already cover? So, maybe we can find a solution that fits the idea of your professor.
Last edited on
We haven't really covered much, compared to what some of the guys on this website talk about! We've done basic i/o and output formatting, reading and writing to files, control structures (including loops) and we've just recently covered functions.
Hmmmm, check out the replace method of a string object. Maybe that fits your professors perception...
Bad thing about that, depending on the input that could end in a confusing If "spamming" and looping :(
It's problems like these that make me realize how much of an amateur I am.. I was able to get by with the .at() function xD I don't know why it wasn't obvious to use string functions...
Thanks for your help anyways! I'll look more into this regex stuff sometime, because it seems handy.
Last edited on
closed account (E0M1hbRD)
Might this help? It's just a test conversion engine. You can call the data from a file, parse it, store the raw phone numbers in an int array and then just convert. Might even drop this into a function and call it as needed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	int A(2), B(2), C(2);
	int D(3), E(3), F(3);
	int G(4), H(4), I(4);
	int J(5), K(5), L(5);
	int M(6), N(6), O(6);
	int P(7), Q(7), R(7), S(7);
	int T(8), U(8), V(8);
	int W(9), X(9), Y(9), Z(9);

	int i;

	int test[] = {1,8,8,8,D,O,G,B,I,T,E};
	int test2[] = {F,A,S,T,C,A,S,H,N,O,W};
	for(i = 0; i != 11; ++i) std::cout << test[i] << " ";
	cout << endl;
	for(i = 0; i != 11; ++i) std::cout << test2[i] << " ";


Output is 1 8 8 8 3 6 4 2 4 8 3
3 2 7 8 2 2 7 4 6 6 9
Last edited on
@ToeKnee0x10:
As far as I know your idea won't fit with c++ and dynamically reading in unknown data, as c++ does not get interpreted during runtime.
What your idea seems to me, is that it looks like reflection, in case you extend it a little bit. This should be possible in java and other program languages that get interpreted during runtime.
MAYBE this is possible with c++, too, I would think of some runtime compiling api, but it seems very dowdy to me.

@All: Please correct me if I am wrong with the above statement.

@Keppit: I would suggest you stick to the simple string manipulation stuff for doing the correct thing for your prof.
And also I suggest you to learn RegEx for easy and fast parsing. An other very good thing about RegEx is, that the idea of RegEx is language independent, just the syntax, how to use it differs slightly.
closed account (E0M1hbRD)
@cppbegin. Hi. I wrote an entire program in C++ according to his assignment in about 15 minutes and it worked just fine. I only posted clips of it to give the OP ideas. The only problem I see, now that I went back and looked at his instructions, is that he is not allowed to use arrays (his class hasn't learned them yet) so my code would not be accepted by his teacher. His teacher thinks using a switch/case is efficient for this? Oh well...

To the OP: How about using a switch/case first to parse incoming data from the file and then another one as a translator. Not exactly elegent but should do the trick.
Topic archived. No new replies allowed.