943,935 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 18228
  • C++ RSS
Oct 24th, 2005
0

Passing array of structures into a function

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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] >>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amy590 is offline Offline
3 posts
since Oct 2005
Oct 24th, 2005
0

Re: Passing array of structures into a function

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?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 25th, 2005
0

Re: Passing array of structures into a function

Quote 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.

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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, 31 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amy590 is offline Offline
3 posts
since Oct 2005
Oct 26th, 2005
0

Re: Passing array of structures into a function

I got my own answer. Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amy590 is offline Offline
3 posts
since Oct 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need help in C/C++
Next Thread in C++ Forum Timeline: query about getchar()





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC