Can anyone right a C++ program that does this? To make telephone numbers easier to remember, some companies use letters to show their telephone number. For example, using letters, the telephone number 438-5626 can be show as GET LOAN. In some cases, to make a telephone number meaningful, companies might use more than seven letters. For example, 225-5466 can be displayed as CALL HOME, which uses eight letters. Write a program that prompts the user to enter a telephone number expressed in letters and outputs the corresponding telephone number in digits. If the user enters more than 7 letters, then process only the first seven letters. Also output the –(hyphen) after the third digit. Allow the user to use both uppercase and lowercase letters as well as spaces between words. Moreover, your program should process as many telephone numbers, as the user wants.

Her is what i have.

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


int letters, numbers;
char charArray [8];
//char a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z;
//char A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z;

int main()
{
    
    cout << "Please enter enter a telephone number expressed in letters: " << endl;
    cin >> letters;
            
 
    if (letters == 'a')                                          //and this determens the character value        
        {  
            return 2;
        }
    if (letters == 'd'||letters == 'e'||letters =='f'||letters == 'D'||letters == 'E'||letters == 'F')                                            //if it does not go in array 1 it will be placed in array 2
        {
             cout << "3" << endl;
        }    

return (0);
}

i have tried it 2 different ways as u can see with the letter a and the letter d.

This is the real one the otherone is not the one. look at this one.

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


int letters, numbers;
char charArray [8];
//char a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z;
//char A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z;

int main()
{

    cout << "Please enter enter a telephone number expressed in letters: " << endl;
    cin >> letters;


    if (letters == 'a')                                          
        {  
            return 2;
        }
    if (letters == 'd'||letters == 'e'||letters =='f'||letters == 'D'||letters == 'E'||letters == 'F')

        {
             cout << "3" << endl;
        }    

return (0);
}

Edited 3 Years Ago by happygeek: fixed formatting

the main problem i am having is getting it to print out the number value after i compile it. it just ends the program after i put in the letters.

Please use code tags. There's information in my signature.

I'd suggest for converting the chars to numbers that you create some sort of array. Something like:

int array['A'-'Z'] = {1, 1, 1, 2, 2, 2 // etc

As you see here, I'm using ASCII amounts. I suggest you read up on the ASCII table. So basically that creates an array large enough to hold 26 characters.

Then you basically look them up in the array like this:

number = array[aLetter-'A'];

Hope that gave you the basic concept.

As for the code you posted, you'll need to use a whole string for input, not just an int. ;) Perhaps something like:

char phoneNumber[128];
cin >> phoneNumber;

Then loop through each character with a pointer. Hope that works for ya. :)

can u look at this and tell me what u see< if u run it u will set the problem i am having.

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


int letters;
char phoneNumber[8];


int main()
{
    //This gets the user to input a list of letters, GET Loan for example and the program will convert that to numbers latter on.
    cout << "Please enter enter a telephone number expressed in letters: " << endl;
    cin >> phoneNumber;
            
    
    if (letters == 'a'||letters == 'b'||letters =='c'||letters == 'A'||letters == 'B'||letters == 'C')        
        {  
            cout << "2" << endl;
        }
    if (letters == 'd'||letters == 'e'||letters =='f'||letters == 'D'||letters == 'E'||letters == 'F')
        {
             cout << "3" << endl;
        }
    if (letters == 'g'||letters == 'h'||letters =='i'||letters == 'G'||letters == 'H'||letters == 'I')
        {
             cout << "4" << endl;
        }
        if (letters == 'j'||letters == 'k'||letters =='l'||letters == 'J'||letters == 'K'||letters == 'L')       
        {  
            cout << "5" << endl;
        }
    if (letters == 'm'||letters == 'n'||letters =='o'||letters == 'M'||letters == 'N'||letters == 'O')
        {
             cout << "6" << endl;
        }
        if (letters == 'p'||letters == 'q'||letters =='r'||letters == 's'||letters == 'P'||letters == 'Q'||letters == 'R'||letters == 'S')        
        {  
            cout << "7" << endl;
        }
    if (letters == 't'||letters == 'u'||letters =='v'||letters == 'T'||letters == 'U'||letters == 'V')    
        {
             cout << "8" << endl;
        }
    if (letters == 'w'||letters == 'x'||letters =='y'||letters == 'z'||letters == 'W'||letters == 'X'||letters == 'Y'||letters == 'Z')        
        {  
            cout << "9" << endl;
        }
            return (0);
}

USE CODE TAGS

The reason that your code never prints out anything is because you aren't looping or anything. Look closely: do you see any point in your code at which you assign letters ?

What would be best is if you used a pointer; eg something like this:

char myString[] = "PHONE"; // this is an example only
char *charPtr;

charPtr = myString; // make charPtr point to the first letter of myString

if (*charPtr == 'P') // since the first letter of myString is 'P', this expression is true
    cout << "P is here.\n";

charPtr++; // move to next letter (now would point to 'H')

Do you get the idea? Basically create a loop. Make charPtr point to each letter, compare it like I showed in this example, and then print out the appropriate number. Increment it with the ++ operator until the expression (!charPtr) is true (which means you've reached the end of the string).

[edit]Actually scratch that. Don't know what I was thinking, but it's actually just as easy simply to loop through the array like this:

for (int i=0; i<strlen(phoneNumber); i++) {
   // now do your comparisons with phoneNumber[i] like this:

   if (phoneNumber[i] == 'A') // ....
}

Sorry about that. Don't know what I was thinking. I guess I'm pointer happy. ;)

ok thank yall, I dont have a compiler at home so i will try it in the morning. I might have a few more ?'s so i will post them tomorrow morning, thanks again.

This article has been dead for over six months. Start a new discussion instead.