?
Solved

list

Posted on 1998-12-12
7
Medium Priority
?
181 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 80 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

Technology Partners: 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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

762 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