C++ problem
C++ problem
ok ive had this problem for months, but i havent worked on it. what is wrong with this code? its supposed to make the sides of a box in dos. this is the part thats supposed to do the center lines:
// build the center line
blanks[0] = border[7];
i = rgt - lft;
blanks[ i ] = border[3];
blanks[ i+1 ] = 0;
//write the center lines
i = top + 1;
while ( i < btm )
{
gotoxy( lft, i );
cprintf( blanks );
i++;
}
this isnt the whole code, this is just the portion that is supposed to do the center lines i guess.
// build the center line
blanks[0] = border[7];
i = rgt - lft;
blanks[ i ] = border[3];
blanks[ i+1 ] = 0;
//write the center lines
i = top + 1;
while ( i < btm )
{
gotoxy( lft, i );
cprintf( blanks );
i++;
}
this isnt the whole code, this is just the portion that is supposed to do the center lines i guess.
Re:C++ problem
ok then. here is a .txt file with the whole file. whats wrong?
[attachment deleted by admin]
[attachment deleted by admin]
Re:C++ problem
ok, joey, so you have provided the whole source code, thus faciliating some quick checks. for I have no c++ compiler at this pc at work, I canna do a test compile, but from what I see, you might have some small mistake in it:
It is this place here in your main function:
box_draw( 5, 5, 45, 15, "?????? ", 62, 23 );
Here is the declaration of your box_draw:
void box_draw( int lft, int top, int rgt, int btm,
char* border, int outline_clr, int contents_clr );
Mark the following: if you define somewhere a pointer, you have to pass an adress to it bofore you can use it further, else it points to nowhere in silicium hell.
This char* border pointer you have to provide with the adress of a string instead of a string for it is a POINTER!
Know about pointers and how to use the *-operator with them? It's nearby the same as with the brackets[] in nasm.
I'll tell you a bit about it:
with int *p you define a pointer to any integer var.
with p=&integervar you pass the adress of any integer to the pointer.
with p++/p-- you increment/decrement the adress of the pointer by the size of the datatype it points to. p. ex. for our int-pointer p p would then increase by 4 bytes.
with *p you access the content of the variable p points to.
so for your problem:
you ought define a string in your main function:
stay safe
It is this place here in your main function:
box_draw( 5, 5, 45, 15, "?????? ", 62, 23 );
Here is the declaration of your box_draw:
void box_draw( int lft, int top, int rgt, int btm,
char* border, int outline_clr, int contents_clr );
Mark the following: if you define somewhere a pointer, you have to pass an adress to it bofore you can use it further, else it points to nowhere in silicium hell.
This char* border pointer you have to provide with the adress of a string instead of a string for it is a POINTER!
Know about pointers and how to use the *-operator with them? It's nearby the same as with the brackets[] in nasm.
I'll tell you a bit about it:
with int *p you define a pointer to any integer var.
with p=&integervar you pass the adress of any integer to the pointer.
with p++/p-- you increment/decrement the adress of the pointer by the size of the datatype it points to. p. ex. for our int-pointer p p would then increase by 4 bytes.
with *p you access the content of the variable p points to.
so for your problem:
you ought define a string in your main function:
Code: Select all
char border[8]="12345678"
you then call box_draw in this way:
box_draw( 5, 5, 45, 15, border, 62, 23 );
the parameter char *border is provided with the adress of the string you have defined, because the name of an array is also it's adress.(equal to &border[0])
it should do what you expect it to do then.
Re:C++ problem
Beyond: some good reasoning in your post there. However, it had one small but fatal flaw: a string literal is a pointer.
Therefore these two are equally valid:
Joey: I can't see anything obviously wrong with your code, but then again, it won't compile on my machine (I don't have a <conio.h> that is compatible with yours).
Therefore these two are equally valid:
Code: Select all
char str1[] = "hello\n";
char *str2 = "hello\n";
printf(str1);
printf(str2);
Re:C++ problem
One small thing.
I wouldn't recommend using
and instead use
for the simple reason that if a user can set what's in str1 and puts in a bunch of %n's (writes the number of characters to an int pointer), he could manipulate printf to write data to strange locations.
- Nick
I wouldn't recommend using
Code: Select all
printf(str1);
Code: Select all
printf("%s", str1);
- Nick
Re:C++ problem
aye tim, that's correct.
's been an error of mine blinded by kernel coding and so forth. Sometimes one traps himself and overlooks the small essential thingses.
's been an error of mine blinded by kernel coding and so forth. Sometimes one traps himself and overlooks the small essential thingses.
Re:C++ problem
That's right for this example, but still: be aware of such pointers. Following example will generate a Segmentation Fault:Tim Robinson wrote: Therefore these two are equally valid:Code: Select all
char str1[] = "hello\n"; char *str2 = "hello\n"; printf(str1); printf(str2);
Code: Select all
char *str = "Hello\n";
printf("%s", str);
strcpy(str, "Bla\n");
printf("%s", str);
Just wanted to point out to this thing
Re:C++ problem
[tt]char *str = "Hello";[/tt] does reference a specific address, but the compiler has probably put it in a read-only section within the program's executable. For example, the gcc option [tt]-fwritable-strings[/tt] would let you do [tt]strcpy("Hello", "World");[/tt].
Re:C++ problem
all of you just confused the hell out of me. so what do i change to fix it? what tim said?
Re:C++ problem
No, the last few posts were red herrings.
I can't see anything wrong with your code, except a small mistake here:
I assume that first '+' is meant to be '='.
I get this output:
[tt]+-------------------------+write the bottom[/tt]
Edit: replaced linedraw characters with - and + (YaBB doesn't like HTML &1234; codes).
I can't see anything wrong with your code, except a small mistake here:
Code: Select all
//write the center line
i + top + 1;
I get this output:
[tt]+-------------------------+write the bottom[/tt]
Edit: replaced linedraw characters with - and + (YaBB doesn't like HTML &1234; codes).