Solved

Pass data to a multidimensional array

Posted on 2002-03-17
3
225 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
[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
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

756 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