I have problem in reqruired output

the input is
333-0092-8868389

required output is
Country code is = 0092
City code is = 333
7-digit number is = 8868389
Phone number in correct sequence is = 0092-333-8868389

The full code is

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>

void main()
{
char ch,*str,*country,*city,*digit;
cout<<"Enter the complete phone number: "<<endl;

gets(str);

 char *first=strtok(str,"-");
 char *second=strtok(NULL,"-");
 char *third=strtok(NULL,"-");


if(strlen(first)==4 && strlen(second)==3)
  {
	strcat(country,first);
	strcat(city,second);
	strcat(digit,third);
  }

else if(strlen(first)==3 && strlen(second)==4)
  {
	strcat(city,first);
	strcat(country,second);
	strcat(digit,third);
  }

else if(strlen(first)==7 && strlen(second)==4)
  {
	strcat(digit,first);
	strcat(country,second);
	strcat(city,third);
  }

else if(strlen(first)==7 && strlen(second)==3)
  {
	strcat(digit,first);
	strcat(city,second);
 	strcat(country,third);
  }

cout<<"Country code is :"<<country<<endl;
cout<<"City code is :"<<city<<endl;
cout<<"7-digit number is :"<<digit<<endl;
cout<<"Phone number in correct sequence is: "<<country<<"-"<<city<<"-"<<digit<<endl;

getch();
}

Recommended Answers

All 4 Replies

The first problem is that you are attempting to use unallocated pointers -- can't do that. You have to allocate space for all the characters you want to put into them. Declare them something like this: char str[255] = {0}; ; You can replace the value of 255 with anything you want, as long as its large enough to hold all the charcters you want to put into that string.

Second problem is your use of the function gets(). Don't use gets() for anything -- ever. Use fgets() then remove the '\n' that fgets() adds to the end of the string.

The first problem is that you are attempting to use unallocated pointers -- can't do that. You have to allocate space for all the characters you want to put into them. Declare them something like this: char str[255] = {0}; ; You can replace the value of 255 with anything you want, as long as its large enough to hold all the charcters you want to put into that string.

Second problem is your use of the function gets(). Don't use gets() for anything -- ever. Use fgets() then remove the '\n' that fgets() adds to the end of the string.

i try to do this but unable to solve the problem...
can u rewrite the code...

>>can u rewrite the code...
Yes I can -- but I won't. You will never learn how to write programs if other people do your work for you. Post your attempts to solve it then ask questions. I already gave you a couple hints.

Is this code a function in some program ? Otherwise you really dont need to stract the strings. Just print them in the correct order

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.