Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple array question...please

Status
Not open for further replies.

MrRomeo

IS-IT--Management
Mar 15, 2002
2
US
I know this is easy, but for some reason I am having a hard time with it...(I am fairly new to C++)

What I want to do is set up code that asks how many names you want to input, stores each name in an array, and then once all names are entered, simply prints them to the screen.

I cannot for the life of me figure this out....

Thanks in advance to any that can offer assistance.
 
here's one way to do it:

//get the number of names
int number;
cout<< &quot;enter the number of names&quot;;
cin>>number;

//allocate memory for an array of CString
//you might also try an array of char*
CString *array;
array=new CString[number];

//now prompt the user for name input
//and store the names
for(int i=0; i<number; i++)
{
cout<< &quot;enter name&quot;;
cin>>array;
}

//finally and display the names
for(i=0;i<number;i++)
{
cout<<array;
}


hope this helps
 
#include <iostream>
#include <string>

#define MAX_TEXT 100

void LoadArray( std::string*,int );
void OutArray( std::string*,int );

int main()
{
std::string* sNames=0;
int iElems =0;

std::cout << &quot;How many names would you like?: &quot;;
std::cin >> iElems;
sNames=new std::string[iElems];

LoadArray( sNames,iElems );
OutArray( sNames,iElems );

delete [] sNames;
return 0;
}

void OutArray( std::string* s,int iSize )
{
std::string *p;
std::cout <<
std::endl;

for( p=s;p<&s[iSize];p++ )
std::cout << *p <<
std::endl;
}

void LoadArray( std::string* s,int iSize )
{
char sBuffer[MAX_TEXT];

getchar();
for( int i=0;i<iSize;i++ )
{
std::cout << &quot;Enter name # &quot; << i+1 << &quot;:&quot;;
std::cin.getline( sBuffer,MAX_TEXT );
s=sBuffer;
}

} Mike L.G.
mlg400@blazemail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top