Do while program

I have a programming problem, I am running a program that corresponds with the digits on a telephone keypad. When I enter a valid char, it should give the result and loop until an invalid char is entered. I cannot get it to loop to work properly.

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

using namespace std;

int main()
{
    int digit;
    char letter, end;

    do{
		cout << "Enter a single letter, and I will tell you what the corresponding digit is on the telephone keypad." << endl;
		cin >> letter;
		switch(letter)
		{
		case 'A':
		case 'a':
		case 'B':
		case 'b':
		case 'C':
		case 'c':
			digit = 2;
			cout << "The digit " << digit << " corresponds to the letter " << letter << " on the keypad." << endl;
			break;
		case 'D':
		case 'd':
		case 'E':
		case 'e':
		case 'F':
		case 'f':
			digit = 3;
			cout << "The digit " << digit << " corresponds to the letter " << letter << " on the keypad." << endl;
			break;
		case 'G':
		case 'g':
		case 'H':
		case 'h':
		case 'I':
		case 'i':
			digit = 4;
			cout << "The digit " << digit << " corresponds to the letter " << letter << " on the keypad." << endl;
			break;
		case 'J':
		case 'j':
		case 'K':
		case 'k':
		case 'L':
		case 'l':
			digit = 5;
			cout << "The digit " << digit << " corresponds to the letter " << letter << " on the keypad." << endl;
			break;
		case 'M':
		case 'm':
		case 'N':
		case 'n':
		case 'O':
		case 'o':
			digit = 6;
			cout << "The digit " << digit << " corresponds to the letter " << letter << " on the keypad." << endl;
			break;
		case 'P':
		case 'p':
		case 'R':
		case 'r':
		case 'S':
		case 's':
			digit = 7;
			cout << "The digit " << digit << " corresponds to the letter " << letter << " on the keypad." << endl;
			break;
		case 'T':
		case 't':
		case 'U':
		case 'u':
		case 'V':
		case 'v':
			digit = 8;
			cout << "The digit " << digit << " corresponds to the letter " << letter << " on the keypad." << endl;
			break;
		case 'W':
		case 'w':
		case 'X':
		case 'x':
		case 'Y':
		case 'y':
			digit = 9;
			cout << "The digit " << digit << " corresponds to the letter " << letter << " on the keypad." << endl;
			break;
		default: 
			cout << "There is no digit on the telephone keypad that corresponds to " << letter << "." << endl;
			break;
		}
		cin >> end;
	}while( (end != 'Z') && (end != 'z') );

}
I don't get what you are asking, the code works the way you have it. After you put in a char it outputs the corresponding line then breaks to cin >> end; (line 91) and if you don't put in a 'z' or a 'Z' then it ends.

What did you want it to do or where is your issue with it?
When I input the letter A, It gives the results of
The digit 2 corresponds to the letter A on the keypad
. After that, it should prompt the question
Enter a single letter, and I will tell you what the corresponding digit is on the telephone keypad
. Instead, it doesn't show anything, unless I input a character and hit enter, then it will prompt the question. I don't think it works correctly because I shouldn't have to enter another character to the the question.
1
2
3
4
		cin >> end;
	}while( (end != 'Z') && (end != 'z') );

}


You're telling the program to ask for an input that it stores into variable "end"
before it loops back around.

Plus the statement (end != 'Z') && (end != 'z') will always be true seeing as end cannot be Z and z at the same time.

If you want the program to end when the user enters Z or z then you should remove line 91 cin >> end; and edit line 92 to the following:
}while( (letter != 'Z') || (letter != 'z') );

Also remove the declaration of "end" because it's pointless now.
That got it, thank you VERY much!! Marking this problem as solved.
Topic archived. No new replies allowed.