can you helpp me plz !

1. Write a C++ program that converts uppercase letters to their corresponding telephone digits . The program should reads the letter 'A' through 'Z' and print the corresponding telephone digit. To stop the program, the user is prompted for the sentinel. which is '#'.

Sample Run:

*****************
Telephone Digits
*****************
Program to convert uppercase letters to their corresponding telephone digits.
To stop the program enter #.
Enter a letter: S

The letter you entered is S
The corresponding telephone digit is :7

Enter another uppercase letter to find its corresponding telephone digit. To stop the program enter #.
Enter a letter: @

Invalid input

Enter another uppercase letter to find its corresponding telephone digit. To stop the program enter #.
Enter a letter: A

The letter you entered is A
The corresponding telephone digit is :2


Enter another uppercase letter to find its corresponding telephone digit. To stop the program enter #.
Enter a letter: #

Thank you .
First of all, you could use a bunch of if-statements.
For example:

char x;
cin>>x;

if(x=='A' | x=='B' | x=='C')
{
cout<<"The corresponding telephone digit is 2."<<endl;
}
Then you could do this for the rest.
You can also use switch cases but I think Adijunn is more efficient =). Good practice though.
@Adijunn (1)

if(x=='A' | x=='B' | x=='C')

should be:

if(x=='A' || x=='B' || x=='C')

Note the use of the OR operator.

I would prefer a switch - more elegant IMO, can cope with upper & lower case, or make use of std::toupper (need to #include <locale> ) before the switch.

HTH
Last edited on
Really, i thought or was just one |. At least that works for me? Also he doesn't need uppercase or lowercase conversion because his letters are not referring to ASCII
Yeah, I just double-checked. I am right about the fact that the OR operator only requires one |.
@Adijunn (4)

Yeah, I just double-checked. I am right about the fact that the OR operator only requires one |.


Well you are wrong. A single | is the bitwise inclusive OR, which is not the same thing (very different) as the Boolean logical operation OR.

http://www.cplusplus.com/doc/tutorial/operators/


Last edited on
The bit-wise or operator is |. The logical or operator is ||. As it happens, they will evaluate to the same true or false value for this particular example, nevertheless the logical or operator (||) is what is called for here - we don't care about the bit patterns of boolean values.
thankes for all i very happy cuz that thankes agin
Topic archived. No new replies allowed.