creating text button

I use Turbo c++ 3.0 as well as CodeBlocks compiler/IDE on Windows 7 32 - bit OS.
I want to create a text button as it is in the Turbo c++ 3.0 IDE (if anyone has ever ised it)
I dont want to create a button as it appears in a windows application nor do i want to use windows.h.
I have written this code in the turbo c++ ide.
But as soon as the program execution (or rather the getch() ) is over an error occurs.

Error :
The NTVDM COU has encountered an illegal instruction.
CS : 8e14 IP:69f2 OP:ff ff eb 05ea

please me rectify the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  #include<iostream.h>
#include<conio.h>
#include<stdio.h>
class button
{
	public :

		int posx, posy;

		char caption[];

		void showbutton()
		{
			gotoxy(posx, posy);
			textcolor(0);
			textbackground(7);
			for(int i=0;i<strlen(caption);i++)
			{
				cprintf("%c", caption[i]);
			}
		}

		void position(int x, int y)
		{
		    posx = x;
		    posy = y;
		}

		void getcaption(char cap1[])
		{
			for(int i=0; i<strlen(cap1); i++)
			{
				caption[i]=cap1[i];
			}
		}
};
void main()
{
	char filecap[] = "Filewhatisur";
	clrscr();
	_setcursortype(_NOCURSOR);

	button file;
	file.position(1,1);
	file.getcaption(filecap);
	file.showbutton();

	getch();
}
this problem occurs especially if the caption of the button is too long (as it is in this code)
char caption[];

This is not legal C++. Were it legal, it would indicate a zero-sized array and writing to it would trash memory that you do not own. Supply a size (in the form of a compile time constant) for your array, and do not exceed it.
Last edited on
Which is the illegal part of the program ? (if u are talking about the textcolor() and all those functions, they are in the conio.h )
Even if i remove those functions and supply a size to the array i doesnt work.
Which is the illegal part of the program ?
as cire told you:

char caption[];

you cannot write data to it.
Use a reasonable length like

char caption[100];

make sure that you copy onle 99 (+ appending 0) char to it
Which is the illegal part of the program ?


The portion of code I included in my post so it would be clear what I was referring to. You are also using caption as a c-string without observing the conventions for using one. A c-string is terminated with a nul character.

Your getcaption is actually setting the caption.
actually the program does execute (it does otput the button or just the text on the screen ) and the showbutton() works well but the problem occurs when the user presses a key in response to the getch()
But i'll still try out what u guys said, thanks
I used char caption[25]and it works pretty well. But a new problem has arised, when i declare new object button open; and set its caption as "open" the program actually catenates the caption of both objects and displays it. Please help me. Also when i tried file.caption = "file"; it says Lvalue required. Help me rectify the error.
show your actual code
This the header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#ifndef BUTTON_H_INCLUDED
#define BUTTON_H_INCLUDED
#include<string.h>
char uppershadow = 223;
char lowershadow = 220;
class button
{
	private :

		int x, y;

		char caption[25];

	public :

		void getcaption(char cap1[])
		{
			for(int i=0; i<strlen(cap1); i++)
			{
				caption[i]=cap1[i];
			}
		}

		void showbutton()
		{
			gotoxy(x, y);
			textcolor(0);
			textbackground(7);
			for(int i=0;i<strlen(caption);i++)
			{
				cprintf("%c", caption[i]);
			}
		}

		void position(int posx, int posy)
		{
		    x = posx;
		    y = posy;
		}

		int left()
		{
			return x;
		}

		int top()
		{
			return y;
		}

};
#endif // BUTTON_H_INCLUDED 


and this the source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<objects.h>
void main()
{
	char filecap[4] = "File";
	clrscr();
	_setcursortype(_NOCURSOR);

	button file;
	file.position(5,5);
	file.getcaption(filecap);
	file.showbutton();

	button open;
	char opencap[4]= "open";
	open.position(10,10);
	open.getcaption(opencap);
	open.showbutton();

	getch();
}
Last edited on
on line 7 and 17: the string is longer than 4 char (you keep forgetting the terminating 0)

write it like so:
1
2
	char filecap[] = "File"; // Note: [] the compiler determines the correct length
	char opencap[]= "open"; // Note: [] the compiler determines the correct length 


1
2
3
4
5
6
7
		void getcaption(char cap1[])
		{
			for(int i=0; i<strlen(cap1); i++) // Note: strlen excludes the terminating 0
			{
				caption[i]=cap1[i];
			}
		}
use strcpy:

http://www.cplusplus.com/reference/cstring/strcpy/?kw=strcpy

1
2
3
4
		void getcaption(char cap1[])
		{
			strcpy(caption, cap1);
		}
still: getcaption is really setcaption
thank you coder777 i had really forgotten about the strcpy function. And i do know that getcaption is actually setting the caption. The code is working properly now.
Last edited on
Topic archived. No new replies allowed.