5689 Shares

Problem with beginners' C Plus Plus code - Converting alpha phone numbers into numerical?

Problem with beginners' C Plus Plus code - Converting alpha phone numbers into numerical? Topic: Example of switch case statement c++
April 19, 2019 / By Garfield
Question: So I'm taking a basic C++ class, and the programming assignment this week is to write a program that converts a telephone number in letters (example: GET-LOANS) to numbers. Only take 7 letters, ignore spaces, put a hyphen after the third digit on output.Most of my code is working right, but I've got something wrong, because it's not converting them right. What's the problem with my code? #include using namespace std; int main () { char doContinue, tempLetter; int tempNum, numOne, numTwo, numThree, numFour, numFive, numSix, numSeven; int numCount; numCount = 1; tempNum = 0; cout << "Enter Y/y to convert a telephone number from letters to digits." ; cout << endl; cout << "Enter any other letter to terminate the program." << endl; cin >> doContinue; while (doContinue == 'Y' || doContinue == 'y') { cout << endl << "Enter a telephone number using letters: "; while (numCount <= 7) { cin >> tempLetter; switch (tempLetter) { case 'A': case 'B': case 'C': case 'a': case 'b': case 'c': tempNum = tempNum + 2; break; case 'D': case 'E': case 'F': case 'd': case 'e': case 'f': tempNum = tempNum + 3; break; case 'G': case 'H': case 'I': case 'g': case 'h': case 'i': tempNum = tempNum + 4; break; case 'J': case 'K': case 'L': case 'j': case 'k': case 'l': tempNum = tempNum + 5; break; case 'M': case 'N': case 'O': case 'm': case 'n': case 'o': tempNum = tempNum + 6; break; case 'P': case 'Q': case 'R': case 'S': case 'p': case 'q': case 'r': case 's': tempNum = tempNum + 7; break; case 'T': case 'U': case 'V': case 't': case 'u': case 'v': tempNum = tempNum + 8; break; case 'W': case 'X': case 'Y': case 'Z': case 'w': case 'x': case 'y': case 'z': tempNum = tempNum + 9; break; case 1: tempNum = tempNum + 1;
Best Answer

Best Answers: Problem with beginners' C Plus Plus code - Converting alpha phone numbers into numerical?

Deryck Deryck | 2 days ago
First off, why are you doing this? tempNum = tempNum + 2; Why not just tempNum = 2? Are you trying to build a string of digits, because this is not what the code is going to do for you. It would also be helpful if you told us what was happening as opposed to what you want to happen. You also need to close the while loop, which I assume you mean to do at the end of the switch statement. But your while loop is only going to do one digit and then exit. What you might want to do is make tempNum a string instead of an integer and build a string of digits. ONce you have the string, insert the - in the correct place. If that is what you meant to do, then you do need tempNum to be a string and lines like these, tempNum = tempNum + 2; need to actually be tempNum = tempNum + "2";
👍 132 | 👎 2
Did you like the answer? Problem with beginners' C Plus Plus code - Converting alpha phone numbers into numerical? Share with your friends

We found more questions related to the topic: Example of switch case statement c++


Deryck Originally Answered: I need help solving a cyper problem with numbers as a code?
Assuming that the person who put this together knows something about codes, do this: On a sheet of paper write the alphabet, in order with space between the letters, on one line if possible. Above this line write a 5 above the W. Number the rest of the letters in order until you get to Z and the go back and continue from A. If you do this right, you will have assigned each letter a number from 1 to 26. Below the alphabet you already wrote, write another, starting with V under the A. After you use Z start from A and continue to the end. Now, for each number in the message, substitute the bottom letter you just wrote. This should decode the message.
Deryck Originally Answered: I need help solving a cyper problem with numbers as a code?
so whenever 5 comes up replace it with a W, and the same for other numbers then everywhere in there a v comes up, put an A, and the same for the other letters then do step three Can't do more if you don't post more information

Benoni Benoni
Voting: 1.Adam Ejaz. 2.Dylan Klein. 3.Cameron Hart. 4.Master Of Moves. 5.Scarface. 6.Icon. 7.Deadman 4 Life. 8.Fallen Diablo. 9.Ted Dibiase Jr w/Charlie Scene,Richard Kingston and I A Meen. 8/10.Nice Card.
👍 50 | 👎 0

Benoni Originally Answered: Do the people have any psychological problem who write their own or other cell phone numbers randomly?
I'm not a psychologist or psychiatrist, but from my experience since 1998 while using the cell phones in Bangladesh I came to the following conclusions, which may not be correct in all the cases one can come across, as per your question. 1. Own Cell Number in public places (as mentioned by you): These people are either new with mobile set or never/sparingly used any telephone due to non-availability at their place of living (as in most of the 64,000 villages in Bangladesh) and they would like to make friends with people randomly, without having any idea as to what may go wrong when their personal mobile phone numbers are in the hand of people with 'disturbed' mind or simply criminal minded people. 2. Others' Cell Number in public places (as mentioned by you): The psychology behind this is different than the one mentioned above. Here the perpetrator(s) try to generate nuisance calls to the number of their 'foes' or people they want to 'punish' for their own satisfaction. I was the victim of such nuisances and got the callers ID from the Mobile Phone provider. I also tried with RAB where they have very modern tracking system for nuisance callers. For this help, you have to first file a General Diary Entry (GDE) with the local police station and then try to get the same checked by RAB or such intelligence teams, if the cases are serious, like asking for ransom money etc. I do hope that the Government of Bangladesh takes these nuisances seriously and save the 'user' of mobile phone services from perpetrators who are making us to keep the cell phones in 'off' mode or change the SIM cards frequently to avoid the radar screen of such criminals who are still getting free-rides even though Govt has made it mandatory to get all the SIM cards registered, but the follow-up actions like switching off non-registered SIMs are not made as vigorously as assured initially. So you may assume that there are people who are basically perverted and try to make the life of their opponent as miserable as possible, and the cell phone is the latest gadget in their armory to fire upon their target. Hope above would help.
Benoni Originally Answered: Do the people have any psychological problem who write their own or other cell phone numbers randomly?
There is some level of anti-social behavior in that type of act, but is not normally considered a psychological dysfunction.

If you have your own answer to the question example of switch case statement c++, then you can write your own version, using the form below for an extended answer.