| | |
Passing array of structures into a function
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2005
Posts: 3
Reputation:
Solved Threads: 0
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.
<< moderator edit: added code tags:
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)
Struct menu { int Id; char name[10]; }; menu Main_menu[10]; void createMenu (menu* menuPtr); { While (!numData.getline(inputId,10, '$').eof()) { menuPtr->Id = inputId; getline( inputName, 10, '\n'); strcat(menuPtr->name, inputName); menuPtr++; } } int Main () { createMenu(Main_menu); For (int i= 0; I < 10; i++) { Cout << Main_menu.id << Main_menu.name << endl;} return 0; }
[code][/code] >> 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
Third, using
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?
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
•
•
Join Date: Oct 2005
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
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 useeof()to control a loop. Check for success rather than one possible failure.
Third, usingstrcatto do astrcpymay 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)
#include <iostream> #include <fstream.h> #include <string.h> using namespace std; ofstream outData; //input file structure ifstream inData; char menu_id[10]; char menu_name[10]; char menu_col[10]; char menu_row[10]; char button_id[10]; char button_label_1[10]; char button_label_2[10]; char button_label_3[10]; char button_col[10]; char button_row[10]; //menu file structure struct menu { int mid; char name[20]; int mcol; int mrow; int bid; char blabel[24]; int bcol; int brow; }; //List of menus enum menu_id { MAIN = 1 }; //Define Main menu menu Main_menu[3]; void createMenu(menu newMenu[], int menu_num, MAS_Configure_Button_Request_Message* msg ) { int y = 0; while ((!inData.getline(menu_id,10, '$').eof()) && (atoi(menu_id) == menu_num)) { newMenu[y].mid= atoi(menu_id); inData.getline(menu_name, 10, '$'); newMenu[y].name[0] = '\0'; strcpy(newMenu[y].name,menu_name); outData << " Main Menu Name " << newMenu[y].name << " " ; inData.getline( menu_col, 10, '$'); newMenu[y].mcol = atoi(menu_col); inData.getline( menu_row, 10, '$'); newMenu[y].mrow = atoi(menu_row); inData.getline( button_id, 10, '$'); newMenu[y].bid = atoi(button_id); inData.getline( button_label_1, 10, '$'); inData.getline( button_label_2, 10, '$'); inData.getline( button_label_3, 10, '$'); newMenu[y].blabel[0] = '\0'; strcpy(newMenu[y].blabel,button_label_1); strcat(newMenu[y].blabel,button_label_2); strcat(newMenu[y].blabel,button_label_3); outData << newMenu[y].blabel << "; " ; inData.getline( button_col ,10, '$'); newMenu[y].bcol = atoi(button_col); inData.getline( button_row ,10, '\n'); newMenu[y].brow = atoi(button_row); y++; } } int main() { int loop; inData.open("data.txt"); if (!inData) { cout << "*** Problem with opening data.txt. End program***" << endl; return(1); } outData.open("test.txt"); if (!outData) { cout << "*** Problem with opening test.txt. End program***" << endl; return(2); } createMenu(Main_menu, MAIN, msg); for (loop = 0; loop < 3; loop ++) { outData << " Main menu" << Main_menu[loop].bid << endl; outData << " Main menu" << Main_menu[loop].blabel << endl; } inData.close(); outData.close(); return 0; }
[code][/code] tags >>Input data:
C++ Syntax (Toggle Plain Text)
1$Main$1$1$1$ $ ONE $$1$1 1$Main$1$1$2$ $ TWO $$2$1 1$Main$1$1$3$ $ QUIT $$3$1 2$Main$1$1$4$ $ THREE $$4$2
![]() |
Similar Threads
- Passing 2D Array of Pointers into a function (C++)
- Dynamic Array of Structures, Loop problem! (C++)
- Passing a Value between an Array and Another Function (C++)
- Creating dynamic array structures (C++)
- dynamic array of structures problem (C++)
Other Threads in the C++ Forum
- Previous Thread: Need help in C/C++
- Next Thread: query about getchar()
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






