Solved

list

Posted on 1998-12-12
7
177 Views
Last Modified: 2010-04-02
How can I display a array of a set of words in c++
I want the array to be displayed in the format of a list:
for example
0) rose
1) tulip
2) lily
I want the list to have 10 elements.
0
Comment
Question by:doll112298
  • 3
  • 2
  • 2
7 Comments
 
LVL 1

Accepted Solution

by:
MatthewL earned 20 total points
ID: 1180032
Hi doll;

What you need is a 2D array.  I wrote up an example for you.
Let me know how it goes.

Matt

#include <iostream.h>
#include <string.h>

void main( void )
{
      char list[20][20];
      int count;

      strcpy( list[0], "rose" );
      strcpy( list[1], "tulip" );
      strcpy( list[2], "lily" );

      for( count = 0 ; count < 3 ; count++ )
      {
            cout << list[ count ]<< endl;
      }
}

0
 
LVL 1

Expert Comment

by:MatthewL
ID: 1180033
Hi doll;

Now that I've read the question correctly, I'll post a more appropriate answer - sorry about jumping the gun.  This time I printed out the entire list, and used the width to get the list numbers to line up.

Matt

#include <iostream.h>
#include <string.h>

void main( void )
{
      char list[20][20];
      int count;

      strcpy( list[0], "rose" );
      strcpy( list[1], "tulip" );
      strcpy( list[2], "lily" );
      strcpy( list[3], "rose" );
      strcpy( list[4], "tulip" );
      strcpy( list[5], "lily" );
      strcpy( list[6], "rose" );
      strcpy( list[7], "tulip" );
      strcpy( list[8], "lily" );
      strcpy( list[9], "lily" );


      for( count = 0 ; count < 10 ; count++ )
      {
            cout.width(2);
            cout << count+1;
            cout << ")" << list[ count ]<< endl;
      }
}


0
 
LVL 1

Expert Comment

by:The_Brain
ID: 1180034
Why did you say
list[20][20]  thats is a waste of memory.

list[10][20] would be fine...

10 elements of 20 chacters each.

if you are not so worried about the amount of letters

char* list[10] would work just as good.
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 1

Expert Comment

by:The_Brain
ID: 1180035
Why did you say
list[20][20]  thats is a waste of memory.

list[10][20] would be fine...

10 elements of 20 chacters each.

if you are not so worried about the amount of letters

char* list[10] would work just as good.
0
 

Author Comment

by:doll112298
ID: 1180036
Suppose after the list, the user chooses one of these choices
for example he chooses lily then how can I make a code so that when the next user chooses from the list lily does no longer appear in the list because it has been chosen already. Could I make like a method called Available with no parameters. Its function would be to return 1 if the element can be chosen and 0 if otherwise.
0
 
LVL 1

Expert Comment

by:MatthewL
ID: 1180037
Hi doll;

I'd suggest the easiest way would be to have a structure with a field representing availablity.  I pasted some sample code below.

Matt



#include <iostream.h>
#include <string.h>

typedef struct      {      char item[20];
                                    int  avail;
                              } list_item;



void initList( list_item *theList );
void printList( list_item *theList );

void main( void )
{
      list_item list[10];

      initList( list );
      printList( list );

      list[3].avail = 0;   // make 4th item unavialable

      printList( list );   // now reprint the list
}

void printList( list_item *theList )
{
      int count;
      int itemNum = 0;

      for( count = 0 ; count < 10 ; count++ )
      {
            if( theList[count].avail )
            {
                  cout.width(2);
                  cout << ++itemNum;
                  cout << ")" << theList[ count ].item << endl;
            }
      }
}

void initList( list_item *theList )
{
      int count;

      strcpy( theList[0].item, "rose" );
      strcpy( theList[1].item, "tulip" );
      strcpy( theList[2].item, "lily" );
      strcpy( theList[3].item, "rose" );
      strcpy( theList[4].item, "tulip" );
      strcpy( theList[5].item, "lily" );
      strcpy( theList[6].item, "rose" );
      strcpy( theList[7].item, "tulip" );
      strcpy( theList[8].item, "lily" );
      strcpy( theList[9].item, "lily" );

      for( count = 0 ; count < 10 ; count++ )
      {
            theList[count].avail=1;
      }
}
0
 

Author Comment

by:doll112298
ID: 1180038
Thank you MatthewL
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question