Solved

list

Posted on 1998-12-12
7
179 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
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!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Building cUrl in Windows v7.43.0 4 39
how not to pass environment to child process by using ShellExecute or ShellExecuteEx 16 170
Unable to start eclipse ? 17 163
Header of docx file 17 132
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

726 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