Hi, I'm Ryogathelost and I'm devastatingly new to C++. I've been a bit of a code detective today, wandering the internet finding different methods to use for my current assignment, which requires me to make a program that will turn a phone number with characters into all numbers. I've made this iffy frankencode below and I've been told it will work if I can ever get it to compile. I'm currently on an error (invalid types `char[int]' for array subscript) that I can't get past to test the program (which I'm not even sure will work). Does anyone have a workaround for that?
I'm also a little paranoid that the switch statement will just keep reassigning new numbers to phoneNum instead of building a new string, but I really do have very little experience with loops and strings, so I'm not sure what it will do. There sure are a lot of differing opinions out there.
I'm not asking anyone to do my work for me, but if anyone has suggestions on how I can build this program better, I welcome them. They WILL make me a better programmer.
My code follows. I've removed my comments to make it fit in the forum without screwing up the format, but if you need help identifying something (you probably won't because you're all geniuses) please let me know. I'm sorry there aren't any tabs. The text entry window ate them...
int length = phoneChar.size();
for(int i=0; i < length; i++)
{
if (isalpha(characters))
{
switch (characters[i])
{
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
phoneNum = 2;
break;
case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
phoneNum = 3;
break;
case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
phoneNum = 4;
break;
case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
phoneNum = 5;
break;
case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
phoneNum = 6;
break;
case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
phoneNum = 7;
break;
case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
phoneNum = 8;
break;
case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
phoneNum = 9;
break;
}
cout << phoneNum << endl;
}
if(i == 2)
{
cout << "." << endl;
}
}
system("pause");
return 0;
}
Again, any help would be outstanding, so thank you in advance! I'm not very bright, though, so...you know - speak slowly...
Also, I know it isn't finished. I still need to cout the answer at the end of the program. Just one of those "well, I'll cross that bridge when I get to it" sorta things...
Thank you for your character array suggestion! The program ran, but it translated every string into 1994334389 every time. Could someone offer any insight into why it does this?
Then I added a cout that was supposed to output small chunks of the number at a time and put decimal points in-between (eg. 800.555.5555) but now my compiler's complaining that I didn't declare substr. How do you declare substr?! I have <string> and <cstring> included already - isn't substr in the <string> library?
Obviously I have some fatal flaws somewhere. Any ideas?
cout << right << setw(300) << "Ryogathelost" << endl; //name
cout << "Phone Key Pad - Convert Letters to Numbers" << endl; //title heading
cout << "------------------------------------------" << endl;
cout << endl;
cout << "Enter a character string phone number (###.aaa.aaaa):" << endl; //prompt user to enter string
string phoneChar; //declares string and reads in character phone number
getline(cin, phoneChar);
cout << endl;
char characters[12]; //declares a variable for the number with characters
int phoneNum; //declares a variable for the translated number
int length = phoneChar.size();
for(int i=0; i < length; i++) //beginning of for loop to move through string
{
if (isalpha(characters[12]))
{
switch (characters[i]) //beginning of switch
{
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
phoneNum = 2;
break;
case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
phoneNum = 3;
break;
case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
phoneNum = 4;
break;
case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
phoneNum = 5;
break;
case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
phoneNum = 6;
break;
case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
phoneNum = 7;
break;
case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
phoneNum = 8;
break;
case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
phoneNum = 9;
break;
}
cout << phoneNum << endl;
}
if(i == 2)
{
cout << endl;
}
}
cout << "The translated number is: " << phoneNum.substr(0, 3) << "." << phoneNum.substr(4, 3) << "." << phoneNum.substr(8, 4) << endl;
One problem: You input into the string phoneChar and then examine the characters array. This array has nothing in it.
Another problem: when you write 'isalpha(characters[12])', there is no characters[12]. You have an empty array of 12 characters, with indexes from 0 to 11.
Third problem: You simply keep changing the single int value phoneNum in your loop. Its final value will be whatever the last loop gives it.
Yeah, I was kinda anticipating that third problem. It runs through the array, but just keeps changing the same variable. I need to use the loop to fill a new array, correct?