Solved

Pass data to a multidimensional array

Posted on 2002-03-17
3
188 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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.

762 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

6 Experts available now in Live!

Get 1:1 Help Now