Passing array of structures into a function

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2005
Posts: 3
Reputation: amy590 is an unknown quantity at this point 
Solved Threads: 0
amy590 amy590 is offline Offline
Newbie Poster

Passing array of structures into a function

 
0
  #1
Oct 24th, 2005
I direly need help with passing an array of structures into a function.
The problem is that Main_menu.name is blank. What is the correct syntax to pass by reference, so that I will get the Main_menu.name in main()? I would like to use pointer in the createMenu function for efficiency. createMenu function reads data from a resource file and assigns the value to the field.
Would someone please help? Thanks.

  1. Struct menu
  2. {
  3. int Id;
  4. char name[10];
  5. };
  6.  
  7. menu Main_menu[10];
  8.  
  9. void createMenu (menu* menuPtr);
  10. {
  11. While (!numData.getline(inputId,10, '$').eof())
  12. {
  13. menuPtr->Id = inputId;
  14. getline( inputName, 10, '\n');
  15. strcat(menuPtr->name, inputName);
  16. menuPtr++;
  17. }
  18.  
  19. }
  20.  
  21. int Main ()
  22. {
  23. createMenu(Main_menu);
  24. For (int i= 0; I < 10; i++)
  25. { Cout << Main_menu.id << Main_menu.name << endl;}
  26. return 0;
  27. }
<< moderator edit: added code tags: [code][/code] >>
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Passing array of structures into a function

 
0
  #2
Oct 24th, 2005
First, consider a real code editor instead of something like MS Word that capitalizes unwantedly and automatically. C and C++ are case sensitive. And I'm sure there must be plenty of good, and free, code editors available.

Second, it's a bad idea to use eof() to control a loop. Check for success rather than one possible failure.

Third, using strcat to do a strcpy may not be the safest choice.

Now, do you have an actual copy-and-paste of your actual code, and perhaps a sample input, so that it would be easier for me (and others) to try to help you?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 3
Reputation: amy590 is an unknown quantity at this point 
Solved Threads: 0
amy590 amy590 is offline Offline
Newbie Poster

Re: Passing array of structures into a function

 
0
  #3
Oct 25th, 2005
Originally Posted by Dave Sinkula
First, consider a real code editor instead of something like MS Word that capitalizes unwantedly and automatically. C and C++ are case sensitive. And I'm sure there must be plenty of good, and free, code editors available.

Second, it's a bad idea to use eof() to control a loop. Check for success rather than one possible failure.

Third, using strcat to do a strcpy may not be the safest choice.

Now, do you have an actual copy-and-paste of your actual code, and perhaps a sample input, so that it would be easier for me (and others) to try to help you?
--------------------------------------------------------------------
Here are the codes, with data input at end. Thanks again.

  1. #include <iostream>
  2. #include <fstream.h>
  3. #include <string.h>
  4.  
  5. using namespace std;
  6.  
  7. ofstream outData;
  8.  
  9.  
  10. //input file structure
  11. ifstream inData;
  12. char menu_id[10];
  13. char menu_name[10];
  14. char menu_col[10];
  15. char menu_row[10];
  16. char button_id[10];
  17. char button_label_1[10];
  18. char button_label_2[10];
  19. char button_label_3[10];
  20. char button_col[10];
  21. char button_row[10];
  22.  
  23. //menu file structure
  24. struct menu
  25. {
  26. int mid;
  27. char name[20];
  28. int mcol;
  29. int mrow;
  30. int bid;
  31. char blabel[24];
  32. int bcol;
  33. int brow;
  34. };
  35.  
  36. //List of menus
  37. enum menu_id
  38. {
  39. MAIN = 1
  40. };
  41.  
  42.  
  43. //Define Main menu
  44. menu Main_menu[3];
  45.  
  46.  
  47.  
  48. void createMenu(menu newMenu[], int menu_num, MAS_Configure_Button_Request_Message* msg )
  49. {
  50. int y = 0;
  51.  
  52. while ((!inData.getline(menu_id,10, '$').eof()) && (atoi(menu_id) == menu_num))
  53. {
  54.  
  55. newMenu[y].mid= atoi(menu_id);
  56.  
  57. inData.getline(menu_name, 10, '$');
  58. newMenu[y].name[0] = '\0';
  59. strcpy(newMenu[y].name,menu_name);
  60. outData << " Main Menu Name " << newMenu[y].name << " " ;
  61.  
  62. inData.getline( menu_col, 10, '$');
  63. newMenu[y].mcol = atoi(menu_col);
  64.  
  65. inData.getline( menu_row, 10, '$');
  66. newMenu[y].mrow = atoi(menu_row);
  67.  
  68. inData.getline( button_id, 10, '$');
  69. newMenu[y].bid = atoi(button_id);
  70.  
  71. inData.getline( button_label_1, 10, '$');
  72. inData.getline( button_label_2, 10, '$');
  73. inData.getline( button_label_3, 10, '$');
  74. newMenu[y].blabel[0] = '\0';
  75. strcpy(newMenu[y].blabel,button_label_1);
  76. strcat(newMenu[y].blabel,button_label_2);
  77. strcat(newMenu[y].blabel,button_label_3);
  78. outData << newMenu[y].blabel << "; " ;
  79.  
  80. inData.getline( button_col ,10, '$');
  81. newMenu[y].bcol = atoi(button_col);
  82.  
  83.  
  84. inData.getline( button_row ,10, '\n');
  85. newMenu[y].brow = atoi(button_row);
  86. y++;
  87.  
  88.  
  89. }
  90. }
  91.  
  92.  
  93. int main()
  94. {
  95. int loop;
  96. inData.open("data.txt");
  97. if (!inData)
  98. {
  99. cout << "*** Problem with opening data.txt. End program***" << endl;
  100. return(1);
  101. }
  102.  
  103. outData.open("test.txt");
  104. if (!outData)
  105. {
  106. cout << "*** Problem with opening test.txt. End program***" << endl;
  107. return(2);
  108. }
  109.  
  110.  
  111. createMenu(Main_menu, MAIN, msg);
  112. for (loop = 0; loop < 3; loop ++)
  113. {
  114. outData << " Main menu" << Main_menu[loop].bid << endl;
  115. outData << " Main menu" << Main_menu[loop].blabel << endl;
  116. }
  117.  
  118. inData.close();
  119. outData.close();
  120.  
  121. return 0;
  122. }
<< moderator edit: added [code][/code] tags >>


Input data:

  1. 1$Main$1$1$1$ $ ONE $$1$1
  2. 1$Main$1$1$2$ $ TWO $$2$1
  3. 1$Main$1$1$3$ $ QUIT $$3$1
  4. 2$Main$1$1$4$ $ THREE $$4$2
Attached Files
File Type: txt main_client.cpp.txt (2.9 KB, 5 views)
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 3
Reputation: amy590 is an unknown quantity at this point 
Solved Threads: 0
amy590 amy590 is offline Offline
Newbie Poster

Re: Passing array of structures into a function

 
0
  #4
Oct 26th, 2005
I got my own answer. Thanks.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC