s #include 6 using namespace std 8 char translateletter(char ch) 10 switch (ch)
ID: 3730807 • Letter: S
Question
s #include 6 using namespace std 8 char translateletter(char ch) 10 switch (ch) 12 13 case 'A case 'a': 14 15 16 17 18 19 20 21 case B' case 'b': case 'C' case 'c': return '2' break; case D case 'd': case 'E: 23 24 25 26 27 28 29 30 31 32 case 'e': case ' F ' : case 'f' return '3' break; case 'G' case 'g' case 'H': case 'h' case 'I' case 'i': 34 35 36 37 38 return 4' break; 39 case J': 41 42 43 case 'j case 'K' case 'k' case 'L': case '1': 45 46 47 48 49 50 51 -INSERT return '5'; break; case 'M' case 'n' case 'N': case 'n':Explanation / Answer
Let's see what is happening by checking each value one by one :
Here as we have cout<<ch inside each if condition hence we have no need to do cout<<ch after loop ends. This is why last digit is printing twice as ch still holds the last digit after loop ends and prints it.
Remove cout<<ch after loop and it will fix the issue.
i i<len ch = phone.at(i) ch>='0' && ch<='9', cout<<ch ch>='a' && ch<='b', ch= translateLetter(ch) cout<<ch ch>='A' && ch<='B', ch= translateLetter(ch) cout<<ch i++ Loop ends, cout<<ch 0 0<3, T ch = phone.at(0)= A ch = translateLetter(A): ch = 2: cout<<2 1 1 1<3, T ch = phone.at(1)= 2 cout<<ch: cout<<2 2 2 2<3, T ch = phone.at(2)= 4 cout<<ch: cout<<4 3 3 F Here loop ends and ch hols value 4 Now cout<<4Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.