Telephone no.

Write a program that inputs a telephone number as a string in format 00920519085 and uses function getcountrycode ,getareacode,getphonecode, to extract country code , area code , and phone code respectively from the input string.call these functions from main() to display phone no in format as +92(51) 9085.


can anybody do that

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #include <iostream>
#include <conio.h>
#include <string>

using namespace std;

int getcountrycode();
int getareacode():
int getphonecode();

int main(){


return 0;
}
Last edited on
We can all do that.

You should look at your notes and try to do it too.
i got into problems when doing it ....
closed account (48T7M4Gy)
i got into problems when doing it ....

We are problem solvers. What were the problem(s) you had with your attempt, which is a small but worthwhile start?
oh, i am in the same situation. do you have something else? I had problem and have not found new solution
Here is some code to get you started:
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
#include<iostream>
#include<string>

int getcountrycode ()
{
  string countryCodeInput;
  /* extract the first 4 characters from input and store in countryCodeInput
   * return the  countryCodeInput converted to int
  */
  
}
int getareacode ()
{
  string areaCodeInput;
  /* extract characters 5-7 from input and store in areCodeInput
  * return the areaCodeInput converted to int
  */
}
int getphonecode ()
{
  string phoneInput;
  /* extract the last 4 characters from input and store in phoneInput
  * return the phoneInput converted to int
  */
}

using namespace std;

string input;

int main ()
{
  cout << "Phone demo\n";
  cout << "----------\n";
  cout << "\nEnter the phone number";
  cin >> input;

  int areaCode = getareacode ();
  int countryCode = getcountrycode ();
  int phoneNumber = getphonecode ();

  // TO DO display the phone number

  return 0;
}


You need to use the substring function of the string to extract the characters you want + stoi to convert your string to int.
if the number is like 0092-3085-472645

how to capture like 0092 part....
i am trying but not succeeding
The string class has a function called substring
http://www.cplusplus.com/reference/string/string/substr/
closed account (48bpfSEw)
just two words: regular expression!
just two words: regular expression!

@Necip
can't you find a more difficult option ?
You are dealing here with a total beginner..
closed account (48bpfSEw)
@Thomas, your wish is my command! ^^

There is it: Pattern recognition!!!

http://www.ee.oulu.fi/~topiolli/?section=cpplibs
http://www.cse.oulu.fi/CMV/Research


Last edited on
That's it. :)
Here's the original code for any phone number using structure variable

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
#include <iostream>
#include <string>

using namespace std;

    
struct phone{
    int ncode;
    int acode;
    long number;
    
    
    };
    
    int main(){
        
    phone p1,p2={92,42,922020};
    cout << "enter national code ";
    cin>>p1.ncode;
    cout <<"enter area code "<<endl;
    cin>>p1.acode;
    cout<<"enter number "<<endl;
    cin>>p1.number;
    
    cout << "Phone number "<<"\n";
    cout << "+"<<p2.ncode<<"-"<<p2.acode<<"-"<<"-"<<p2.number<<endl;

return 0;    
    

}
salmanasghar 12

Pick apart your instructions. It says first that you will get input in the format of
00920519085. So you start knowing what to start with. I would hard code this to start with and worry how to do the input later. Next it tells you to write three functions to take apart your input. Looking at the output changing the country code and area code to "ints" would get rid of leading zeros when printed. And the last part shows you what the output will look like.

The struct works, but simple variables in main will work fine. Your last output line will work, but does not match format as +92(51) 9085. Just going by the instructions.

Hope this helps,

Andy

Topic archived. No new replies allowed.