C++/How can i correct an error in this program(to use arrow keys for selection in a menu)..
Expert: Zlatko - 7/19/2010
QuestionDear Zlatko,
I have wrtiten a program to use arrow keys for selection in a menu..
I wrote it as a function so that i can save it as ".h" file and use in future..
But to test when i used it for the first time there as an error an passing 2d charcter array as argument..
I use TurboC++ 3.0 on windows xp
My program goes like this.
═══════════════════════════════════════════════════════════════
#include<conio.h>
#include<iostream.h>
int nmenuitemcount;
int nselectedindex=0;
//═══════════════════════════════════════════════════════════════
void drawmenu(char menu[][10])
{
int imenuitemcount=sizeof(menu)/sizeof(menu[0]);
nmenuitemcount=imenuitemcount;
int imenuitem;
textbackground(BLACK);
textcolor(YELLOW);
for(imenuitem=0;imenuitem<nmenuitemcount;imenuitem++)
{
if(imenuitem==nselectedindex)
{
textcolor(WHITE) ;
textbackground(BLUE) ;
}
else
{
textcolor(LIGHTGRAY) ;
textbackground(BLACK) ;
}
cprintf(menu[imenuitem]);
cprintf("\r\n");
}
}
int extendedkeypress(int key)
{
int iretvalue=0;
switch(key)
{
case 72:
if(nselectedindex>0)
{
nselectedindex--;
iretvalue=1;
}
break;
case 80:
if(nselectedindex<(nmenuitemcount-1))
{
nselectedindex++;
iretvalue=1;
}
}
return iretvalue;
}
int smenu(char menu[][10])
{
int brunning=1;
nselectedindex=0;
int nkey;
int brefreshmenu=1;
int toreturn;
while(brunning==1)
{
if(brefreshmenu==1)
{
drawmenu(menu[][10]);
}
nkey=getch();
switch(nkey)
{
case 0:
brefreshmenu=extendedkeypress(getch());
break;
case 13:
toreturn=nselectedindex;
break;
}
}
textbackground(BLACK);
clrscr();
return toreturn;
}
void main()
{
char temp[][10]={
"1.ABC ",
"2.DEF ",
"3.GHI ",
};
int y;
y=smenu(temp[][10]);
cout<<y;
getch();
}
═══════════════════════════════════════════════════════════════
Please check the program and help me to solve the error..
Thanks in advance
Avijeet
AnswerHello Avijeet
The problem with the program is in the way temp is being passed from main to smenu and from smenu to drawmenu.
In main, the call should look like this
y=smenu(temp);
not like this
y=smenu(temp[][10]);
The same is true for the other places menu is being passed around.
The second problem is the idea of finding the count of menu items in drawmenu
At that point
int imenuitemcount=sizeof(menu)/sizeof(menu[0]);
will not work because the compiler has no knowledge of the first dimension of menu. In the function, menu is just a pointer, and sizeof(menu) will be the size of a pointer, or 4 bytes.
You need to calculate
int imenuitemcount=sizeof(temp)/sizeof(temp[0]);
in main, then pass the count to all the other functions.
The program then becomes like this:
#include<conio.h>
#include<iostream>
int nmenuitemcount;
int nselectedindex=0;
//═══════════════════════════════════════════════════════════════
void drawmenu(char menu[][10], int imenuitemcount)
{
nmenuitemcount=imenuitemcount;
textbackground(BLACK);
textcolor(YELLOW);
for(imenuitem=0;imenuitem<nmenuitemcount;imenuitem++)
{
if(imenuitem==nselectedindex)
{
textcolor(WHITE) ;
textbackground(BLUE) ;
}
else
{
textcolor(LIGHTGRAY) ;
textbackground(BLACK) ;
}
cprintf(menu[imenuitem]);
cprintf("\r\n");
}
}
int extendedkeypress(int key)
{
int iretvalue=0;
switch(key)
{
case 72:
if(nselectedindex>0)
{
nselectedindex--;
iretvalue=1;
}
break;
case 80:
if(nselectedindex<(nmenuitemcount-1))
{
nselectedindex++;
iretvalue=1;
}
}
return iretvalue;
}
int smenu(char menu[][10], int imenuitemcount)
{
int brunning=1;
nselectedindex=0;
int nkey;
int brefreshmenu=1;
int toreturn;
while(brunning==1)
{
if(brefreshmenu==1)
{
drawmenu(menu, imenuitemcount);
}
nkey=getch();
switch(nkey)
{
case 0:
brefreshmenu=extendedkeypress(getch());
break;
case 13:
toreturn=nselectedindex;
break;
}
}
textbackground(BLACK);
clrscr();
return toreturn;
}
void main()
{
char temp[][10]={
"1.ABC ",
"2.DEF ",
"3.GHI ",
};
int imenuitemcount=sizeof(temp)/sizeof(temp[0]);
int y;
y=smenu(temp, imenuitemcount);
std::cout<<y;
getch();
}
You probably don't need the global variable nmenuitemcount if you are passing the count to all the functions.
Sorry that I cannot verify that the menus are created correctly. I don't have Turbo C.
I hope that helps you.
Best regards
Zlatko