Solved

Pass data to a multidimensional array

Posted on 2002-03-17
3
198 Views
Last Modified: 2011-09-20
Hello,

I am kinda new to c++ programming..I have a menu to display and i have to do that thru a multi dimensional arrray the code looks something like this...

char name [7] [40] = {"  ","  ","   ","   ","   ","   ","   "};

     
char name0[40] = "\n Currency Menu:  \n";
char name1[40] = "\n 1.Euro Dollar  \n";
char name2[40] = "\n 2.Indian Rupees  \n";
char name3[40] = "\n 3.Great Britan Pounds  \n";
char name4[40] = "\n 4.Hong Kong Dollar  \n";
char name5[40] = "\n 5.Argentina Pesos  \n";

cout << name << endl;

I am not sure what mistake i did but the menu is not displayed...

Thanks
0
Comment
Question by:sramkris
  • 2
3 Comments
 
LVL 4

Expert Comment

by:ct.smith
ID: 6874698
A few things:
1 - Your declarations don't give you the structure you're looking for.  For example, you have a string name0 that is not in name[].  An easier way to do the declaration is the following:

char name[][] = {"In Currency Menu: \n", "1.Euro\n", "2.Rupees\n"};

2 - You should prefer using the standard library string class than character arrays for your text handling.  Simply put, there's more things you can do easier and far less things that can go wrong.  Likewise, the standard library vector should be prefered over arrays.  So:

std::vector<std::string> names;
names.push_back("Currency Menu:");
names.push_back("Euro");
...

3 - "cout << name << endl" does not give what you expect.  It will pring the location of the array, not all the contents.  Instead, use "cout << name[0] << endl" to reference particular elements in the array or vector.

4 - When using iostreams, use endl instead of '\n' where possible.  endl will give the proper end-of-line sequence for your platform, '\n' is not always corrent.
0
 
LVL 1

Author Comment

by:sramkris
ID: 6875625
Hi

Thanks for your comments...I need to implement this with a 2 dimensional array and i need the menu to be displayed
like this

Currency Menu:
1.Euro
2.Peso
3.Pounds

I declared an array and initailized it with "" then
i added values to it  
char name0[40] = "\n Currency Menu:  \n";
char name1[40] = "\n 1.Euro Dollar  \n";
char name2[40] = "\n 2.Indian Rupees  \n";
char name3[40] = "\n 3.Great Britan Pounds  \n";
char name4[40] = "\n 4.Hong Kong Dollar  \n";
char name5[40] = "\n 5.Argentina Pesos  \n";

When i run the program the results i get are correct but i am not able to display the menu...

Any help would be greatly appreciated

Thanks
0
 
LVL 4

Accepted Solution

by:
ct.smith earned 150 total points
ID: 6875654
Again, this goes back to the way you're declaring your chararcter strings.  the statement "char name0[40] = "\n Currency Menu:  \n";" does not add the string into name[][], it creates a new array called "name0" that contains your string.

Either you need to copy in the string to your 2D array or declare it containing your data.  A copy can be done as "strcpy(name[0], name0);", declaring it with the data already included is done as I posted before as point #1.

Also, going back to my point 3#, "cout << name << endl" does not give you anything useful.  You need to reference and output each element individually as:

cout << name[0] << endl;
cout << name[1] << endl;
...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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 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.

939 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