Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am coding a GUI for my menu. The problem is this that when I access the drawtext function only the first element of my sub_menu char array is getting displayed when I access the function using

drawText(38,195,*a->sub_Menu[1],0);
drawText(38,240, a->sub_Menu[2],0);
drawText(38,285, a->sub_Menu[3],0);
drawText(38,330, a->sub_Menu[4],0);
and rest of the boxes show up blank. And when I try to access the drawtext funcion using

drawText(38,195,*a->sub_Menu[1],0);
drawText(38,240,*a->sub_Menu[2],0);
drawText(38,285,*a->sub_Menu[3],0);
drawText(38,330,*a->sub_Menu[4],0);
the program compiles and runs but as soon as I click on Settings button of my menu the program crashes saying myprogram.exe has stopped working. I don't know what the problem is as I am new to coding. Kindly Help me out.
C#
typedef struct {
    short startXPos;
    short startYPos;
    short height;
    short width;
    unsigned int c;
    char *sub_Menu[5][18];
} menu, *ptr_Menu;

ptr_Menu a;
char sub_Menu1[5][18] = {"Big Font", "5 channel", "7 channel", "12 channel", "Alarm"};
menu touch_menu[10] = {30, 365, 45, 100, 5, &sub_Menu1};

void drawMenu(short b)
{
    int k = 0;
    if (b == 0) {
        a = &touch_menu[0];
        for (k=0; k<a->c; k++) {
            setColor(GREY);
            drawRectangle(a->startXPos, a->startYPos - (k+1)*a->height,a->width,a->height);
        }
        setColor(CYAN);
        drawText(38,150,*a->sub_Menu[0],0);
        drawText(38,195,*a->sub_Menu[1],0);
        drawText(38,240,*a->sub_Menu[2],0);
        drawText(38,285,*a->sub_Menu[3],0);
        drawText(38,330,*a->sub_Menu[4],0);
    }
}
Posted

1 solution

You have employed some unwieldy ideas here. The two dimensional array is problematic. I can't compile your code because sub_Menu is declared as an array of size 5 of arrays of size 18 pointers to char and you can't take the address of sub_Menu1 and assign that to it.

However
C++
menu touch_menu[10] = {30, 365, 45, 100, 5, &sub_Menu1};

only initialises the first member of touch_menu.

Look at this and it may solve your problem:

C++
#include <iostream>
using namespace std;

typedef struct {
    short startXPos;
    short startYPos;
    short height;
    short width;
    unsigned int c;
    char (*sub_Menu)[18];
} menu, *ptr_Menu;
 
ptr_Menu a;

char sub_Menu1[5][18] = {"Big Font", "5 channel", "7 channel", "12 channel", "Alarm"};

menu touch_menu[10];

int main()
{
	for(int i=0; i <10; i++)
	{
		touch_menu[i].c = i;
		touch_menu[i].startXPos = 30;
		touch_menu[i].startXPos = 365;
		touch_menu[i].sub_Menu = sub_Menu1;
	}

    a = &touch_menu[0];

	for(int i=0; i <10; i++)
		cout << (a + i)->c << "   " << (a+i)->sub_Menu[0] << endl;

	return 0;
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900