Phone Number Program

Hello, I'm Ryoga - a student trying to cram intros to Java and C++ into a single summer session... For a project, I'm trying to write a program that reads a ten digit phone number made of characters (eg. 800-buy-cars) into a string and then spits out the literal number (800-289-2277).

I've tried a lot of different things. At one point I even had a loop going to read through the array character by character, but now, for simplicity's sake, I have seven switches - one for each of the characters I wish to translate.

I included the declaration of and filling of the input string as well as the first switch below. (the full program is too long to post) Below that, I also posted the cout that's supposed to display the translated numbers to the console and the end of the program.

Right now the program runs, but it spits out the same ridiculous number every time, meaning I've done something dumb. If you have the time, could you please skim through it and tell me if you see the dumb things causing my grief?


~~~


string phoneChar;
getline(cin, phoneChar);
cout << endl;

if ((phoneChar.size() > 10) || (phoneChar.size() < 10))
cout << "Invalid Length" << endl;

int phoneNum[10];
phoneChar[0] = phoneNum[0];
phoneChar[1] = phoneNum[1];
phoneChar[2] = phoneNum[2];

switch (phoneChar[3])
{
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
phoneNum[3] = 2;
break;

case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
phoneNum[3] = 3;
break;

case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
phoneNum[3] = 4;
break;

case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
phoneNum[3] = 5;
break;

case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
phoneNum[3] = 6;
break;

case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
phoneNum[3] = 7;
break;

case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
phoneNum[3] = 8;
break;

case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
phoneNum[3] = 9;
break;
default:
cout << "Invalid Character" << endl;
}


~~~


cout << "The translated number is: " << phoneNum[10] << endl;


system("pause");
return 0;
}


~~~


This should be a fairly simple program... What am I doing wrong? Thank you in advance.
Hmmm... I feel like I've seen this before.
http://www.cplusplus.com/forum/beginner/45672/

Anyway, I'm not quite sure what it is, but like I said in the other one I think switch cases are supposed to have a break for each one. Please, correct me if I'm wrong.

EDIT: Wait! I see what you're doing! Nevermind.

1
2
3
phoneChar[0] = phoneNum[0];
phoneChar[1] = phoneNum[1];
phoneChar[2] = phoneNum[2];

You're trying to convert from characters to numbers, right?
Last edited on
Yes sir/ma'am! Can you offer any advice?
I wonder if that other user is in my class, haha - we're getting close to the due date. :)
Yup! :)

1
2
3
phoneChar[0] = phoneNum[0];
phoneChar[1] = phoneNum[1];
phoneChar[2] = phoneNum[2];


You're trying to convert from characters to numbers, right?


You need to set the phoneNum equal to the phoneChar, not the other way around. Just reverse those three lines.
Last edited on
He probably is. Lol.
Woops... Just saw the:
int phoneNum[10];
So, you need to make a for loop to display the entire phone number.
Alright, switched those things around as per your first suggestion.

So I need a for loop to display the number. Okay, thank you for the advice; I will work on that. But should the switch statements work the way I anticipated? Like, do I have the right idea so far you think?
Yeah. I think you have the right idea, althought at the moment you are only checking the value of the 4th in the string.
switch (phoneChar[3])

So the rest of your array is going to be empty. You need to cycle through the string to each value and test it. This means you will need a...? :)
Agh! Another for loop? lol
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <cmath>
#include <ostream>
using namespace std;

int main()
{

cout << "\t\t\tnameless" << endl;
cout << "Phone Key Pad - Convert Letters to Numbers" << endl;
cout << "------------------------------------------" << endl;
cout << "This program will convert a phone letter' to a number" << endl;
cout << "Enter a character string phone number (###.aaa.aaaa):" << endl;

string phoneChar;
getline(cin, phoneChar);

cout << endl;


char characters[12];
for (unsigned int i = 0; i < 12; i++)
	characters[i] = phoneChar[i];
double phoneNum;

int length = 12;
for(int i=0; i < length; i++)
{

	if (isalnum(characters[i]))
	{
		switch (characters[i])
		{
		case 'A':
		case 'a':
		case 'B':
		case 'b':
		case 'C':
		case 'c':
		case '2':
		phoneNum = 2;
		break;

		case 'D':
		case 'd':
		case 'E':
		case 'e':
		case 'F':
		case 'f':
		case '3':
		phoneNum = 3;
		break;

		case 'G':
		case 'g':
		case 'H':
		case 'h':
		case 'I':
		case 'i':
		case '4':
		phoneNum = 4;
		break;

		case 'J':
		case 'j':
		case 'K':
		case 'k':
		case 'L':
		case 'l':
		case '5':
		phoneNum = 5;
		break;

		case 'M':
		case 'm':
		case 'N':
		case 'n':
		case 'O':
		case 'o':
		case '6':
		phoneNum = 6;
		break;

		case 'P':
		case 'p':
		case 'Q':
		case 'q':
		case 'R':
		case 'r':
		case 'S':
		case 's':
		case '7':
		phoneNum = 7;
		break;

		case 'T':
		case 't':
		case 'U':
		case 'u':
		case 'V':
		case 'v':
		case '8':
		phoneNum = 8;
		break;

		case 'W':
		case 'w':
		case 'X':
		case 'x':
		case 'Y':
		case 'y':
		case 'Z':
		case 'z':
		case '9':
		phoneNum = 9;
		break;
		}
		cout << phoneNum << endl;
	}

	if(i == 2)
	{
		cout << "." << endl;
	}
	if(i == 6)
	{
		cout << "." << endl;
	}
}

system("pause");
return 0;
}


This guy is difficult. But this is my code so far. It outputs the correct 'translation' i.e. from 800.MIS.DEPT but it doesn't output it in the correct format. All that needs to be done is to take each output of the switch statement and store it as a string, and output the string.
Last edited on
@Ryogathelost: Yup! :D Add that for loop and in my head it is working smoothly, but that never turns out right. Lol. you will probably have some debugging to do.

@madbadger89: Who is difficult? Me? I'm only trying to help him learn. If I freely give out the answers he won't learn anything. Give a man a fish, feed him for a day. Teach a man to fish, feed him for a lifetime.
Oh, I was talking about the instructor, same class. Not you, you are awesome. Seriously, all the help you have given is awesome.
Okay. Lol. Just making sure. :) We cool.
@madbadger89: You already know how to fix yours right?

@Ryogathelost: Tell me how it goes.
I am having issues storing the results of the switch statement to a string and then 'out-putting' the string. The output would be easy, its just how to store after each switch statement that I cannot figure out.
Wait, can I use a C-string?
You could use a C-style string. Most certainly. That is how I would do it. :)
If you guys need anymore help you can email me. I will definitely help out. Be warned though. I will not give you straight answers. :P letslearncpp@gmail.com
Last edited on
A lot of switch statements is a terribly ugly way to go about this. You can define a range of characters with an if statement using the ASCII values of the characters. On an ASCII chart a is 97, and c is 99, you could just use an if statement
1
2
if(characters[i] >= 97 && characters[i] <= 99) 
	phoneNum = 1;

If you want to use capitals you can just add more conditions like so.
1
2
3
if((characters[i] >= 97 && characters[i] <= 100) 
		|| (characters[i] >= 65 && characters[i] <= 67)
	phoneNum = 1;

Also, you're probably going to be needing this: http://www.asciitable.com/
Last edited on
Topic archived. No new replies allowed.