Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

Pass data to a multidimensional array

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
sramkris
Asked:
sramkris
  • 2
1 Solution
 
ct.smithCommented:
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
 
sramkrisAuthor Commented:
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
 
ct.smithCommented:
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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now