Solved

Pass data to a multidimensional array

Posted on 2002-03-17
3
208 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 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 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.

776 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