Solved

list

Posted on 1998-12-12
7
174 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:The_Brain
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you MatthewL
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now