Please help with "loops"

I need to create a C++ program that the user type letter and the program convert the letter into a telephone number including the hyphen.
Can anyone show me how to cast uppercase and lowercase alphabet letters to covert in number ( like the Get-loan will convert in some number )
It has to be short without case A, Case B... I think I gotta use static_cast to do that but i don't know how. Please help!!!

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
#include <iostream>
#include <string>
using namespace std;
int main()
{
    char startProgram;

    while(true) // This while(run = true) is incorrect, it will always be true, change to while(run) or while(true)
    {
        cout <<"\nEnter Y or y to convert phone number from letters to numbers \n(enter any other letter to quit)"<<endl;
        cin >> startProgram;

		cin.ignore(10000,'\n');

        if(startProgram=='Y'|| startProgram=='y')
        {
            cout << "\nEnter the letters you would like your phone number to represent: "<<endl;
         
			getline(cin, letters);

            cout << "\nThe corresponding number is: ";

			// No need for these operations:
            //length = letters;
            //maxLength = length.length();


			// This doesnt make sense:
			//if(maxLength > 8)
            //{
                //maxLength = 7;
            //}

			for(unsigned int i=0; i <= 7 ; i++) // You could just set it to <= 7 or < 8
			// There is a function from the string class that gives you the size letters.length()
            {
				// Put this if BEFORE the switch statement, if its AFTER, 
				// it will first do the operations on the switch and then check
			
                if(i==3)
                    cout<<'-';  // If you are only outputting a single character use single quotes

                switch(letters[i])
                {
                case 'A':
                case 'a':
                case 'B':
                case 'b':
                case 'C':
                case 'c':
                    cout << 2;
                    break;
                case 'D':
                case 'd':
                case 'E':
                case 'e':
                case 'F':
                case 'f':
                    cout << 3;
                    break;
                case 'G':
                case 'g':
                case 'H':
                case 'h':
                case 'I':
                case 'i':
                    cout << 4;
                    break;
                case 'J':
                case 'j':
                case 'K':
                case 'k':
                case 'L':
                case 'l':
                    cout << 5;
                    break;
                case 'M':
                case 'm':
                case 'N':
                case 'n':
                case 'O':
                case 'o':
                    cout << 6;
                    break;
                case 'P':
                case 'p':
                case 'Q':
                case 'q':
                case 'R':
                case 'r':
                case 'S':
                case 's':
                    cout << 7;
                    break;
                case 'T':
                case 't':
                case 'U':
                case 'u':
                case 'V':
                case 'v':
                    cout << 8;
                    break;
                case 'W':
                case 'w':
                case 'X':
                case 'x':
                case 'Y':
                case 'y':
                case 'Z':
                case 'z':
                    cout << 9;
                    break;

				// I need to remove all these cases and make one short program: '
                //case ' ':
                    //cout << " ";
                default:
					// Just do a break here
                    //cout <<"Now quitting..."<<endl;
                    break;
                }
			
            }
        }
        else 
			break;
    }
	cout << "Now quitting..." << endl;
    //system("pause");
    return 0;
}
Last edited on
First of all, you can use std::tolower to convert all letters to lowercase. Then, you can get the value you want in the following way:
1
2
3
4
int convert (char c)
{
        return (static_cast <int>(std::tolower (c)) - static_cast <int>('a')) / 3 + 1;
} 
thanks! I am trying it out without much success :(
Last edited on
Topic archived. No new replies allowed.