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.

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;
}

<< moderator edit: added code tags: [co[u][/u]de][/co[u][/u]de] >>

Recommended Answers

All 3 Replies

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?

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.

#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;
}

<< moderator edit: added [co[u][/u]de][/co[u][/u]de] tags >>


Input data:

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

I got my own answer. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.