void main()
{
char name[80];
char *p;
int i;
cout<<"Enter your full address.: ";
cin.getline(name,80);
i=0;
p=name;
while(name[i]='\0')
{
if(*p!='.')
cout<<*p;
else
cout<<"\n" <<*p;
i++;
p++;
}
}
when i give address in one line as
xxx.c/o opp sai petrol pump.pune-24.
it should appear in following format
xxx.
c/o opp sai petrol pump.
pune-24.
but i got o/p in somewhat differnet format on screen
xxx
. c/o opp sai petrol pump
. pune-24
.
why this happened. let me know where is my mistake and why?
Firstly, is you're program C or C++? #include <iostream.h , #include <conio.h> are both old headers.
Secondly, I see no need for Conio to be in there as you haven't used any functions from its library.
Thirdly, main must always return an int - it must not be void. Some compliers do not even allow this to be compiled.
while(name[i]='\0') I believe this reads as: if name is null, execute this code.
Also, the reason you code it printing it in that format is that when the code reaches a '.' you are outputting a new line and then the '.' itself. You need to increment the pointer by one to move on to the next character so not to ouput the '.'.
Your code didn't seem to compile when I run it so I made my own that you may find useful. It doesn't contian pointers however: