OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 1:38 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: C++ problem
PostPosted: Wed May 07, 2003 12:58 pm 
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.


Top
  
 
 Post subject: Re:C++ problem
PostPosted: Tue May 13, 2003 3:31 pm 
ok then. here is a .txt file with the whole file. whats wrong?

[attachment deleted by admin]


Top
  
 
 Post subject: Re:C++ problem
PostPosted: Thu May 15, 2003 12:50 am 
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:

Code:

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.



stay safe


Top
  
 
 Post subject: Re:C++ problem
PostPosted: Thu May 15, 2003 2:14 am 
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:
Code:
char str1[] = "hello\n";
char *str2 = "hello\n";
printf(str1);
printf(str2);

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).


Top
  
 
 Post subject: Re:C++ problem
PostPosted: Thu May 15, 2003 2:40 am 
One small thing.

I wouldn't recommend using

Code:
printf(str1);


and instead use

Code:
printf("%s", str1);


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


Top
  
 
 Post subject: Re:C++ problem
PostPosted: Thu May 15, 2003 2:46 am 
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.


Top
  
 
 Post subject: Re:C++ problem
PostPosted: Thu May 15, 2003 5:51 am 
Tim Robinson wrote:
Therefore these two are equally valid:
Code:
char str1[] = "hello\n";
char *str2 = "hello\n";
printf(str1);
printf(str2);


That's right for this example, but still: be aware of such pointers. Following example will generate a Segmentation Fault:
Code:
char *str = "Hello\n";

printf("%s", str);
strcpy(str, "Bla\n");
printf("%s", str);

The pointer str does not reference to a specified address. Chaning char *str to char str[] will make the program working perfectly.
Just wanted to point out to this thing ;)


Top
  
 
 Post subject: Re:C++ problem
PostPosted: Thu May 15, 2003 8:52 am 
[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].


Top
  
 
 Post subject: Re:C++ problem
PostPosted: Thu May 15, 2003 12:41 pm 
all of you just confused the hell out of me. so what do i change to fix it? what tim said?


Top
  
 
 Post subject: Re:C++ problem
PostPosted: Thu May 15, 2003 4:38 pm 
No, the last few posts were red herrings.

I can't see anything wrong with your code, except a small mistake here:
Code:
//write the center line
i + top + 1;

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).


Top
  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 101 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group