switch statemet letters to numbers conversion

I have tried to structure my program but im stuck at this point. I have a file that contains phone phrases on each line (1-900-WINNING), is one of the phrases, if i get more digits than a phone number requires i must ignore the remainder. This data is coming from a file. Im just not understanding it, maybe i shouldn't read my data in as a string? . I am suppose to use a switch statement in my solution

the output should look like this
1-800-Free-Willy -> 1-800-373-3945

heres my program so far

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

int main()
{
ifstream inFile;
string phrase;
char x;

inFile.open("abc.txt");

inFile >> phrase;

while(inFile)
{
getline(inFile,phrase);
inFile.get(x);
cout << setw(17) << phrase << "-> ";
cout << endl;
inFile >> phrase;

if (( x >= 'A' && x <= 'Z') || ( x >= 'a' && x <= 'z') || ( x >= 0 && x <= 9 ))
{
switch (x)
{
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
case '2':
cout << "2";
break;

case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
case '3':
cout << "3";
break;

case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
case '4':
cout << "4";
break;

case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
case '5':
cout << "5";
break;

case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
case '6':
cout << "6";
break;

case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
case '7':
cout << "7";
break;

case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
case '8':
cout << "8";
break;

case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
case '9':
cout << "9";
break;

}



}

}



system("pause");
return 0;
}
Reading in as string is not a problem since the number doesn't only contain digits. However if you are learning C++, you might try to use C++ things instead of using switch. A hint, since this is just a letters to digits translation work, you can predefine a translation table, then use this table to translate phone numbers you read from file.
Last edited on
this is an intro class but the teacher he said he would expect us to use switch statement. why cant i make the way i started it work?
I can see the following things need to be reviewed to make the program working.

- while (inFile)
- where to handle 0 and 1?
- getline(inFile,phrase); // if phone# is read into phrase here
inFile.get(x); // why read a char from file to x?
inFile >> phrase; // why read another phone# into phrase before previous one is parsed?
-I dont see whats wrong with while(inFile)
-I handled 0 but don't know what to do with one
-i'm also confused here, ive been told to read in like this, i don't understand this, if i could somehow see an example on how all this works together or if someone could help me step by step id appreciate it.

Don't read in a string. Do initialize new_str = "";

Read in a character using .get and out put it.
figure out what the digit should be.
new_str = new_str + character_digit ('0', '1', etc)

Repeat.

When you get to ' ', output new_string.
Topic archived. No new replies allowed.