The numbers are only 7 digits plus a hyphen in the 4th position. any ideas would be great. Here's the first section of the program i already wrote but it only takes one digit at a time.
#include %26lt;iostream%26gt;
using namespace std;
void main()
{
char letter, number;
cout %26lt;%26lt; "Enter a letter\n";
cin %26gt;%26gt; letter;
cout %26lt;%26lt; endl;
letter = toupper (letter);
switch(letter)
{
case '0':
number = '0';
cout %26lt;%26lt; number;
cout %26lt;%26lt; endl;
break;
case '1':
number = '1';
cout %26lt;%26lt; number;
cout %26lt;%26lt; endl;
break;
case 'A': case 'B': case 'C': case '2':
number = '2';
cout %26lt;%26lt; number;
cout %26lt;%26lt; endl;
break;
I need to modify my C++ program to take a full alpha-numeric phone number and convert the letters to numbers.?
You have the right idea. Now all you need to do is to capture the full string instead of a character at a time. Then walk through the string one character at a time and convert. Take the code you wrote and put it into a function. Then create a loop and call the function multiple times. As you walk through the input characters, build an output string. When the string is built, just output the whole thing.
void append(char *s, char c)
{
int len = strlen(s);
s[len++] = c;
s[len] = '\0';
}
char * convertPhoneString(char * input)
{
char * output = new char[200];
output[0] = '\0';
for(int i=0;i%26lt;strlen(input);i++)
{
if ( input[i] %26gt;= '0' %26amp;%26amp; input[i] %26lt;= '9' )
append(output, input[i]);
else if ( (input[i] %26gt;= 'A' %26amp;%26amp; input[i] %26lt;= 'C' ) ||
(input[i] %26gt;= 'a' %26amp;%26amp; input[i] %26lt;= 'a' ) )
append(output, '2');
... etc
}
return output;
}
Reply:Hmm... while a switch/case structure would seem to be the obvious choice for this, there is something that'll work a little better: an if/then logic structure.
C/C++ has the unique ability to take a character and treat it like a numeric value, in particular it's ASCII value. For example, the letter 'A' has the ASCII value 65, but you don't need to use that in your structure.
So instead of using a switch/case structure, change it to if and else if statements. I'll start it off for you:
if(letter == '0' || letter == '1') number = letter;
else if(letter %26gt;= 'A' %26amp;%26amp; letter %26lt;= 'C') number = '2';
else if....
And you can fill in the rest. You'll find your program simplified quite a bit using this method, and probably easier to read and understand. At the end of the if/else if statements is where you'd put the output statements.
Hope this helps.
Reply:Not sure how detailed this must be. But here is simplest way to code the letters to numbers. You can completely eliminate the switches, IFs, etc.
#include %26lt;iostream%26gt;
using namespace std;
int main(int argc, char *argv[])
{
char letterToNumbers[] = {'2', '2', '2', //letters a,b,c
'3', '3', '3', //letters d,e,f
'4', '4', '4', //letters g,h,i
'5', '5', '5', //letters j,k,l
'6', '6', '6', //letters m,n,o
'7', '7', '7', '7', //letters p,q,r,s
'8', '8', '8', //letters t,u,v
'9', '9', '9', '9' //letters w,x,y,z
};
char letter;
cout %26lt;%26lt; "Enter a letter\n";
cin %26gt;%26gt; letter;
letter = toupper(letter) - 'A';
cout %26lt;%26lt; letterToNumbers[letter] %26lt;%26lt; endl;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment