Hello, I'm Ryoga - a student trying to cram intros to Java and C++ into a single summer session... For a project, I'm trying to write a program that reads a ten digit phone number made of characters (eg. 800-buy-cars) into a string and then spits out the literal number (800-289-2277).
I've tried a lot of different things. At one point I even had a loop going to read through the array character by character, but now, for simplicity's sake, I have seven switches - one for each of the characters I wish to translate.
I included the declaration of and filling of the input string as well as the first switch below. (the full program is too long to post) Below that, I also posted the cout that's supposed to display the translated numbers to the console and the end of the program.
Right now the program runs, but it spits out the same ridiculous number every time, meaning I've done something dumb. If you have the time, could you please skim through it and tell me if you see the dumb things causing my grief?
switch (phoneChar[3])
{
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
phoneNum[3] = 2;
break;
case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
phoneNum[3] = 3;
break;
case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
phoneNum[3] = 4;
break;
case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
phoneNum[3] = 5;
break;
case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
phoneNum[3] = 6;
break;
case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
phoneNum[3] = 7;
break;
case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
phoneNum[3] = 8;
break;
case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
phoneNum[3] = 9;
break;
default:
cout << "Invalid Character" << endl;
}
~~~
cout << "The translated number is: " << phoneNum[10] << endl;
system("pause");
return 0;
}
~~~
This should be a fairly simple program... What am I doing wrong? Thank you in advance.
Anyway, I'm not quite sure what it is, but like I said in the other one I think switch cases are supposed to have a break for each one. Please, correct me if I'm wrong.
Alright, switched those things around as per your first suggestion.
So I need a for loop to display the number. Okay, thank you for the advice; I will work on that. But should the switch statements work the way I anticipated? Like, do I have the right idea so far you think?
This guy is difficult. But this is my code so far. It outputs the correct 'translation' i.e. from 800.MIS.DEPT but it doesn't output it in the correct format. All that needs to be done is to take each output of the switch statement and store it as a string, and output the string.
@Ryogathelost: Yup! :D Add that for loop and in my head it is working smoothly, but that never turns out right. Lol. you will probably have some debugging to do.
@madbadger89: Who is difficult? Me? I'm only trying to help him learn. If I freely give out the answers he won't learn anything. Give a man a fish, feed him for a day. Teach a man to fish, feed him for a lifetime.
I am having issues storing the results of the switch statement to a string and then 'out-putting' the string. The output would be easy, its just how to store after each switch statement that I cannot figure out.
If you guys need anymore help you can email me. I will definitely help out. Be warned though. I will not give you straight answers. :P letslearncpp@gmail.com
A lot of switch statements is a terribly ugly way to go about this. You can define a range of characters with an if statement using the ASCII values of the characters. On an ASCII chart a is 97, and c is 99, you could just use an if statement