Converting 800fastcash to 1-800-327-8227

Hello, I'm pretty new to c++ so please help... also I have tried searching the archives and elsewhere on the internet already (perhaps I missed or did not recognize the solution)

Alright so here's my problem.. I will break it down into smaller sections noting with "*" what I have managed to accomplish already.

I am writing a program to:
--------------------------------------------------------------------------------
*1. Read in a list of phone numbers from a file, (the data in said file is not organized uniformly and the phone numbers are in the form of words)

*2. After reading in the data I would like my program to then convert all letters into the numbers they correspond to on a touch-tone telephone.

3. Then Display the original number followed by its converted equivalent.

4. All converted numbers displayed should begin with a prefix "1-" and contain "-" hyphens where appropriate.

*5. Lastly the program should disregard extra letters in the phone number that are beyond the range(length) of a normal phone number

Here is the contents of my input file
1
2
3
1-888-DOG-BITE                    
FAST-CASH-NOW
1-800-Free-Willy

and my output
1
2
3
The phone number: 1-888-364-2483
The phone number: 3278-2274-669
The phone number: 1-800-3733-945


I am most currently concerned with point 3 of my list...

is there an easy way to display each original character while storing its converted value in a string until I reach the end of line "\n" then display the converted string in its entirety ?
such that the output would resemble

1
2
3
The phone number: 1-888-DOG-BITE converts to 1-888-364-2483
The phone number: FAST-CASH-NOW converts to 3278-2274-669
The phone number: 1-800-Free-Willy converts to 1-800-3733-94559


here is the code for my program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
int main()
{
char ch1;				
int char_count = 0;		
ifstream fon_num;		
fon_num.open("phone_num.txt");	

//priming read
fon_num.get(ch1);
cout << setw(18) << left << "The phone number:" ;
if(islower(ch1))			
	ch1=toupper(ch1);
char_count++;

//conversion loop
while((fon_num)&&(!fon_num.eof()))
	{if(isupper(ch1))
		{if((ch1=='A')||(ch1=='B')||(ch1=='C'))
		 ch1 ='2';
		 else if((ch1=='D')||(ch1=='E')||(ch1=='F'))
		 ch1 ='3';
		 else if((ch1=='G')||(ch1=='H')||(ch1=='I'))
		 ch1 ='4';
		 else if((ch1=='J')||(ch1=='K')||(ch1=='L'))
		 ch1 ='5';
		 else if((ch1=='M')||(ch1=='N')||(ch1=='O'))
		 ch1 ='6';
		 else if((ch1=='P')||(ch1=='Q')||(ch1=='R')||(ch1=='S'))
		 ch1 ='7';
		 else if((ch1=='T')||(ch1=='U')||(ch1=='V'))
		 ch1 ='8';
		 else if((ch1=='W')||(ch1=='X')||(ch1=='Y')||(ch1=='Z'))
		 ch1 ='9';
		}
	if(ch1=='\n')
		{char_count=0;
		 cout << endl << setw(18) << "The phone number:";
		}
	if((char_count<=14)&&(char_count>0))  
		cout << ch1;	//Only display when (phone number not too long.)
	
	fon_num.get(ch1);
	
	if(islower(ch1))
		ch1=toupper(ch1);
	char_count++;
}
cout << endl;
return 0;
You could do something like the following pseudo-code:

1
2
3
position_of_LETTER = find(LETTER)
while (position_of_LETTER != can_not_be_found)
    replace(position_of_LETTER with appropriate_NUMBER);


http://www.cplusplus.com/reference/string/string/find/ -- Find a character or characters in a string
http://www.cplusplus.com/reference/string/string/replace -- Replace a character or characters in the string

In actual C++ code, using string& replace ( size_t pos1, size_t n1, const string& str );

1
2
3
4
5
size_t pos = str.find('a');

while (pos != std::string::npos) { // Find and replace all instances of 'a' in str
    str.replace(pos, 1, "2"); // Replace "a" with "2"  (keypad button)
}
That's stupid. OP's method is closer to optimality.

First, store the string in a... string.
1
2
std::string input, output;
std::getline(std::cin,input);


Now, for each character in the input (for (size_t a=0;a<input.size();a++)):
1
2
3
4
5
6
7
8
9
10
11
12
//(optional)
//(char push;)
switch (tolower(input[a])){
    case 'a':
    case 'b':
    case 'c':
        output.push_back('2');
        //(push='2';)
        break;
//...
}
//(output.push_back(push);) 

The above takes advantage of the fall-through property of switch. It's not faster than checking with an if, but it's easier on the eye.
Topic archived. No new replies allowed.