pointer

Hi . what is my error in program
#include <iostream>

using namespace std;

int main()
{
char name[20];
char* ptr1,*ptr2;


cout << "Enter a name -> ";
cin >> name;
cout << "Output : ";

for (ptr1 = &name[0]; ptr1 != '\0'; ptr1++)
{
for (ptr2 = ptr1; ptr2 != '\0'; ptr2++)
cout << *ptr2;
cout << ' ';
}
cout << endl;

system("pause");
return 0;
}

Last edited on
This:
for (ptr1 = &name[0]; ptr1 != '\0'; ptr1++)

should probably be:


for (ptr1 = &name[0]; *ptr1 != '\0'; ptr1++)

And probably the same with ptr2.
I didnt understand what you meant to do with this program but I corrected the errors and the code is...
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
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
char name[20];
char* ptr1,*ptr2;


cout << "Enter a name -> ";
cin >> name;
cout << "Output : ";

for (ptr1 = &name[0]; ptr1 < &name[0] + strlen(name); ptr1++)
{
for (ptr2 = ptr1; ptr2 < &name[0] + strlen(name); ptr2++){
cout << *ptr2;
cout << ' ';
}
}
cout << endl;

system("pause");
return 0;
}

Erm... could you please not use system(), and rather use cin.ignore(); cin.ignore();? It's considerably better form.

-Albatross
Ok.I can try...Thanks..
Topic archived. No new replies allowed.