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 1

Status
Not open for further replies.

MrRomeo

IS-IT--Management
Mar 15, 2002
2
US
I am new to C++ and here is what I am trying to do (I am using Borland C++ for this)

I want to ask the user how many names to process and then take those names, store them in an array, and then print them all to the screen.

I can't for the life of me get it to work correctly....can someone help me with this?

Scott
 
Show us some code.
James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
I can take a stab at this, but without any code I have a good chance of thoroughly slaughtering it.

//--------------------------------------------
/* Decalre these in the header */

/*
The maximum number of names...
You can also just take out this and not put anything inside the brackets. This will allow you an unlimited amount of names.
*/
#define MAXNAMES 100

/* The array */
String userNames[MAXNAMES];

/* your going to need to memset the array, but I'm doing this from memory and i don't want to give you bad code if avoidable */

//--------------------------------------------
/* this is after the user inputs a name. WARNING: this will only input one name */

/* where the name is stored */
String name;

/* loop through the array in order to find a blank space for the name */
for (int i = 0; i <= MAXNAMES; i++)
{
/* find a blank string */
if (userNames == NULL)
{
/* put in the name */
userNames = name;
return;
}
}

//--------------------------------------------
/* inside the function where you want the name printed */

for (int i = 0; i <= MAXUSERS; i++)
{

/* print the string. I would do this the correct way, (%s, and so on...) but it's been a while since I had to do console programming, and I forgot */

String p = &quot;Name Number &quot; + IntToStr(i) + &quot;: &quot; + userNames;
printf(p);

}
//--------------------------------------------

That's the best I can do from memory without code. I don't have a compiler on the machine I'm writing this from, so I'm doing all of this somewhat blind. If it doesn't work right, or isn't what you want, don't hesitate to ask and I'll see what I can't do. Cyprus
[noevil]
 
oops, that code won't quite work right, i dont think... it took out the [ i ] after:


if (userNames[ i ] == NULL)
{
/* put in the name */
userNames = name;
return;
}


try that, if you didn't get the expected results.
Cyprus
[noevil]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top