Trouble with a getline and if statement

My problem as the tittle suggest is that I am trying to make a telephone number program that outputs for example GET LOAN as 438-5626. So far my program works but it only as you can tell output a single digit from it's corresponding letter and another problem is that I need a hypen after the third digit and want to limit the program to allow me to enter lets say 30 letters but limit it to 7.

For the getline I was thinking getline(cin, letter) to limit it and im not sure if im on the right track or even where to begin with it.

For the if statement something like if (i == 3) cout << '-'; else cout << "error" << endl;

I have only been working with programming for about 2 and a half months so assume im not looking for something ahead of me even though it's nice to know about what im going to learn.

My work done so far hints are appreciated.


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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	// Remember to use getline(cin, letter) to limit.
	char letter;
	
	cout << "This program will convert upper and lower case letters into digits to a telephone " << endl;

	cout << "To end the program enter # "<< endl;
		
	cout << "Enter a letter: ";
	cin >> letter;
	cout << endl;
		
		
			while (letter != '#')
	
	{
		
		cout << "The letter you entered is: "
			<< letter << endl;

		
		cout << "The corresponding telephone "
			<< "digit is: ";
			
		
	
	
		if (letter >= 'A' && letter <= 'Z' || letter >='a' && letter <= 'z')
			
			switch(letter)	
			
					{		
		
		
		case 'A':
		case 'a':
		case 'B':
		case 'b':
		case 'C':
		case 'c':
			cout << 2 << endl;
				break;
		case 'D':
		case 'd':
		case 'E':
		case 'e':
		case 'F':
		case 'f':
			cout << 3 << endl;
			break;
		case 'G':
		case 'g':
		case 'H':
		case 'h':
		case 'I':
		case 'i':
			cout << 4 << endl;
				break;
		case 'J':
		case 'j':
		case 'K':
		case 'k':
		case 'L':
		case 'l':
			cout << 5 << endl;
				break;
		case 'M':
		case 'm':
		case 'N':
		case 'n':
		case 'O':
		case 'o':
			cout << 6 << endl;
				break;
		case 'P':
		case 'p':
		case 'Q':
		case 'q':
		case 'R':
		case 'r':
		case 'S':
		case 's':	
			cout << 7 << endl;
		case 'T':
		case 't':
		case 'U':
		case 'u':
		case 'V':
		case 'v':
			cout << 8 << endl;
				break;
		case 'W':
		case 'w':
		case 'X':
		case 'x':
		case 'Y':
		case 'y':
		case 'Z':
		case 'z':
			cout << 9 << endl;
				break;
		default:
			cout << "Finished processing ";
			}
		if ( letter == 3)
                     cout << '-';
                  else
                     cout << "error" << endl;
	
		
	cout << " Continue by pressing another letter." << endl;
	
	cout << "To stop the program remember to input #." << endl;

	cout << "Enter letter: ";
	cin >> letter;
	cout << endl;
	
	}

	return 0;
}
Last edited on
I figured that changing char to a string would help with my problem and maybe the if(letter >='A'&& letter<='Z'...) won't be required. Also the if statement seems to be taken care of by the default: statement in the case switch can anyone else give me advice on getting the hypen (-). Last thing since the while(letter != '#') letter was changed into a string I this can no longer work so should I just make a char named word or something?
Last edited on
Topic archived. No new replies allowed.