You are here:

C++/Program question

Advertisement


Question
I have to write a program that takes phone numbers that are spelled out in letters and then convert it to the corresponding phone number.  I am having great difficulty with this program.
Any help would greatly be appreciated.

Thanks
Cheryl

Answer
#include<iostream>

#include <string>
using namespace std;
using std::string;
int main(int argc, char *argv[])
{
 string number;
 string compare[10] = {"zero","one", "two","three","four","five","six","seven","eight","nine"};
 cout<<"enter the phone number";
 cin>>number;
 int num[10],multiple =1,pos =0,loop=0;
 int length = number.length();
 bool found= false;
 int place =0;
 do
 {
     string s = number.substr(pos,3);
     for( int i=0;i<10;i++)
     {
         if(stricmp(s.c_str(),compare[i].c_str())==0)
         {
         found = true;
         place = i;
         break;
         }
     }
     if(found == false)
     {
         s= "";
         s = number.substr(pos,4);
         for( int i=0;i<10;i++)
         {
         if(stricmp(s.c_str(),compare[i].c_str())==0)
         {
         found = true;
         place = i;
         break;
         }
         }
     }
     if(found == false)
     {
         s= "";
         s = number.substr(pos,5);
         for( int i=0;i<10;i++)
         {
         if(stricmp(s.c_str(),compare[i].c_str())==0)
         {
         found = true;
         place = i;
         break;
         }
         }
     }  
     pos=pos + s.length();    
     s = "";
     
     num[loop] = place;
     loop++;
     found = false;      
         
}   
while(pos != length) ;
for(int i = 0;i<10;i++)
cout<<"the number is"<< num[i];
cin>>multiple;
 return 0;
}
Hi,

I have compleeted almost unless the number entered should be zero to nine. If it exceeds nine then follow the similar proceedure.Also, Please the convert the end array back to number

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


rakshitha

Expertise

Difficult questions related to C .

Experience

C ,vc ,opc ,windows CE, ActiveX

Education/Credentials
Masters in electrical engineering

©2016 About.com. All rights reserved.