Solved

list

Posted on 1998-12-12
7
180 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

728 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