array of character pointers
Am new to c++, and am having some problems in initilizing arrays that hold strings during the program.
I want to write a program that requests 5 names from the user
and then stores it in an array of character pointers.
Example:
int main()
{
clrscr();
char *names[5];
int ctr;
for(ctr=0; ctr<5; ctr++)
{
cout<<"\nWhat is the next name " ;
cin>>*(names+ctr);
}
for(ctr=0; ctr<5; ctr++)
{
cout<<"Names:\n\n ";
cout<<*(names+ctr) <<"\n";
}
return 0;
}
When a try to run this it just shows garbage . Can anyone give me a hint of where i went wrong.
Thanks in advance.
Re: array of character pointers
PLEASE LOOK AT THE COMMENT THAT I MADE
Quote:
Originally posted by RedRum
I want to write a program that requests 5 names from the user
and then stores it in an array of character pointers.
Example:
Code:
int main()
{
clrscr();
char *names[5];
int ctr;
for(ctr=0; ctr<5; ctr++){
cout<<"\nWhat is the next name " ;
cin>>*(names+ctr); // YOU CAN'T DO THIS
}
for(ctr=0; ctr<5; ctr++){
cout<<"Names:\n\n ";
cout<<*(names+ctr) <<"\n"; // YOU CAN'T DO THIS TOO
}
return 0;
}
When a try to run this it just shows garbage . Can anyone give me a hint of where i went wrong.
As you are new in C++ ... try first to arrang your code...
You just remind me in the way that I start...
LQQK at the code that Chris wrote you .. and then try to solve it in another way.. this way you will learn more....